SemaTemplateInstantiateDecl.cpp revision 60e141e1f87211ca831de6821003d80fe20a06f3
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,
341d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola                                 D->getStorageClass());
34238afbc7361d861968232defaeaf8e302af75b5eeRichard Smith  Var->setTLSKind(D->getTLSKind());
3435b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  Var->setInitStyle(D->getInitStyle());
344ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
345796c1a1e3e63e459e371383ac878aa5f40b02a8cRichard Smith  Var->setConstexpr(D->isConstexpr());
3461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
347b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
348b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
349b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
350b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
358a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3596b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (!D->isStaticDataMember()) {
360c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3616b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    Var->setReferenced(D->isReferenced());
3626b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  }
363a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
364671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith  SemaRef.InstantiateAttrs(TemplateArgs, D, Var, LateAttrs, StartingScope);
365671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith
366671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith  if (Var->hasAttrs())
367671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith    SemaRef.CheckAlignasUnderalignment(Var);
368671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith
369390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
370390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3716826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3726826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
373449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
37460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
37560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3769aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3779aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
3784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().ObjCAutoRefCount &&
3799aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
3809aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
3819aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3822c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain  SemaRef.CheckVariableDeclaration(Var, Previous);
3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
3853e9ea0b8cd7c4691d62e385245556be5fded58a7Richard Smith    D->getLexicalDeclContext()->addDecl(Var);
3867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
389f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
390f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
392be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
393251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
394251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
395251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
396a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
397cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
398a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
39960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
40060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
40160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
4021f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
403adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith      SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated, D);
4041f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
405adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, D);
4061f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
4085b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs,
4095b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                        D->getInitStyle() == VarDecl::CallInit);
4105b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (!Init.isInvalid()) {
41134b41d939a1328f484511c6002ba2456db879a29Richard Smith      bool TypeMayContainAuto = true;
4125b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      if (Init.get()) {
4135b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        bool DirectInit = D->isDirectInit();
4145b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit,
4155b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                     TypeMayContainAuto);
4165b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      } else
4176aeaa60217e1ed11a621409acf1b53df0d14b591Eli Friedman        SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
4186eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4226eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
423a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4241f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
425ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
426ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith             !Var->isCXXForRangeDecl())
427d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
4283d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
429e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // Diagnose unused local variables with dependent types, where the diagnostic
430e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // will have been deferred.
431e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
432e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith      D->getType()->isDependentType())
4335764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
434bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
4353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4363d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4373d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4386206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4396206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4406206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4416206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4426206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4436206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4446206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4456206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
448a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
449561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
450836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
45107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
45207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
45307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
454a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
45507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
45607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
46407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
467b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
468b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
475f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
476f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
477f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47960d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
480ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
485e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
48807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
48907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
494ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                            D->getInClassInitStyle(),
495703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith                                            D->getInnerLocStart(),
4968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
498663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
499663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
500f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
501663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
504a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
505be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith  if (Field->hasAttrs())
506be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith    SemaRef.CheckAlignasUnderalignment(Field);
507be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
508f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
509f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
511f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
512f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
513f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
514a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
5159901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
5169901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
5177a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
5189901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
52246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
523f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
52876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCallDecl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) {
52976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  bool Invalid = false;
53076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
53176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
53276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (DI->getType()->isVariablyModifiedType()) {
53376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
53476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    << D->getName();
53576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Invalid = true;
53676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else if (DI->getType()->isInstantiationDependentType())  {
53776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
53876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           D->getLocation(), D->getDeclName());
53976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    if (!DI) {
54076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      DI = D->getTypeSourceInfo();
54176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
54276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    } else if (DI->getType()->isFunctionType()) {
54376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      // C++ [temp.arg.type]p3:
54476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   If a declaration acquires a function type through a type
54576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   dependent on a template-parameter and this causes a
54676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   declaration that does not use the syntactic form of a
54776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   function declarator to have function type, the program is
54876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   ill-formed.
54976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
55076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      << DI->getType();
55176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
55276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    }
55376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else {
55476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
55576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  }
55676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
55776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  MSPropertyDecl *Property = new (SemaRef.Context)
55876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      MSPropertyDecl(Owner, D->getLocation(),
55976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getDeclName(), DI->getType(), DI,
56076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getLocStart(),
56176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getGetterId(), D->getSetterId());
56276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
56376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs,
56476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           StartingScope);
56576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
56676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (Invalid)
56776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Property->setInvalidDecl();
56876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
56976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Property->setAccess(D->getAccess());
57076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Owner->addDecl(Property);
57176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
57276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  return Property;
57376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall}
57476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
57587c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
57687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
57787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
57887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
57987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
58087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
58187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
582b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
583a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
584b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
585b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
586b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
587a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
588b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
589b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
59087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
59140e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
59287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
59387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
59440e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
59587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
59687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
59787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
59887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
59987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
60087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
60187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
60287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
60387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
60402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
60502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
60606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
60732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
6084fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
6094fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
6104fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
6114fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
6124fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
6134fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
6144fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
6154fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
6164fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
6174fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
6184fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
6194fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
62006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
621fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
622d6f80daa84164ceeb8900da07f43b6a150edf713Richard Smith    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
6230216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
62406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
62506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
626a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
62706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
6289a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
62906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
63006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
631a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
632a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
63306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
63406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
63532f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
636af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
637af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
638af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
639af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
640af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
64106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
6421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
644a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
64506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
6465fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
6479a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
64802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
64902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
650fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
651fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
6528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
6538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
656f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
657f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
660ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
6618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
6628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
6638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
664e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith  return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
6659ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
666e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getMessage(),
667e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getRParenLoc(),
668e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->isFailed());
6698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
67238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  EnumDecl *PrevDecl = 0;
67338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (D->getPreviousDecl()) {
67438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
67538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   D->getPreviousDecl(),
67638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   TemplateArgs);
67738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    if (!Prev) return 0;
67838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    PrevDecl = cast<EnumDecl>(Prev);
67938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  }
68038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith
681ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
68338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                    PrevDecl, D->isScoped(),
684a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6851274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
686f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
6871274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6881274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6891274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6901274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
691f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
692f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                                DeclarationName());
693f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
6941274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
695f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      else
696f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith        Enum->setIntegerTypeSourceInfo(NewTI);
697f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    } else {
6981274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6991274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
7001274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
7011274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
7021274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
7031274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
7045b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
7055b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
706f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
70706c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
708b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
70917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
710f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
7114ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  EnumDecl *Def = D->getDefinition();
7124ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  if (Def && Def != D) {
7134ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // If this is an out-of-line definition of an enum member template, check
7144ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // that the underlying types match in the instantiation of both
7154ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // declarations.
7164ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
7174ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
7184ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      QualType DefnUnderlying =
7194ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith        SemaRef.SubstType(TI->getType(), TemplateArgs,
7204ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                          UnderlyingLoc, DeclarationName());
7214ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
7224ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                                     DefnUnderlying, Enum);
7234ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    }
7244ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  }
7258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
72696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
72796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
728a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
729f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // C++11 [temp.inst]p1: The implicit instantiation of a class template
730f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // specialization causes the implicit instantiation of the declarations, but
731f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // not the definitions of scoped member enumerations.
732f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: There appears to be no wording for what happens for an enum defined
73338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // within a block scope, but we treat that much like a member template. Only
73438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // instantiate the definition when visiting the definition in that case, since
73538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // we will visit all redeclarations.
73638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (!Enum->isScoped() && Def &&
73738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith      (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
7384ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    InstantiateEnumDefinition(Enum, Def);
739f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
740f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return Enum;
741f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
742f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
743f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid TemplateDeclInstantiator::InstantiateEnumDefinition(
744f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    EnumDecl *Enum, EnumDecl *Pattern) {
745f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->startDefinition();
746f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
7471af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  // Update the location to refer to the definition.
7481af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  Enum->setLocation(Pattern->getLocation());
7491af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
7505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
7518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
753f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
754f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith         ECEnd = Pattern->enumerator_end();
7558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
7568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
75760d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
758ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
759f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
761f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
7621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
763ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
764ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
7658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
7678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
7688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
7698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
7708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
7718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
7748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
7758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
7769ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
7778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
7798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
7808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
7818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
7828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
785581deb3da481053c4993c7600f97acf7768caac5David Blaikie      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
7865b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
7873b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
78817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
789d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
7908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
791a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
792f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (Pattern->getDeclContext()->isFunctionOrMethod() &&
793f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith          !Enum->isScoped()) {
79496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
79596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
796581deb3da481053c4993c7600f97acf7768caac5David Blaikie        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
79796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
7988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
801f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: Fixup LBraceLoc
802f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
803f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                        Enum->getRBraceLoc(), Enum,
8049ff2b421f352fe0a0769c0a2a75af922c147b878Dmitri Gribenko                        Enumerators,
805fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
8068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
8078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
8086477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
809b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
8106477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
8116477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
812e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
81493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
815550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
816550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
8172a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
818e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
819ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
821d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
822e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
823e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
82493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
82593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
82693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
82793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
828c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
829c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
830c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
831c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
832c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
833c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
83493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
83593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
83693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
83793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
83893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
839ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
84037574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
8413bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
8423bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
84337574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
84437574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
84537574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
84637574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
84737574f55cd637340f651330f5cfda69742880d36Nick Lewycky
84893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
84993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
85093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
85193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
85293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
85393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
854c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
85593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
856c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
85793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
85893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
85993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
86093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
86193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
86293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
86393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
86493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
86593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
86693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
86793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
86893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
86993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
87093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
87193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
87293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
87393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
87493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
87593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
87693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
877c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
87893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
8791eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
880c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
88193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
88293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
88393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
884c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
88593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
886c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
887c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
888c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
889c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
890c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
891c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
892c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
893c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
894a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
895c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
896c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
897c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
898c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
899c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
900c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
901c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
902c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
903c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
904c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
905c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
906c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
907c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
908c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
909c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
910c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
911c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
91293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
91393ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
91493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
91593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
91693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
917a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
918c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
919c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
920c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
921c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
922c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
923c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
924c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
92593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
92693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
92793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
928c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
929c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
93093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
93193ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
93293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
93393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
93493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
935e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
93693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
937ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
938ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
939f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
940e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
941c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
942c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
943b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
944e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
94593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
94693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
94793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
948e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
949ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
95093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
951ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
952ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
953ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
954ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
955ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
95693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
95793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
95893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
95993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
960e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
96137574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
96237574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
96393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
964a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
965f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
9663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
96724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
968ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
969259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
97093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
9711b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(Inst);
9724c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
9734c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
974e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
975259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
976a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
9774c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
9784c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
9794c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
9804c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
9814c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
982e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
983d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
984d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
985d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
986d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
987d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
9885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
989d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
990d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
991d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
992d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
993d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
994d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
995e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
996e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
997e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
998d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
9997974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
10007974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
1001ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
1002a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1003ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
1004ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
1005ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
1006ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
10073bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie  if (Found.empty())
1008ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
1009a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1010ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
10113bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    = dyn_cast<ClassTemplateDecl>(Found.front());
1012ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
1013ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
1014a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1015d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
1016d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
1017d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
1018d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
1019d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
10207974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
10217974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
10227974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
1023d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1024550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
1025550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
1026a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
1027550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
10282a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1029895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
1030d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
1031d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
10321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
1033d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
1034a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1035a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
1036a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
1037a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
1038a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
1039a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
1040a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
1041a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
1042a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
1043a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1044a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
1045d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
1046d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
1048d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
1049a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
1050a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
105137d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
1052a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1054e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
1055b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1056b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1057e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
1058e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
1059e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1060b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1061a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
1062a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1063b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
10641f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (!isFriend) {
1065a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
10661f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (InstTemplate->getDeclContext()->isRecord() &&
10671f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall             !D->getPreviousDecl()) {
10681f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    SemaRef.CheckFriendAccess(InstTemplate);
10691f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  }
1070b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1071d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
1072d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
1073d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1074d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1075d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
1076d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
1077d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
1078ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
10797c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1080ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
10816c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
10826c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
10836c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
10846c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
1085d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
1086d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
10871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1088ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
1089ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
1090b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1091b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1092b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
1093b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1094b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1095d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
1096eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
1097eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
1098eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
1099eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
1100eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
1101d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
1102f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1103d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
110402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
110502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
110602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
110702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
110802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
11099901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
11109901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
11119901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
11127a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
11139901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
11149901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
1115d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
111617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
1117d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1118d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1119d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
112071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \brief Adjust the given function type for an instantiation of the
112171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// given declaration, to cope with modifications to the function's type that
112271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// aren't reflected in the type-source information.
112371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor///
112471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param D The declaration we're instantiating.
112571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param TInfo The already-instantiated type.
112671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregorstatic QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
112771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   FunctionDecl *D,
112871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   TypeSourceInfo *TInfo) {
1129bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *OrigFunc
1130bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = D->getType()->castAs<FunctionProtoType>();
1131bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *NewFunc
1132bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = TInfo->getType()->castAs<FunctionProtoType>();
1133bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1134bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    return TInfo->getType();
1135bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor
1136bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1137bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  NewEPI.ExtInfo = OrigFunc->getExtInfo();
1138bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  return Context.getFunctionType(NewFunc->getResultType(),
1139bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                 ArrayRef<QualType>(NewFunc->arg_type_begin(),
1140bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                    NewFunc->getNumArgs()),
1141bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor                                 NewEPI);
114271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor}
114371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor
114402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
114502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
114602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
114702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
114802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
11497557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1150a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1151127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1152127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1153127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1154b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1155c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11571e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
11582c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1159c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(), Innermost.size(),
11602c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1162127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
11632c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
11642c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1165127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1167b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1168b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1169b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1170b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1171b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1172b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
117379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1174b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1175a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
117679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
11772a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
118064b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
118121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
11822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
118371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1184fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1185c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1186c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1187c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1188c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1189c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1190c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1191d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1192d325daa506338ab86f9dd468b48fd010673f49a6John McCall
119368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
119468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
119568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
119668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
119768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1198c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  else if (isFriend && QualifierLoc) {
1199d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1200c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1201d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1202d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1203d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1204a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
12057c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1206d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
120768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
120802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1209ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1210635311f94e8fd4ff153130d91046ff78ffe97b06Abramo Bagnara                           D->getNameInfo(), T, TInfo,
1211459ef03126f9f0420efb3355e3b2ed3c1fdfb38aRafael Espindola                           D->getCanonicalDecl()->getStorageClass(),
1212af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
121386c3ae46250cdcc57778c27826060779a92f3815Richard Smith                           D->isConstexpr());
1214b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1215d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1216d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
1217d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1218c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1219c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1220b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1221b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1222b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1223b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1224b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1225b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1226b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1227b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1229e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
12305cbe101b502e06d16bc77df45a27ce8bc13f33c8Douglas Gregor  if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
12311d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Adopt the already-instantiated parameters into our own context.
12321d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (unsigned P = 0; P < Params.size(); ++P)
12331d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      if (Params[P])
12341d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        Params[P]->setOwningFunction(Function);
12351d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  } else {
12361d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Since we were instantiated via a typedef of a function type, create
12371d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // new parameters.
12381d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    const FunctionProtoType *Proto
12391d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      = Function->getType()->getAs<FunctionProtoType>();
12401d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    assert(Proto && "No function prototype in template instantiation?");
12411d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
12421d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor         AE = Proto->arg_type_end(); AI != AE; ++AI) {
12431d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      ParmVarDecl *Param
12441d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
12451d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor                                             *AI);
12461d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Param->setScopeInfo(0, Params.size());
12471d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Params.push_back(Param);
12481d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    }
12491d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  }
12504278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
125102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1252ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1253a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1254a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1255a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1256a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1257a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1258a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1259a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1260a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1261a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1262a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1263a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1264a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1265a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1266a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1267a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1268d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1269a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1270a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1271a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1272a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1273b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1274b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1275d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1276d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1277d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1278d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1279d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1280d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1281d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
128266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
128366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1284c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1285838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1286910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
1287c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.begin(),
1288c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.size()),
12891e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                                /*InsertPos=*/0);
129080f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
129180f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
129280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
129380f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
129480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
129580f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
129680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1297d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
129802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1299a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1300e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1301e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1303af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1304a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
13056826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
13066826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
13076826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1308af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1309af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1310af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1311af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1312af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1313af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1314af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1315af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1316af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1317af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1318e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1319e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1320e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1321af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1322af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1323af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1324af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1325af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1326af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1327af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1328af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1329af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1330af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1331af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1332af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1333af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1334af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1335af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1336a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1337af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1338af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1339af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1340a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1341a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1342a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
13436826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1344a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1345a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1346a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1347a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1348a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13496826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13506826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1351a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1352a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
13539f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
13542c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1355e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
135676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
135776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
135876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
135976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1360a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1361a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1362d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
13636826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
136476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1365ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = FunctionTemplate->getPreviousDecl();
136676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1367ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = Function->getPreviousDecl();
136876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
136976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
13701b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1371ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
137277535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1373ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
137453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++98 [temp.friend]p5: When a function is defined in a friend function
137553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is defined at each
137653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   instantiation of the class template. The function is defined even if it
137753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   is never used.
137853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++11 [temp.friend]p4: When a function is defined in a friend function
137953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is instantiated when the
138053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   function is odr-used.
138153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //
138253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // If -Wc++98-compat is enabled, we go through the motions of checking for a
138353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // redefinition, but don't instantiate the function.
138480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if ((!SemaRef.getLangOpts().CPlusPlus11 ||
138553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith         SemaRef.Diags.getDiagnosticLevel(
138653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             diag::warn_cxx98_compat_friend_redefinition,
138753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             Function->getLocation())
138853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith           != DiagnosticsEngine::Ignored) &&
1389238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1390238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1391238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
139210620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1393238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
139453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        SemaRef.Diag(Function->getLocation(),
139580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                     SemaRef.getLangOpts().CPlusPlus11 ?
139653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::warn_cxx98_compat_friend_redefinition :
139753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::err_redefinition) << Function->getDeclName();
1398238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
139980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        if (!SemaRef.getLangOpts().CPlusPlus11)
140053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          Function->setInvalidDecl();
1401a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1402238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1403238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1404238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1405238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1406238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
140713a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
140813a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1409ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1410ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
141180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          if (!SemaRef.getLangOpts().CPlusPlus11 &&
141253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              !queuedInstantiation && R->isUsed(false)) {
1413ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1414ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1415ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1416ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1417ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1418ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1419ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1420ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1421ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1422ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1423ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1424ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1425ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1426238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
14276a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
142810620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
142953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              SemaRef.Diag(Function->getLocation(),
143080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                           SemaRef.getLangOpts().CPlusPlus11 ?
143153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::warn_cxx98_compat_friend_redefinition :
143253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::err_redefinition)
1433238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
14346a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
143580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith              if (!SemaRef.getLangOpts().CPlusPlus11)
143653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                Function->setInvalidDecl();
1437238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1438238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1439238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1440238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1441238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1442a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1443a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
144476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
144576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
144676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
144776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1448eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1449e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1450e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
14512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1452d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1453d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1454af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1455af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
14566b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1457d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1460d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1461c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14631e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
14642c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1465c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(),
1466c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                             Innermost.size(),
14672c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
14681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14696b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
14702c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
14712c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
14726b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
14736b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1474b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1475b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1476b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1477b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1478b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1479b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
148079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1481a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
148279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
14832a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
148448dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
14854eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
14865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
14874eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
14884eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
14894eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
14904eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
14914eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
14924eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
14934eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
14944eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
14954eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
14964eab39f0745fb1949dbb40c4145771b927888242John McCall    }
14974eab39f0745fb1949dbb40c4145771b927888242John McCall  }
14984eab39f0745fb1949dbb40c4145771b927888242John McCall
14995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1500dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
150121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
15022dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
150371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
15042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1505723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1506723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1507723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
15085f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
15095f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
15105f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
15115f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
15125f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
15135f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1514723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
15155f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
15165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 4> ParamTypes;
1517a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1518a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                               D->getNumParams(), TemplateArgs, ParamTypes,
151912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               &Params))
1520a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      return 0;
15215f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
15225f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1523c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1524c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1525c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1526b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1527a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1528c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1529b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1530b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1531b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1532b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1533c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1534b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1535c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1536b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1537c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1538c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1539c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1540b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1541b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1542b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1543b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1544b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1545b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1546b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1547b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
15482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1549b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1550dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
15511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1552ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
15532577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
15542577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
155517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
15561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1557ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
155916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
156086c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                        false, Constructor->isConstexpr());
15614841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // Claim that the instantiation of a constructor or constructor template
15624841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // inherits the same constructor that the template does.
15634841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    if (const CXXConstructorDecl *Inh = Constructor->getInheritedConstructor())
15644841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
156517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
156617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1567ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15682577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
156916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
157065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
157165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1572ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15730130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1574f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
157586c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                       Conversion->isConstexpr(),
15769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1577dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
157872fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola    StorageClass SC = D->isStatic() ? SC_Static : SC_None;
15792577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1580ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
158172fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola                                   SC, D->isInlineSpecified(),
158286c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                   D->isConstexpr(), D->getLocEnd());
1583dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
15846b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1585d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1586d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Method->setImplicitlyInline();
1587d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1588c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1589c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1590b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1591d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1592d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1593d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1595d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1596d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1597d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1598d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1599d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1600d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1601d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1602d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1603d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1604d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1605d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1606d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1609b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1610b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1611b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1612b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1614d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
161566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
161666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1617c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1618838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1619910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1620c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.begin(),
1621c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.size()),
16221e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                              /*InsertPos=*/0);
1623b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
162466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
16252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
162666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1627a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
16297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
16307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1631b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
16324eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
16334eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
16344eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
16354eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
16364eab39f0745fb1949dbb40c4145771b927888242John McCall
1637b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1638b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1639b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
16407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
16435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
16445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
16454278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
16465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
16475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
16485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
16492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
16502577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
16512577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
16521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1653b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1654b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1656dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1657dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1658dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1659dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
16606826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
16616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1662dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
16632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1664af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
16652c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
166665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
16674ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
16684ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
16694ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
16701f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Propagate access.  For a non-friend declaration, the access is
16711f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // whatever we're propagating from.  For a friend, it should be the
16721f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // previous declaration we just found.
16731f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (isFriend && Method->getPreviousDecl())
16741f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(Method->getPreviousDecl()->getAccess());
16751f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  else
16761f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(D->getAccess());
16771f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (FunctionTemplate)
16781f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    FunctionTemplate->setAccess(Method->getAccess());
167946460a68f6508775e98c19b4bb8454bb471aac24John McCall
16809eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
16819eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
16823bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
1683ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith  if (D->isExplicitlyDefaulted())
1684ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
16853bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
1686ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDeleted(Method, Method->getLocation());
16873bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
16881f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // If there's a function template, let our caller handle it.
1689b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
16901f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
16911f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
16921f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Don't hide a (potentially) valid declaration with an invalid one.
1693b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
16941f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
16951f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
16961f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, check access to friends and make them visible.
16971f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (isFriend) {
16981f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // We only need to re-check access for methods which we didn't
16991f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // manage to match during parsing.
17001f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    if (!D->getPreviousDecl())
17011f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall      SemaRef.CheckFriendAccess(Method);
17021f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17031f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Record->makeDeclVisibleInContext(Method);
17041f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17051f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, add the declaration.  We don't need to do this for
17061f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // class-scope specializations because we'll have matched them with
17071f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // the appropriate template.
17081f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (!IsClassScopeSpecialization) {
17091f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Owner->addDecl(Method);
1710b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1711eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
17122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
17132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1715615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1716dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1717615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1718615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
171903b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
172017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
172103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
172203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1723bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
172465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1725bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1726bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
17276477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
172866874fb18afbffb8b2ca05576851a64534be3352David Blaikie  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
172966874fb18afbffb8b2ca05576851a64534be3352David Blaikie                                  /*ExpectParameterPack=*/ false);
17302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1734e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
17354fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
17361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1737e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1738344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1739344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
17404fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
17414fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
17449a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1745a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17460f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
1747a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1749a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1750550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1751550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1752a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1755e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
175633642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
175733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
175833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
17596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
17605f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
17615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
17626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1763a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
176433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
176533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
17666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
17676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1768a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
17696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
17706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
17716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
17726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
17736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
17746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1775a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
17766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
17776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
17786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1779a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
17816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
17826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
17836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
17846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
17856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
17866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1787a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
17896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
17906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
17916964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
17926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
17936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
17946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
179539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
17966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
17975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
17986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1799a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
18016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
18026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
18036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
1804dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> OrigNumExpansions
18056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
1806dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions = OrigNumExpansions;
18076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
18086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1809a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
18106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1811a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
18126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
18136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1814a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
18166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
18176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
18186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1819a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
18206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
18216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
18226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1823a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
18256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
18266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
18276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
18286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
18296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
18306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
18316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1832a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
18346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
18356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
18366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
18376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
18386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
18406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
18416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
18426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
18436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1844a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
18456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
18466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
18476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1848a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
18506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
18516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
18526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1853a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
18566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
18576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1858a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
18596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
18606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
18616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1862a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1864a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
18656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
18666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
18676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
18686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
18696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
187033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1871a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
18736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1874a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1875ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1876ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1877a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1878a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
18796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
18806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
18816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
18826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
18836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
18846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1885a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1886ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
18876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1888a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1889a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1890a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
18916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1892a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18939a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
189433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
189533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1896a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1897d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1898a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1899a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1900550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1901550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
190233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
190333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
190433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
19056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smithstatic void collectUnexpandedParameterPacks(
19066964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Sema &S,
19076964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    TemplateParameterList *Params,
19086964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
19096964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  for (TemplateParameterList::const_iterator I = Params->begin(),
19106964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             E = Params->end(); I != E; ++I) {
19116964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if ((*I)->isTemplateParameterPack())
19126964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      continue;
19136964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
19146964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
19156964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                        Unexpanded);
19166964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
19176964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
19186964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                      Unexpanded);
19196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  }
19206964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith}
19216964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19220dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
19239106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
19249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
19259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
19269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
19279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
19286964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  SmallVector<TemplateParameterList*, 8> ExpandedParams;
19296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19306964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  bool IsExpandedParameterPack = false;
19316964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19326964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (D->isExpandedParameterPack()) {
19336964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack is an already-expanded pack
19346964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // expansion of template parameters. Substitute into each of the expanded
19356964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // parameters.
19366964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
19376964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
19386964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith         I != N; ++I) {
19396964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
19406964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      TemplateParameterList *Expansion =
19416964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        SubstTemplateParams(D->getExpansionTemplateParameters(I));
19426964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!Expansion)
19436964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
19446964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      ExpandedParams.push_back(Expansion);
19456964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
19466964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    IsExpandedParameterPack = true;
19486964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    InstParams = TempParams;
19496964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
19506964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack expands to a pack of template
19516964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // template parameters. Determine whether we need to expand this parameter
19526964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // pack into separate parameters.
19536964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
19546964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
19556964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                    Unexpanded);
19566964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19576964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // Determine whether the set of unexpanded parameter packs can and should
19586964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // be expanded.
19596964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool Expand = true;
19606964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool RetainExpansion = false;
1961dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions;
19626964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
19636964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TempParams->getSourceRange(),
19646964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Unexpanded,
19656964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TemplateArgs,
19666964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Expand, RetainExpansion,
19676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                NumExpansions))
19686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
19696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19706964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (Expand) {
19716964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      for (unsigned I = 0; I != *NumExpansions; ++I) {
19726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
19736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        LocalInstantiationScope Scope(SemaRef);
19746964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
19756964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        if (!Expansion)
19766964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith          return 0;
19776964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        ExpandedParams.push_back(Expansion);
19786964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      }
19796964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19806964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // Note that we have an expanded parameter pack. The "type" of this
19816964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // expanded parameter pack is the original expansion type, but callers
19826964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // will end up using the expanded parameter pack types for type-checking.
19836964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      IsExpandedParameterPack = true;
19846964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = TempParams;
19856964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    } else {
19866964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // We cannot fully expand the pack expansion now, so just substitute
19876964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // into the pattern.
19886964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
19896964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19906964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
19916964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = SubstTemplateParams(TempParams);
19926964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!InstParams)
19936964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
19946964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
19956964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else {
19969106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
19979106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
19982a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
19999106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
20009106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
20016964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
2002a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
2003a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20049106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
20056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  TemplateTemplateParmDecl *Param;
20066964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (IsExpandedParameterPack)
20076964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20086964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
20096964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                   D->getDepth() - TemplateArgs.getNumLevels(),
20106964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20116964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams,
20126964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             ExpandedParams);
20136964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  else
20146964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20156964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
2016a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
20176964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20186964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->isParameterPack(),
20196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams);
2020d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
20219a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
2022a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2023a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
20249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
20259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
2026a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
20289106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
20299106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
203048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
2031db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
2032db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
2033a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
203448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
203548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2036a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
2037db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
2038a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
2039a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
204048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
2041536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
2042536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // Add the using directive to its declaration context
2043536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // only if this is not a function or method.
2044536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  if (!Owner->isFunctionOrMethod())
2045536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara    Owner->addDecl(Inst);
2046536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
204748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
204848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
204948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
2050ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
20511b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20521b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
20531b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
20541b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
20551b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
20561b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
20571b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
20581b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
20591b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
20605149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
20615149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
20625149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
20635149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
2064dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
20651b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20661b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
20671b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
2068ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
2069ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20709f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
20719f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
20729f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
20739f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
20749f54ad4381370c6b771424b53d219e661d6d6706John McCall
2075ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2076ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
20779f54ad4381370c6b771424b53d219e661d6d6706John McCall
2078ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
2079ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
20805149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
2081ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
2082ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
2083ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20845149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
20855149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
20869f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
20879f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
20889f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
20899f54ad4381370c6b771424b53d219e661d6d6706John McCall
20909f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
20919f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
20929f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
20939f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
20949f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
20959f54ad4381370c6b771424b53d219e661d6d6706John McCall
20969f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
20979f54ad4381370c6b771424b53d219e661d6d6706John McCall
20989f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
20999f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
2100ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
2101ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
21029f54ad4381370c6b771424b53d219e661d6d6706John McCall
2103ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2104ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
2105ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
2106ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21079f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
21089f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
21099f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
21109f54ad4381370c6b771424b53d219e661d6d6706John McCall
2111c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2112c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2113c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith      NewUD->setInvalidDecl();
2114c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    return NewUD;
2115c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  }
2116c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith
2117323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
2118323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
21199f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
21209f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
21219f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
21229f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
21239f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
2124b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2125b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
2126b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
2127b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
2128b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
2129b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
21309f54ad4381370c6b771424b53d219e661d6d6706John McCall
21319f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
21329f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
21339f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
21349f54ad4381370c6b771424b53d219e661d6d6706John McCall
21359f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
21369f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
21379f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
2138323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
2139323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
2140323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
21419f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
2142ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2143ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
2144ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2145ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2146ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
21479f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
21489f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
2149ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2150ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
21527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
21535149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
2154a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
21555149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
21565149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
21587ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
21597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
21605149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
2162ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
2163accaf19bc1129c0273ec50dba52318e60bc29103Benjamin Kramer  // Hence, no transformation is required for it.
2164ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
21657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
21667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2167ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
21687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
21697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
21704469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2171ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2172ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
21747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
21757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
21767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
21777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
21785149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
21795149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
21805149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21810dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
2182a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
21830dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
21845149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2186ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
2187ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2188ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
21891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
21909488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2191ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
21927ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
21937ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
21944469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2195ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2196ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21970d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
21980dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
21990dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
2200af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2201af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2202af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
2203af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
22046b02009359a462ffe633696a4441313b462e6566Nico Weber  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
22056b02009359a462ffe633696a4441313b462e6566Nico Weber                                                                0, true));
2206af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2207af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2208af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
2209af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
22106b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo TemplateArgs;
22116b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo* TemplateArgsPtr = 0;
22126b02009359a462ffe633696a4441313b462e6566Nico Weber  if (Decl->hasExplicitTemplateArgs()) {
22136b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgs = Decl->templateArgs();
22146b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgsPtr = &TemplateArgs;
22156b02009359a462ffe633696a4441313b462e6566Nico Weber  }
22166b02009359a462ffe633696a4441313b462e6566Nico Weber
2217af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
22186b02009359a462ffe633696a4441313b462e6566Nico Weber  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
22196b02009359a462ffe633696a4441313b462e6566Nico Weber                                                  Previous)) {
2220af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
2221af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
2222af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
2223af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2224af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
2225af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2226af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
2227af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2228af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2229af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
2230af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2231af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2232c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey BataevDecl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2233c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev                                     OMPThreadPrivateDecl *D) {
2234c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  SmallVector<DeclRefExpr *, 5> Vars;
2235c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  for (ArrayRef<DeclRefExpr *>::iterator I = D->varlist_begin(),
2236c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev                                         E = D->varlist_end();
2237c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev       I != E; ++I) {
2238c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    Expr *Var = SemaRef.SubstExpr(*I, TemplateArgs).take();
2239c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
2240c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    Vars.push_back(cast<DeclRefExpr>(Var));
2241c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  }
2242c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2243c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  OMPThreadPrivateDecl *TD =
2244c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2245c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2246c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  return TD;
2247c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev}
2248c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2249ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
2250d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
22517e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
22522fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
22532fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
22542fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
22558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
22568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
22578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2258e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
2259e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
2260e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2261e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
2262e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2263e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
2264e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
2265ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2266e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2267e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2268e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2269e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
22705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2271e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2272e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2273e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2274e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2275bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2276e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
22779148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2278e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2279e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2280e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2281ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
2282e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
2283e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2284e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2285e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2286e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2287e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2288e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
22891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2290e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2291a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2292ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2293ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2294ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2295ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2296ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2297a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2298ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2299ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2300d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2301d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2302d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2303ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2304ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2305ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2306550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2307550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2308550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
23092a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2310a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2311ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2312ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2313ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2314ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2315ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2316d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2317a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2318ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2319ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2320d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
2321a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2322a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                    PartialSpec->getNumTemplateArgsAsWritten(),
2323e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2324e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2325a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2326ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2327ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
23285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2329a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2330ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2331a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2332ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2333ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2334d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2335ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2336ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2337ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2338ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2339ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2340910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2341910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2342a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2343ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2344ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2345a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2346ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2347910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2348910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2349ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2350ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2351ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2352ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2353ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2354ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2355ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2356ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
23573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
23583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
23593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
23603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2361d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2362ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2363a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2364ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2365ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2366ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2367ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2368ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2369ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2370ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2371ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2372ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2373ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2374ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2375ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2376ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2377ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2378ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2379ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2380ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2381d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2382ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2383ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2384d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2385ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2386a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2387a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2388ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2389ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2390a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
239113c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2392a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2393ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2394ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2395ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2396a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2397910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2398910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2399d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
24003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
2401dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
2402cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
2403b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2404b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2405b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2406b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2407ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
24084469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2409a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2410ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2411ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
24121e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor  ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
2413d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2414ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2415ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
241621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
241721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
24185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
241921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
242021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
242121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
2422cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
2423cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *ThisContext = 0;
2424cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ThisTypeQuals = 0;
2425cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2426cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisContext = Method->getParent();
2427cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisTypeQuals = Method->getTypeQualifiers();
2428cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
2429cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
24306cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
24316cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
24326cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
2433cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    D->getDeclName(),
2434cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    ThisContext, ThisTypeQuals);
243521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
243621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
24375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2438cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
2439cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
2440140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
244139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    if (FunctionProtoTypeLoc OldProtoLoc =
244239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            OldTL.getAs<FunctionProtoTypeLoc>()) {
2443140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
244439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
2445500d729e85028944355a119f9823ac99fa5ddcabRichard Smith      unsigned NewIdx = 0;
244639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
244712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
244839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
2449500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2450500d729e85028944355a119f9823ac99fa5ddcabRichard Smith
2451dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie        Optional<unsigned> NumArgumentsInExpansion;
2452500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (OldParam->isParameterPack())
2453500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          NumArgumentsInExpansion =
2454500d729e85028944355a119f9823ac99fa5ddcabRichard Smith              SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2455500d729e85028944355a119f9823ac99fa5ddcabRichard Smith                                                 TemplateArgs);
2456500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (!NumArgumentsInExpansion) {
2457a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
245812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
245939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
246012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
2461500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->InstantiatedLocal(OldParam, NewParam);
2462500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        } else {
2463500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          // Parameter pack expansion: make the instantiation an argument pack.
2464500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->MakeInstantiatedLocalArgPack(OldParam);
2465500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
246639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
2467500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Params.push_back(NewParam);
2468500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2469500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          }
247012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
24716920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2472895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
2473cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
2474cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
2475cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
2476cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
2477140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
247839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    if (FunctionProtoTypeLoc OldProtoLoc =
247939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            OldTL.getAs<FunctionProtoTypeLoc>()) {
248039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
248139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc.getArg(i));
24826920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
24836920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
24846920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
24856920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2486cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2487cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
248821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
24895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
24905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2491e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// Introduce the instantiated function parameters into the local
2492e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// instantiation scope, and set the parameter names to those used
2493e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// in the template.
2494e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2495e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             const FunctionDecl *PatternDecl,
2496e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             LocalInstantiationScope &Scope,
2497e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
2498e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned FParamIdx = 0;
2499e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2500e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2501e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (!PatternParam->isParameterPack()) {
2502e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Simple case: not a parameter pack.
2503e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(FParamIdx < Function->getNumParams());
2504e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2505e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2506e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocal(PatternParam, FunctionParam);
2507e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2508e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2509e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2510e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2511e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Expand the parameter pack.
2512e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2513dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumArgumentsInExpansion
2514e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
2515500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    assert(NumArgumentsInExpansion &&
2516500d729e85028944355a119f9823ac99fa5ddcabRichard Smith           "should only be called when all template arguments are known");
2517500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
2518e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2519e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2520e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2521e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2522e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2523e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2524e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2525e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2526e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2527e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     const FunctionProtoType *Proto,
2528e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
252913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
253013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2531e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // C++11 [expr.prim.general]p3:
2532e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   If a declaration declares a member function or member function
2533e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   template of a class X, the expression this is a prvalue of type
2534e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2535e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   and the end of the function-definition, member-declarator, or
2536e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   declarator.
2537e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  CXXRecordDecl *ThisContext = 0;
2538e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned ThisTypeQuals = 0;
2539e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2540e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisContext = Method->getParent();
2541e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisTypeQuals = Method->getTypeQualifiers();
2542e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2543e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
254480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                                   SemaRef.getLangOpts().CPlusPlus11);
2545e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2546e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // The function has an exception specification or a "noreturn"
2547e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // attribute. Substitute into each of the exception types.
2548e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  SmallVector<QualType, 4> Exceptions;
2549e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2550e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // FIXME: Poor location information!
2551e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (const PackExpansionType *PackExpansion
2552e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2553e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // We have a pack expansion. Instantiate it.
2554e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2555e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2556e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                              Unexpanded);
2557e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(!Unexpanded.empty() &&
2558e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith             "Pack expansion without parameter packs?");
2559e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2560e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Expand = false;
2561e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool RetainExpansion = false;
2562dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions
2563e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                        = PackExpansion->getNumExpansions();
2564e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2565e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  SourceRange(),
2566e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Unexpanded,
2567e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  TemplateArgs,
2568e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Expand,
2569e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  RetainExpansion,
2570e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  NumExpansions))
2571e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2572e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2573e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!Expand) {
2574e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // We can't expand this pack expansion into separate arguments yet;
2575e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // just substitute into the pattern and create a new pack expansion
2576e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // type.
2577e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2578e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2579e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2580e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2581e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull())
2582e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2583e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2584e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2585e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2586e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        continue;
2587e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2588e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2589e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Substitute into the pack expansion pattern for each template
2590e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Invalid = false;
2591e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2592e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2593e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2594e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2595e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2596e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2597e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull()) {
2598e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          Invalid = true;
2599e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2600e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        }
2601e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2602e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2603e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2604e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2605e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (Invalid)
2606e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2607e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2608e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2609e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2610e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2611e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    QualType T
2612e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2613e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                          New->getLocation(), New->getDeclName());
2614e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (T.isNull() ||
2615e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2616e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2617e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2618e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Exceptions.push_back(T);
2619e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2620e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Expr *NoexceptExpr = 0;
2621e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2622e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
2623e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                 Sema::ConstantEvaluated);
2624e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2625e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable())
2626e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2627e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2628e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable()) {
2629e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      NoexceptExpr = E.take();
2630e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!NoexceptExpr->isTypeDependent() &&
2631e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          !NoexceptExpr->isValueDependent())
2632ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        NoexceptExpr
2633ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2634ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              0, diag::err_noexcept_needs_constant_expression,
2635ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              /*AllowFold*/ false).take();
2636e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2637e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2638e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2639e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Rebuild the function type
2640e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionProtoType *NewProto
2641e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    = New->getType()->getAs<FunctionProtoType>();
2642e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  assert(NewProto && "Template instantiation without function prototype?");
2643e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2644e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2645e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2646e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NumExceptions = Exceptions.size();
2647e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.Exceptions = Exceptions.data();
2648e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NoexceptExpr = NoexceptExpr;
2649e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2650e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2651bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  ArrayRef<QualType>(NewProto->arg_type_begin(),
2652bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                     NewProto->getNumArgs()),
2653e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                               EPI));
2654e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2655e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2656e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithvoid Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2657e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                    FunctionDecl *Decl) {
265813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
265913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  if (Proto->getExceptionSpecType() != EST_Uninstantiated)
2660e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2661e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2662e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2663e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                             InstantiatingTemplate::ExceptionSpecification());
2664b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  if (Inst) {
2665b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // We hit the instantiation depth limit. Clear the exception specification
2666b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // so that our callers don't have to cope with EST_Uninstantiated.
2667b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2668b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    EPI.ExceptionSpecType = EST_None;
2669b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Decl->setType(Context.getFunctionType(Proto->getResultType(),
2670bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                    ArrayRef<QualType>(Proto->arg_type_begin(),
2671bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                       Proto->getNumArgs()),
2672b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith                                          EPI));
2673e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2674b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  }
2675e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2676e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Enter the scope of this instantiation. We don't use
2677e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // PushDeclContext because we don't have a scope.
2678e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::ContextRAII savedContext(*this, Decl);
2679e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  LocalInstantiationScope Scope(*this);
2680e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2681e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
2682e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2683e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
268413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  FunctionDecl *Template = Proto->getExceptionSpecTemplate();
268513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
2686e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
268713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  ::InstantiateExceptionSpec(*this, Decl,
268813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             Template->getType()->castAs<FunctionProtoType>(),
268913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             TemplateArgs);
2690e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2691e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
26921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2693e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2694e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2695e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
26961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
26971eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2698e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
269985f485a70fbec54c9b4562dfc4d95188ea6c9b48David Blaikie  if (Tmpl->isDeleted())
270010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
27011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2702cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2703cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2704cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
27051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
27061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2707cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2708cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2709cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2710cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2711cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2712cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2713cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
27141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
27154a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky          = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
27161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2717cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2718bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2719cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
27204a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky      ActiveInst.Entity = New;
2721cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
2722cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
27231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27240ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
27250ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
27260ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
272760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
2728e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2729e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2730e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // DR1330: In C++11, defer instantiation of a non-trivial
2731e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
273280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 &&
2733e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_None &&
2734e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_DynamicNone &&
2735e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_BasicNoexcept) {
273613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      FunctionDecl *ExceptionSpecTemplate = Tmpl;
273713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      if (EPI.ExceptionSpecType == EST_Uninstantiated)
273813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith        ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
27394841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      ExceptionSpecificationType NewEST = EST_Uninstantiated;
27404841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      if (EPI.ExceptionSpecType == EST_Unevaluated)
27414841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith        NewEST = EST_Unevaluated;
274213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2743e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Mark the function has having an uninstantiated exception specification.
2744e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      const FunctionProtoType *NewProto
2745e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        = New->getType()->getAs<FunctionProtoType>();
2746e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(NewProto && "Template instantiation without function prototype?");
2747e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI = NewProto->getExtProtoInfo();
27484841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      EPI.ExceptionSpecType = NewEST;
2749e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecDecl = New;
275013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
2751e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2752bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  ArrayRef<QualType>(NewProto->arg_type_begin(),
2753bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                     NewProto->getNumArgs()),
2754e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                   EPI));
2755e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    } else {
2756e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2757e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
27580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
27590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
276019f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
2761e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionDecl *Definition = Tmpl;
276219f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
276319f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
276423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
276523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
27667cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2767e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2768e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2769e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
27705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
27715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
27725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
27735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
27745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
27751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
27761eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
27775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2778e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2779e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
27801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2782e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
278385606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
27845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
27855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
27865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
27875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2788a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2789a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2790a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2791a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2792b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2793b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2794b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2795b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2796a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2797b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2798b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2799b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2800b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2801b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2802e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2803e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2804e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2805e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2806f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2807b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2808e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2809e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
281010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
281154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
281254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2813af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
2814af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
2815af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2816af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
2817251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
28186cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
28191eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
28203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
2821f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
2822f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
2823f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2824f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
2825f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
2826f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
2827f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
2828dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
2829f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
28301eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
28318387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
2832f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
28338a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
28348387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
28358387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
28368387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
28378387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
28388387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
28398387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Call the LateTemplateParser callback if there a need to late parse
2840a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
2841f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
28428387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
28434a47e8d35dc1778ef7e428d9edd7676be67e725fFrancois Pichet    LateTemplateParser(OpaqueParser, PatternDecl);
28448387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
28458387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
28468387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
2847f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
2848e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2849e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2850a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2851e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2852e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2853e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2854a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2855e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2856e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2857a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2858e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2859a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
2860e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2861cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
286258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
286358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
286462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
286558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2866e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
286758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
28681eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2869e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
28701eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
287160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  // C++1y [temp.explicit]p10:
287260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   Except for inline functions, declarations with types deduced from their
287360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   initializer or return value, and class template specializations, other
287460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   explicit instantiation declarations have the effect of suppressing the
287560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   implicit instantiation of the entity to which they refer.
28761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2877d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
287860e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      !PatternDecl->isInlined() &&
287960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      !PatternDecl->getResultType()->isUndeducedType())
2880d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
28811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2882d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (PatternDecl->isInlined())
2883d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
2884d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
2885f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2886f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2887a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
2888a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2889e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
2890e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2891e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
2892b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2893b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2894b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
28955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
289662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
28972a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
28982a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
289962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
29002a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
29011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2902a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
2903f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2904e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
290554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
290660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
290760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
290860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
290960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
291060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
291160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
291260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
291360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
29141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29151d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  if (PatternDecl->isDefaulted())
29161d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    SetDeclDefaulted(Function, PatternDecl->getLocation());
29171d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  else {
29181d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    ActOnStartOfFunctionDef(0, Function);
29197c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
29201d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // Enter the scope of this instantiation. We don't use
29211d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // PushDeclContext because we don't have a scope.
29221d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    Sema::ContextRAII savedContext(*this, Function);
29237c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
29241d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    MultiLevelTemplateArgumentList TemplateArgs =
29251d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith      getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
292654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
29271d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
29281d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith                                     TemplateArgs);
29291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2930cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
2931cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
2932cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2933cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2934cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
2935cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
2936cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2937cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
2938cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2939cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2940cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
2941cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
2942a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2943cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
2944cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
2945b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
29461d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    PerformDependentDiagnostics(PatternDecl, TemplateArgs);
29470c01d18094100db92d38daa923c95661512db203John McCall
29481d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    savedContext.pop();
29491d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  }
2950aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2951aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2952aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
29531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
295460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
295560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
295662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
295760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
295860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2959b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
29602a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
29612a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
29622a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2963b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
29641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
296562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
29661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29672a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
29688155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
29698155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
29702a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
29712a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2972b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
29738155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
29748155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
297562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2976b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2977a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2978a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2979a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2980a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2981a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
29827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
29837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
29847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
29857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
29867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
29877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
29887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
29897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
29907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2991e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2992e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2993e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2994e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
29957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
29967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
29977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2998e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2999e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
30007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
30017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
30021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
30047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
30057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
3006a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(Def->isStaticDataMember() && "Not a static data member?");
30070d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
30081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30090d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
30107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
30117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
30121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
30131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
3014e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
30150d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
3016a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      Diag(PointOfInstantiation,
3017e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
3018e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
3019e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
302058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
302158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
302262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
302358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
302458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
302558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
30267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
30277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
30287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
3029234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
3030234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola
3031251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
3032234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitSpecialization)
3033251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
3034a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3035251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
3036251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
3037251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
3038251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
3039234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitInstantiationDeclaration)
3040251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
30411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3042afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  // Make sure to pass the instantiated variable to the consumer at the end.
3043afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  struct PassToConsumerRAII {
3044afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ASTConsumer &Consumer;
3045afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    VarDecl *Var;
3046afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3047afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3048afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      : Consumer(Consumer), Var(Var) { }
3049afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3050afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ~PassToConsumerRAII() {
3051afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3052afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    }
3053afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  } PassToConsumerRAII(Consumer, Var);
3054025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola
3055f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor  // If we already have a definition, we're done.
305695e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  if (VarDecl *Def = Var->getDefinition()) {
305795e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    // We may be explicitly instantiating something we've already implicitly
305895e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    // instantiated.
305995e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
306095e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky                                       PointOfInstantiation);
3061f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor    return;
306295e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  }
3063f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
30647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
30657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
30667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
30671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
30697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
30707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
30715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
307262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
30738155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
30748155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
307562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
30768155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
30771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
30797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
3080f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  ContextRAII previousContext(*this, Var->getDeclContext());
30817bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  LocalInstantiationScope Local(*this);
30827bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
30831028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
3084ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
30856bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
3086f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
3087f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  previousContext.pop();
30887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
30897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
3090afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII.Var = Var;
3091583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
3092583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
3093583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
3094583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
30957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
30967bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  Local.Exit();
30977bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
30987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
30998155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
31008155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
31018155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
31027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
31031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
310462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
31051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31068155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
31078155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
31088155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded, "
31098155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
31108155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
31118155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
31127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
31138155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
31148155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded, "
31158155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
311662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
31171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3118a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3119815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3120090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
3121090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
3122090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
3123090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
31241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
312590ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
312654b3ba8cf2eb4886a88cdb8adedb15f43333ff1dRichard Smith  bool AnyErrors = Tmpl->isInvalidDecl();
3127a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3128090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
3129090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
313072f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
313172f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
3132cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
3133090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
3134030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
3135030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
3136030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
3137030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
3138030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
31393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
3140a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31413fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
31423fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
314376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
31445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
31453fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
31463fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
3147d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
3148dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions;
3149a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
31503fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
3151a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
3152a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
3153d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
31543fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
31553fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
31563fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
31573fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
31583fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
31593fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
3160a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3161a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
3162cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
31633fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
31643fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31653fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
31665b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
31675b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                               /*CXXDirectInit=*/true);
31685b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        if (TempInit.isInvalid()) {
31693fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
31703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
31713fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
31723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31733fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
317476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
3175a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
3176a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
31773fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
31783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
31793fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
31803fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
31813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
31823fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31833fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
31846df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
31855b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                                     BaseTInfo, TempInit.take(),
31863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
31873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
31883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
31893fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
31903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
31913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
3192a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31933fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
31943fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
3195a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31963fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
31973fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
31983fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
32005b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
32015b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                           /*CXXDirectInit=*/true);
32025b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (TempInit.isInvalid()) {
32036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
32046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
3205090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3206a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3207090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
320876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
320976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
321076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
321176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
321276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
321376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
32149db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
3215802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
3216802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
3217802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
32186df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
321976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
32205b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
322176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
322276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
32235b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
322476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
3225090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
3226b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
322700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
322800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
322900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
3230b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
3231b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3232b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
3233b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
3234b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
32351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32365b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(Member, TempInit.take(),
32376df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
323800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
323900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
3240b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
324100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
324200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
324300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
3244b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
3245b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3246b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
32476df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
3248b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
32496df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
32505b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
32516df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
3252090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3253090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
32549db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
32559db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
3256090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
32579db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
325890ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
3259090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3260090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
32611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3262090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
3263d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
3264090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
3265090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
326693c8617bec98aeb769ee9f569d7ed439eec03249David Blaikie                       NewInits,
32679db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
3268090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
3269090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
327052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
327152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
327252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
327352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
327452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
327552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
327652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
327752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
327852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
327952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
328052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
328152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
328252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
328352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
328452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
32850d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
32860d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
32870d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
3288a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
32890d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
32900d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
32910d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
32920d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
32930d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
3294a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
32950d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
32960d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
32970d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3298a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
3299ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3300ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
3301a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
3302ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3303ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
3304ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
3305ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
3306ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
3307ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
3308ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
3309ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
3310a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3311ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
3312ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
3313ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
331452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
331552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
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->getInstantiatedFromMemberClass();
332252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
332352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
332452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
332552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
332652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
332752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
332852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
332952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
333052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
333152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
333252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
333352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
333452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
333552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
333652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
333752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
333852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
333952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
334052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
334152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
334252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
334352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
334452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
334552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
334652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
334752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
334852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
334952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
335052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
335152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
335252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3353ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
3354ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
3355ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3356ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3357ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3358ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3359ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
3360ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
3361ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3362ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3363ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3364ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
33657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
33667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
33677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
3368ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
33697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
33707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
33717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
33720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
33730d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
3374ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
33750d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
33760d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
337752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
337852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
337952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
338052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
338152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
338252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
338352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
338452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
338552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
338652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
338752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
338852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
338952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
339052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
339152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3392ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
3393ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
3394815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
33950d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
33967ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
33977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
33987ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
33997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
34007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
34017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
34027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
34037ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
34047ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
34050d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
34060d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
34070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
34080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
3409815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
34100d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
34110d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
34121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
341452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
34151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
341752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
3418815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
341952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
342052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
3421815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
34227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
342352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
342452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
342552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
342652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
342752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
3428a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
34290d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
34300d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
34310d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3432ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
3433ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3434ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3435ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
3436ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
3437d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3438d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
3439d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
34401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
3441d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
3442d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
3443d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
34441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3445ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3446ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3447ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3448ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3449ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3450ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3451815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
3452815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3453815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3454815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3455815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
34561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
3457815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
3458815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
3459815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
3460815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
3461815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
3462815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
3463815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3464815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
3465815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3466815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
346702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
346802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
346902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
347002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
34717c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
3472e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
347302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
34747c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
347502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
347602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
347702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
347802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
3479ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
3480ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
3481815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3482815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
3483815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
3484815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
3485815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
3486815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3487815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
3488815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
3489815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
3490815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
3491815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
3492815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
3493815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3494815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
3495815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
3496815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3497815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
3498815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
3499815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3500815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
3501815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
3502f198d126ecec1e07a474c419de1bf4dadf92a490James Dennett/// X<T>::\<Kind>\::KnownValue) to its instantiation
3503ef2b5b327b524f9ea3243c07e04fb24706e63120James Dennett/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
3504ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
35057c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
3506e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
3507815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
3508550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
35096d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
35107bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
35117bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
35122bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
35132bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
3514d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3515d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3516d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
3517a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
351857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
351957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
352057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
3521a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
35229a4db032ecd991626d236a502e770126db32bd31Richard Smith      int PackIdx = ArgumentPackSubstitutionIndex;
35239a4db032ecd991626d236a502e770126db32bd31Richard Smith      assert(PackIdx != -1 && "found declaration pack but not pack expanding");
352457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
352557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
352657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
352757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
352857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
352957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
3530a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
353157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
353257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
3533a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
353457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
353557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
35362bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
3537815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3538e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3539e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
3540e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
3541a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
35422c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
35432c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
3544e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
35452c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
35462c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
35472c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
35482c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
35492c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
35502c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35512c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
35522c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
35532c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
35542c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
35552c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
35562c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
35572c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
35582c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
35592c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35602c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
35612c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
35622c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
35632c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
35642c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
35652c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
35662c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
35672c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
35682c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
35692c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35702c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
35712c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
35722c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
3573e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
35742c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35752c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35762c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
35772c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
35782c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
35792c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
35802c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
35812c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
358252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
35832c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35842c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
358552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
35868b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
3587e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
3588e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
3589e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
359052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3591e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
3592e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
3593a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
35947c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
35951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
359644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
35971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3598815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
3599815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
3600815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
3601815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
36027c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
36033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
36043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
3605eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
36063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
36077c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
36087c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
36093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
36103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
3611eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
3612eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
36133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
36143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
36153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
3616a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
3617a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
36187c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
36197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
36207c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
3621815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
3622815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
362317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
36243bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
3625815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
3626815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
3627815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
3628815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
3629815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3630815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
3631815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
3632815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3633815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
36341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
363517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
363617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
3637815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
36381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3639eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
3640eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
3641eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
3642eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
3643eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
3644eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
3645eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
3646eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
3647a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
3648eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
3649eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
3650eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
3651eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
3652eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
3653eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
3654eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3655eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
36560724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith      } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
36570724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // This enumeration constant was found when the template was defined,
36580724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // but can't be found in the instantiation. This can happen if an
36590724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // unscoped enumeration member is explicitly specialized.
36600724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
36610724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
36620724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                                                             TemplateArgs));
36630724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        assert(Spec->getTemplateSpecializationKind() ==
36640724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                 TSK_ExplicitSpecialization);
36650724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Loc, diag::err_enumerator_does_not_exist)
36660724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << D->getDeclName()
36670724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
36680724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Spec->getLocation(), diag::note_enum_specialized_here)
36690724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(Spec);
3670eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
3671eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
3672eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
3673eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
3674eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
3675a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3676815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
3677815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
3678815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3679815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
3680815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3681d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
36821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
3683d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
36848155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
36856e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
36866e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
3687b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    SmallVector<PendingImplicitInstantiation, 4> Pending;
36886e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
36896e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
36906e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
36916e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
3692a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
369360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
369462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
369560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
369660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
369760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
369862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
369962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
370060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
370160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
370260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
370360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
37041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
37067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3707f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3708f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
370958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
371058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
371158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
371258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
37137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
37147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
37151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
37177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
37187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
3719c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
3720291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
3721291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
3722ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
3723291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
3724291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3725291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
3726291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
3727ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
3728291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
3729b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
3730291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
3731291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
373258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
373358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
373458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
373558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
3736ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
3737291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
3738291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
3739291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
3740291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3741f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3742f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
3743f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
37441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
374558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
374658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
374758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
374858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
3749d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
3750d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
37510c01d18094100db92d38daa923c95661512db203John McCall
37520c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
37530c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
37540c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
37550c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
37560c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
37570c01d18094100db92d38daa923c95661512db203John McCall
37580c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
37590c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
37600c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
37610c01d18094100db92d38daa923c95661512db203John McCall      break;
37620c01d18094100db92d38daa923c95661512db203John McCall    }
37630c01d18094100db92d38daa923c95661512db203John McCall  }
37640c01d18094100db92d38daa923c95661512db203John McCall}
3765