SemaTemplateInstantiateDecl.cpp revision 8387e2a41eef6fa17fb140a18c29b6eee9dd2b8a
16b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
26b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//
36b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//                     The LLVM Compiler Infrastructure
46b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//
56b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner// This file is distributed under the University of Illinois Open Source
66b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner// License. See LICENSE.TXT for details.
76b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//===----------------------------------------------------------------------===/
86b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//
96b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//  This file implements C++ template instantiation for declarations.
106b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//
116b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner//===----------------------------------------------------------------------===/
126b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner#include "clang/Sema/SemaInternal.h"
136b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner#include "clang/Sema/Lookup.h"
142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
1582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "clang/Sema/Template.h"
166b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner#include "clang/AST/ASTConsumer.h"
17384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/ASTContext.h"
18acc5f3e42334525bf28c86471551f83dfce222d5Daniel Dunbar#include "clang/AST/DeclTemplate.h"
19acc5f3e42334525bf28c86471551f83dfce222d5Daniel Dunbar#include "clang/AST/DeclVisitor.h"
20fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner#include "clang/AST/DependentDiagnostic.h"
2119510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/AST/Expr.h"
229c3087b0b0bea2fd782205c1274ebfc4290265e0John McCall#include "clang/AST/ExprCXX.h"
23797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner#include "clang/AST/TypeLoc.h"
246b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner#include "clang/Lex/Preprocessor.h"
259c3087b0b0bea2fd782205c1274ebfc4290265e0John McCall
266b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattnerusing namespace clang;
27883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
28883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                                              DeclaratorDecl *NewDecl) {
30883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  if (!OldDecl->getQualifierLoc())
31883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    return false;
32883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
33883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  NestedNameSpecifierLoc NewQualifierLoc
34883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
35883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                                          TemplateArgs);
36883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
37883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  if (!NewQualifierLoc)
38883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    return true;
39883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
40883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  NewDecl->setQualifierInfo(NewQualifierLoc);
41883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  return false;
42883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall}
43883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
44883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                                              TagDecl *NewDecl) {
46883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  if (!OldDecl->getQualifierLoc())
47e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner    return false;
48e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner
49e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner  NestedNameSpecifierLoc NewQualifierLoc
50e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner  = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
51a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek                                        TemplateArgs);
52a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek
536b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  if (!NewQualifierLoc)
54a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek    return true;
556b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
56a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek  NewDecl->setQualifierInfo(NewQualifierLoc);
576b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  return false;
58a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek}
596b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
606b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner// FIXME: Is this still too simple?
616b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattnervoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
62bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                            Decl *Tmpl, Decl *New) {
636b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek       i != e; ++i) {
65755f9d2c65f75d539a2440e5de82d881e4417397Fariborz Jahanian    const Attr *TmplAttr = *i;
666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    // FIXME: This should be generalized to more than just the AlignedAttr.
67d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar    if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
68183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      if (Aligned->isAlignmentDependent()) {
696b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        // The alignment expression is not potentially evaluated.
706b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        EnterExpressionEvaluationContext Unevaluated(*this,
713568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar                                                     Sema::Unevaluated);
723568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar
733568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar        if (Aligned->isAlignmentExpr()) {
74d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes          ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
75a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek                                        TemplateArgs);
76a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek          if (!Result.isInvalid())
77a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek            AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
78a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek        }
79a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek        else {
80a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek          TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
81d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar                                             TemplateArgs,
82d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar                                             Aligned->getLocation(),
83a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek                                             DeclarationName());
84a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek          if (Result)
85d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar            AddAlignedAttr(Aligned->getLocation(), New, Result);
863568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar        }
87620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian        continue;
88620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian      }
89620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian    }
90a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek
91620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian    // FIXME: Is cloning correct for all attributes?
92620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian    Attr *NewAttr = TmplAttr->clone(Context);
93620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian    New->addAttr(NewAttr);
94620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian  }
95620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian}
96620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian
97620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz JahanianDecl *
98d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz JahanianTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
99620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian  assert(false && "Translation units cannot be instantiated");
100620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian  return D;
101711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
102711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
103711c52bb20d0c69063b52a99826fb7d2835501f1John McCallDecl *
104711c52bb20d0c69063b52a99826fb7d2835501f1John McCallTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
105711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
106711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                      D->getIdentifier());
107711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Owner->addDecl(Inst);
108d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar  return Inst;
109d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar}
110620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian
111a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted KremenekDecl *
112620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz JahanianTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
11372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
114620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian  return D;
115d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz Jahanian}
116d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar
117d3f2c10f881311831a84114179342ff4db55e0c3Daniel DunbarDecl *
1183568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel DunbarTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1193568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar  NamespaceAliasDecl *Inst
120d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
121d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar                                 D->getNamespaceLoc(),
122d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar                                 D->getAliasLoc(),
123a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek                                 D->getIdentifier(),
12489951a86b594513c2a013532ed45d197413b1087Chris Lattner                                 D->getQualifierLoc(),
12572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor                                 D->getTargetNameLoc(),
126d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz Jahanian                                 D->getNamespace());
127d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz Jahanian  Owner->addDecl(Inst);
12889951a86b594513c2a013532ed45d197413b1087Chris Lattner  return Inst;
1293568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar}
1303568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar
131a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted KremenekDecl *TemplateDeclInstantiator::VisitTypedefNameDecl(TypedefNameDecl *D,
13289951a86b594513c2a013532ed45d197413b1087Chris Lattner                                                     bool IsTypeAlias) {
13372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  bool Invalid = false;
134d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz Jahanian  TypeSourceInfo *DI = D->getTypeSourceInfo();
135d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz Jahanian  if (DI->getType()->isDependentType() ||
136bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      DI->getType()->isVariablyModifiedType()) {
13789951a86b594513c2a013532ed45d197413b1087Chris Lattner    DI = SemaRef.SubstType(DI, TemplateArgs,
1383568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar                           D->getLocation(), D->getDeclName());
1393568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar    if (!DI) {
140a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek      Invalid = true;
1415b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1425b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    }
1435b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  } else {
1445b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1455b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  }
146a18d7d80eb914a48521f0b7b25057fb8a69c4652Ted Kremenek
147d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar  // Create the new typedef
14872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  TypedefNameDecl *Typedef;
1493568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar  if (IsTypeAlias)
150d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz Jahanian    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
151db9a0aec04cfd95830d3745b17b0bab5b87b16d1Ted Kremenek                                    D->getLocation(), D->getIdentifier(), DI);
152d66f22d9f8423579322a6dd16587ed52b0a58834Fariborz Jahanian  else
1533568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
1543568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar                                  D->getLocation(), D->getIdentifier(), DI);
1553568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar  if (Invalid)
1563568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar    Typedef->setInvalidDecl();
15707d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
15807d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  // If the old typedef was the name for linkage purposes of an anonymous
15907d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  // tag decl, re-establish that relationship for the new typedef.
16007d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
16107d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    TagDecl *oldTag = oldTagType->getDecl();
16207d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    if (oldTag->getTypedefNameForAnonDecl() == D) {
1636b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
164183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl());
165b77792eabf5882cf9af8cc810599b20432fda6c2Chris Lattner      newTag->setTypedefNameForAnonDecl(Typedef);
1666b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    }
167bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  }
168506b57e8b79d7dc2c367bf2ee7ec95420ad3fc8fJohn McCall
169506b57e8b79d7dc2c367bf2ee7ec95420ad3fc8fJohn McCall  if (TypedefNameDecl *Prev = D->getPreviousDeclaration()) {
1706b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
171bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                                       TemplateArgs);
172506b57e8b79d7dc2c367bf2ee7ec95420ad3fc8fJohn McCall    if (!InstPrev)
173bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      return 0;
1746b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1756b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    Typedef->setPreviousDeclaration(cast<TypedefNameDecl>(InstPrev));
1766b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  }
1776b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1786b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
179085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar
1806217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  Typedef->setAccess(D->getAccess());
181085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar  Owner->addDecl(Typedef);
182085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar
183085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar  return Typedef;
1846217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek}
185085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar
186085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel DunbarDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
187bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  return VisitTypedefNameDecl(D, /*IsTypeAlias=*/false);
188085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar}
189465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
190085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel DunbarDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
191085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar  return VisitTypedefNameDecl(D, /*IsTypeAlias=*/true);
192085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar}
193085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar
194085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar/// \brief Instantiate an initializer, breaking it into separate
195e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner/// initialization arguments.
196e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner///
197e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner/// \param S The semantic analysis object.
198e5c5ee1cff9ec084f176fa252774262677857ad2Chris Lattner///
1993068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar/// \param Init The initializer to instantiate.
2003068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar///
2013068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar/// \param TemplateArgs Template arguments to be substituted into the
2023068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar/// initializer.
203bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump///
2049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
205545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner///
206545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner/// \returns true if an error occurred, false otherwise
207803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattnerstatic bool InstantiateInitializer(Sema &S, Expr *Init,
208545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner                            const MultiLevelTemplateArgumentList &TemplateArgs,
2096b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                   SourceLocation &LParenLoc,
210bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                   ASTOwningVector<Expr*> &NewArgs,
2116b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                   SourceLocation &RParenLoc) {
2129cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  NewArgs.clear();
2139cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  LParenLoc = SourceLocation();
2149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  RParenLoc = SourceLocation();
2159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
2169cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (!Init)
217f7a1a744eba4b29ceb0f20af8f34515d892fdd64John McCall    return false;
218f7a1a744eba4b29ceb0f20af8f34515d892fdd64John McCall
219f7a1a744eba4b29ceb0f20af8f34515d892fdd64John McCall  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
220f7a1a744eba4b29ceb0f20af8f34515d892fdd64John McCall    Init = ExprTemp->getSubExpr();
2219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
2229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Init = Binder->getSubExpr();
2249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
2259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    Init = ICE->getSubExprAsWritten();
2277a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne
2286b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2299cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    LParenLoc = ParenList->getLParenLoc();
2309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    RParenLoc = ParenList->getRParenLoc();
2319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
2329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                        true, TemplateArgs, NewArgs);
2339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
234ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
235a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
236bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
2379cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
2389cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                       TemplateArgs, NewArgs))
2396b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        return true;
2406b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
2416b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      // FIXME: Fake locations!
242803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
2436b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      RParenLoc = LParenLoc;
244545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner      return false;
2453c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    }
2466b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  }
2476b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
248bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  ExprResult Result = S.SubstExpr(Init, TemplateArgs);
2496b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  if (Result.isInvalid())
250cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    return true;
2516b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
2526b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  NewArgs.push_back(Result.takeAs<Expr>());
2536b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  return false;
2546b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner}
255803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner
256fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris LattnerDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
25708631c5fa053867146b5ee8be658c229f6bf127cChris Lattner  // If this is the variable for an anonymous struct or union,
2586b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // instantiate the anonymous struct/union type first.
259cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2606b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2613c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2626b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        return 0;
2636b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
26463e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  // Do substitution on the type of the declaration
26596329d4e07a9bcddb5a927892b70408c8fd8c474Ted Kremenek  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
26696329d4e07a9bcddb5a927892b70408c8fd8c474Ted Kremenek                                         TemplateArgs,
2673c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner                                         D->getTypeSpecStartLoc(),
26896329d4e07a9bcddb5a927892b70408c8fd8c474Ted Kremenek                                         D->getDeclName());
26996329d4e07a9bcddb5a927892b70408c8fd8c474Ted Kremenek  if (!DI)
270bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    return 0;
27163e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek
27263e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  if (DI->getType()->isFunctionType()) {
27363e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
274cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      << D->isStaticDataMember() << DI->getType();
27563e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek    return 0;
27663e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  }
27763e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek
2784ee2bb12dcb8f8b543a3581537a4bc5752106ce2Ted Kremenek  // Build the instantiated declaration
27963e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
28063e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek                                 D->getInnerLocStart(),
28163e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek                                 D->getLocation(), D->getIdentifier(),
28263e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek                                 DI->getType(), DI,
28363e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek                                 D->getStorageClass(),
28463e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek                                 D->getStorageClassAsWritten());
28563e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  Var->setThreadSpecified(D->isThreadSpecified());
28663e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
28763e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
28863e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek
289efbddd23173ea5633cc8a004f1014c68c3ac6593Ted Kremenek  // Substitute the nested name specifier, if any.
290efbddd23173ea5633cc8a004f1014c68c3ac6593Ted Kremenek  if (SubstQualifier(D, Var))
291cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    return 0;
29263e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek
293efbddd23173ea5633cc8a004f1014c68c3ac6593Ted Kremenek  // If we are instantiating a static data member defined
29463e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek  // out-of-line, the instantiation will have the same lexical
2954ee2bb12dcb8f8b543a3581537a4bc5752106ce2Ted Kremenek  // context (which will be a namespace scope) as the template.
29696329d4e07a9bcddb5a927892b70408c8fd8c474Ted Kremenek  if (D->isOutOfLine())
29796329d4e07a9bcddb5a927892b70408c8fd8c474Ted Kremenek    Var->setLexicalDeclContext(D->getLexicalDeclContext());
298857e918a8a40deb128840308a318bf623d68295fTed Kremenek
299857e918a8a40deb128840308a318bf623d68295fTed Kremenek  Var->setAccess(D->getAccess());
300857e918a8a40deb128840308a318bf623d68295fTed Kremenek
301857e918a8a40deb128840308a318bf623d68295fTed Kremenek  if (!D->isStaticDataMember()) {
302a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian    Var->setUsed(D->isUsed(false));
303857e918a8a40deb128840308a318bf623d68295fTed Kremenek    Var->setReferenced(D->isReferenced());
304857e918a8a40deb128840308a318bf623d68295fTed Kremenek  }
305857e918a8a40deb128840308a318bf623d68295fTed Kremenek
306857e918a8a40deb128840308a318bf623d68295fTed Kremenek  // FIXME: In theory, we could have a previous declaration for variables that
307857e918a8a40deb128840308a318bf623d68295fTed Kremenek  // are not static data members.
308857e918a8a40deb128840308a318bf623d68295fTed Kremenek  bool Redeclaration = false;
309857e918a8a40deb128840308a318bf623d68295fTed Kremenek  // FIXME: having to fake up a LookupResult is dumb.
3104ee2bb12dcb8f8b543a3581537a4bc5752106ce2Ted Kremenek  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
311857e918a8a40deb128840308a318bf623d68295fTed Kremenek                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
312857e918a8a40deb128840308a318bf623d68295fTed Kremenek  if (D->isStaticDataMember())
3133a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian    SemaRef.LookupQualifiedName(Previous, Owner, false);
3143a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3153a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian
3163a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian  if (D->isOutOfLine()) {
3173a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian    if (!D->isStaticDataMember())
3183a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian      D->getLexicalDeclContext()->addDecl(Var);
3193a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian    Owner->makeDeclVisibleInContext(Var);
3203a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian  } else {
3213a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian    Owner->addDecl(Var);
3223a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian    if (Owner->isFunctionOrMethod())
3233a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3243a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian  }
3253a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
326a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian
327a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian  // Link instantiations of static data members back to the template from
328a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian  // which they were instantiated.
3293a3400b4fdf73887e9d8b4372334bc24a858702fFariborz Jahanian  if (Var->isStaticDataMember())
330b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
331a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian                                                     TSK_ImplicitInstantiation);
332a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian
333a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian  if (Var->getAnyInitializer()) {
334a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian    // We already have an initializer in the class.
335a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian  } else if (D->getInit()) {
336b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    if (Var->isStaticDataMember() && !D->isOutOfLine())
337a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
338a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian    else
339a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
340a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian
341a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian    // Instantiate the initializer.
342a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian    SourceLocation LParenLoc, RParenLoc;
343a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian    ASTOwningVector<Expr*> InitArgs(SemaRef);
344a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
345a8fb24fa3151567056f6125999cea69e39604f35Fariborz Jahanian                                InitArgs, RParenLoc)) {
346cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      bool TypeMayContainAuto = true;
347cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      // Attach the initializer to the declaration, if we have one.
348857e918a8a40deb128840308a318bf623d68295fTed Kremenek      if (InitArgs.size() == 0)
349857e918a8a40deb128840308a318bf623d68295fTed Kremenek        SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
350eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      else if (D->hasCXXDirectInitializer()) {
351bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump        // Add the direct initializer to the declaration.
352bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump        SemaRef.AddCXXDirectInitializerToDecl(Var,
353d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar                                              LParenLoc,
354fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner                                              move_arg(InitArgs),
355883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                                              RParenLoc,
356eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek                                              TypeMayContainAuto);
357eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      } else {
358bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump        assert(InitArgs.size() == 1);
35907d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth        Expr *Init = InitArgs.take()[0];
36007d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth        SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
36107d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth      }
36207d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    } else {
363eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      // FIXME: Not too happy about invalidating the declaration
364eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      // because of a bogus initializer.
365eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      Var->setInvalidDecl();
366bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    }
367eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek
368eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek    SemaRef.PopExpressionEvaluationContext();
369bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
370bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump             !Var->isCXXForRangeDecl())
371eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek    SemaRef.ActOnUninitializedDecl(Var, false);
3727a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne
373eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek  // Diagnose unused local variables.
374ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
375ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
376fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
377fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  return Var;
378eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek}
379eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek
380bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike StumpDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
381eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek  AccessSpecDecl* AD
382bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
383eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek                             D->getAccessSpecifierLoc(), D->getColonLoc());
384fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  Owner->addHiddenDecl(AD);
38530bc96544346bea42921cf6837e66cef80d664b4Chris Lattner  return AD;
386eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek}
387eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek
388bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike StumpDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
389465172f304248a9aab6f2c398a836ce4e25efbbfTed Kremenek  bool Invalid = false;
39007d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  TypeSourceInfo *DI = D->getTypeSourceInfo();
39107d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  if (DI->getType()->isDependentType() ||
39207d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth      DI->getType()->isVariablyModifiedType())  {
39307d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    DI = SemaRef.SubstType(DI, TemplateArgs,
39407d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                           D->getLocation(), D->getDeclName());
39507d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    if (!DI) {
39607d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth      DI = D->getTypeSourceInfo();
39707d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth      Invalid = true;
39807d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    } else if (DI->getType()->isFunctionType()) {
399eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      // C++ [temp.arg.type]p3:
400eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      //   If a declaration acquires a function type through a type
401de43632a5951abf3f357e2f79dcddda4dc6ec8ffDouglas Gregor      //   dependent on a template-parameter and this causes a
402dbfe99ef39163fd3574332673ee175c2bb6ef3caTed Kremenek      //   declaration that does not use the syntactic form of a
403eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      //   function declarator to have function type, the program is
404c9ef405559c90fc98b016d00aeae8afbc31c6bf6Douglas Gregor      //   ill-formed.
405fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
4067fb43c17eb2b4102f40a80a355629aacd70589adTed Kremenek        << DI->getType();
407eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek      Invalid = true;
408bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    }
409eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek  } else {
410eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
411bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  }
412bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
413bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  Expr *BitWidth = D->getBitWidth();
4147fb43c17eb2b4102f40a80a355629aacd70589adTed Kremenek  if (Invalid)
41546bbacac37141ed9d01d5b6473e8211554b02710Ted Kremenek    BitWidth = 0;
416de43632a5951abf3f357e2f79dcddda4dc6ec8ffDouglas Gregor  else if (BitWidth) {
417dbfe99ef39163fd3574332673ee175c2bb6ef3caTed Kremenek    // The bit-width expression is not potentially evaluated.
418d3f2c10f881311831a84114179342ff4db55e0c3Daniel Dunbar    EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
419ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian
420ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian    ExprResult InstantiatedBitWidth
421ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
422ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian    if (InstantiatedBitWidth.isInvalid()) {
423ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      Invalid = true;
424ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      BitWidth = 0;
425ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian    } else
426ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
427ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian  }
428ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian
429ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
430ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian                                            DI->getType(), DI,
431ff3a078d2a67db9ae6ff4cc0f799a209f85a4e91Fariborz Jahanian                                            cast<RecordDecl>(Owner),
43246bbacac37141ed9d01d5b6473e8211554b02710Ted Kremenek                                            D->getLocation(),
433bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                            D->isMutable(),
434ee1c08c88649aaea9dd53272a726cd23de533215Ted Kremenek                                            BitWidth,
43560acea49c1343e5494edb6da20cac6f9d0b6cfb0Fariborz Jahanian                                            D->getTypeSpecStartLoc(),
43660acea49c1343e5494edb6da20cac6f9d0b6cfb0Fariborz Jahanian                                            D->getAccess(),
43760acea49c1343e5494edb6da20cac6f9d0b6cfb0Fariborz Jahanian                                            0);
43860acea49c1343e5494edb6da20cac6f9d0b6cfb0Fariborz Jahanian  if (!Field) {
43960acea49c1343e5494edb6da20cac6f9d0b6cfb0Fariborz Jahanian    cast<Decl>(Owner)->setInvalidDecl();
4407fb43c17eb2b4102f40a80a355629aacd70589adTed Kremenek    return 0;
44160acea49c1343e5494edb6da20cac6f9d0b6cfb0Fariborz Jahanian  }
442eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek
4437fb43c17eb2b4102f40a80a355629aacd70589adTed Kremenek  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
4447fb43c17eb2b4102f40a80a355629aacd70589adTed Kremenek
4457fb43c17eb2b4102f40a80a355629aacd70589adTed Kremenek  if (Invalid)
446dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    Field->setInvalidDecl();
447cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
448cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  if (!Field->getDeclName()) {
449eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek    // Keep track of where this decl came from.
450eb2b2a3f1898f146c6e153a64ec58ec5e1750bd2Ted Kremenek    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
451dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  }
452dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
453dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    if (Parent->isAnonymousStructOrUnion() &&
454dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek        Parent->getRedeclContext()->isFunctionOrMethod())
455dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
456dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  }
457dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
4582a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose  Field->setImplicit(D->isImplicit());
4592a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose  Field->setAccess(D->getAccess());
460dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  Owner->addDecl(Field);
461dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
462dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  return Field;
463dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek}
464dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
465dd0e490c24aeade2c59ca4cae171199f6af9f02eTed KremenekDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
466dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  NamedDecl **NamedChain =
467cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
4682a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose
4692a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose  int i = 0;
470cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (IndirectFieldDecl::chain_iterator PI =
471dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek       D->chain_begin(), PE = D->chain_end();
472dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek       PI != PE; ++PI) {
473dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
474dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                              TemplateArgs);
4752a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose    if (!Next)
4762a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose      return 0;
477cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
478dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    NamedChain[i++] = Next;
479dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  }
480dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
481dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
4822a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose  IndirectFieldDecl* IndirectField
4832a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
484cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                D->getIdentifier(), T,
485dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                NamedChain, D->getChainingSize());
486dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
487dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
488dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  IndirectField->setImplicit(D->isImplicit());
489dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  IndirectField->setAccess(D->getAccess());
4902a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose  Owner->addDecl(IndirectField);
4912a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose  return IndirectField;
4922a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose}
4932a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose
494dd0e490c24aeade2c59ca4cae171199f6af9f02eTed KremenekDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
495dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  // Handle friend type expressions by simply substituting template
496dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  // parameters into the pattern type and checking the result.
497883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
498883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    TypeSourceInfo *InstTy =
499dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      SemaRef.SubstType(Ty, TemplateArgs,
500dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                        D->getLocation(), DeclarationName());
501dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    if (!InstTy)
50207d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth      return 0;
50307d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
50407d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
50507d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    if (!FD)
506dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      return 0;
507dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
508dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    FD->setAccess(AS_public);
509dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    FD->setUnsupportedFriend(D->isUnsupportedFriend());
510dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    Owner->addDecl(FD);
511dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    return FD;
512dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  }
513dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
514dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  NamedDecl *ND = D->getFriendDecl();
5152a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose  assert(ND && "friend decl must be a decl or a type!");
5162a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose
517dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  // All of the Visit implementations for the various potential friend
5187a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne  // declarations have to be carefully written to work for friend
519dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  // objects, with the most important detail being that the target
520dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  // decl should almost certainly not be placed in Owner.
521dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  Decl *NewND = Visit(ND);
522dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  if (!NewND) return 0;
523dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
524dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  FriendDecl *FD =
525dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
526dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                       cast<NamedDecl>(NewND), D->getFriendLoc());
527dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  FD->setAccess(AS_public);
528dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  FD->setUnsupportedFriend(D->isUnsupportedFriend());
529dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  Owner->addDecl(FD);
530dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  return FD;
531dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek}
532dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
533dd0e490c24aeade2c59ca4cae171199f6af9f02eTed KremenekDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
534dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  Expr *AssertExpr = D->getAssertExpr();
53507d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
53607d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  // The expression in a static assertion is not potentially evaluated.
53707d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
53807d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
53907d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  ExprResult InstantiatedAssertExpr
54007d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
54107d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  if (InstantiatedAssertExpr.isInvalid())
54207d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    return 0;
54307d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
544dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  ExprResult Message(D->getMessage());
545cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  D->getMessage();
546cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
547dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                              InstantiatedAssertExpr.get(),
548dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                              Message.get(),
549dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                              D->getRParenLoc());
550dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek}
551dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
552cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean HuntDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
553dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
554dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                    D->getLocation(), D->getIdentifier(),
555dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                    /*PrevDecl=*/0, D->isScoped(),
556dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                    D->isScopedUsingClassTag(), D->isFixed());
557dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  if (D->isFixed()) {
558dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
559cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      // If we have type source information for the underlying type, it means it
560dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      // has been explicitly set by the user. Perform substitution on it before
561dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      // moving on.
5627a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
563dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
564dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                                       TemplateArgs,
565dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                                       UnderlyingLoc,
566dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek                                                       DeclarationName()));
567dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
568dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      if (!Enum->getIntegerTypeSourceInfo())
569dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek        Enum->setIntegerType(SemaRef.Context.IntTy);
570dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    }
571dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    else {
572dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      assert(!D->getIntegerType()->isDependentType()
573dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek             && "Dependent type without type source info");
5742a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose      Enum->setIntegerType(D->getIntegerType());
5752a479929f70d32f626778ef6e70ef46d3a37f74eJordy Rose    }
576dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  }
577dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
578dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
579cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
580cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  Enum->setInstantiationOfMemberEnum(D);
581cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  Enum->setAccess(D->getAccess());
582cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  if (SubstQualifier(D, Enum)) return 0;
583cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  Owner->addDecl(Enum);
584cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  Enum->startDefinition();
585cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
586cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  if (D->getDeclContext()->isFunctionOrMethod())
587cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
588cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
589dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  llvm::SmallVector<Decl*, 4> Enumerators;
590dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek
591dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  EnumConstantDecl *LastEnumConst = 0;
592dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
593dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek         ECEnd = D->enumerator_end();
594dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek       EC != ECEnd; ++EC) {
595dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    // The specified value for the enumerator.
596dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    ExprResult Value = SemaRef.Owned((Expr *)0);
597dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    if (Expr *UninstValue = EC->getInitExpr()) {
598dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      // The enumerator's value expression is not potentially evaluated.
599cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      EnterExpressionEvaluationContext Unevaluated(SemaRef,
600cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                                   Sema::Unevaluated);
601cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
602cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
603dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    }
604cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
605cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    // Drop the initial value and continue.
606cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    bool isInvalid = false;
607dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    if (Value.isInvalid()) {
608dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      Value = SemaRef.Owned((Expr *)0);
609332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall      isInvalid = true;
610332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    }
611332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
612332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    EnumConstantDecl *EnumConst
613332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
614332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall                                  EC->getLocation(), EC->getIdentifier(),
615332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall                                  Value.get());
616332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
617332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    if (isInvalid) {
618332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall      if (EnumConst)
619332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall        EnumConst->setInvalidDecl();
620332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall      Enum->setInvalidDecl();
621332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    }
622332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
623332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    if (EnumConst) {
624332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
625332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
62611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      EnumConst->setAccess(Enum->getAccess());
62711e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      Enum->addDecl(EnumConst);
62811e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      Enumerators.push_back(EnumConst);
62911e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      LastEnumConst = EnumConst;
63011e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
63111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      if (D->getDeclContext()->isFunctionOrMethod()) {
63211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola        // If the enumeration is within a function or method, record the enum
63311e8ce7380856abee188b237c2600272df2ed09dRafael Espindola        // constant as a local.
63411e8ce7380856abee188b237c2600272df2ed09dRafael Espindola        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
63511e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      }
636332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    }
637332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  }
638883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
639332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  // FIXME: Fixup LBraceLoc and RBraceLoc
640332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
641332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
642332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall                        Enum,
643332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall                        Enumerators.data(), Enumerators.size(),
64411e8ce7380856abee188b237c2600272df2ed09dRafael Espindola                        0, 0);
64511e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
64611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  return Enum;
64711e8ce7380856abee188b237c2600272df2ed09dRafael Espindola}
64811e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
64911e8ce7380856abee188b237c2600272df2ed09dRafael EspindolaDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
65011e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
65111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  return 0;
65211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola}
65311e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
6547a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian RedlDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
6557a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
6567a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl
657332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  // Create a local instantiation scope for this class template, which
6587a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  // will contain the instantiations of the template parameters.
65911e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  LocalInstantiationScope Scope(SemaRef);
66011e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  TemplateParameterList *TempParams = D->getTemplateParameters();
66111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
66211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  if (!InstParams)
66311e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    return NULL;
66411e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
66511e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  CXXRecordDecl *Pattern = D->getTemplatedDecl();
66611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
66711e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  // Instantiate the qualifier.  We have to do this first in case
66811e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  // we're a friend declaration, because if we are then we need to put
66911e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  // the new declaration in the appropriate context.
67011e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
67111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  if (QualifierLoc) {
67211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
67311e8ce7380856abee188b237c2600272df2ed09dRafael Espindola                                                       TemplateArgs);
67411e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    if (!QualifierLoc)
67511e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      return 0;
67611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  }
67711e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
67811e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  CXXRecordDecl *PrevDecl = 0;
679332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
680332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
68111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  if (!isFriend && Pattern->getPreviousDeclaration()) {
68211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
68311e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    if (Found.first != Found.second) {
68411e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
68511e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      if (PrevClassTemplate)
68611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola        PrevDecl = PrevClassTemplate->getTemplatedDecl();
68711e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    }
68811e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  }
6897a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne
69011e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  // If this isn't a friend, then it's a member template, in which
69111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  // case we just want to build the instantiation in the
69211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  // specialization.  If it is a friend, we want to build it in
69311e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  // the appropriate context.
69411e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  DeclContext *DC = Owner;
69511e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  if (isFriend) {
69611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    if (QualifierLoc) {
69711e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      CXXScopeSpec SS;
69811e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      SS.Adopt(QualifierLoc);
69911e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      DC = SemaRef.computeDeclContext(SS);
700f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher      if (!DC) return 0;
701f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    } else {
70211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
70311e8ce7380856abee188b237c2600272df2ed09dRafael Espindola                                           Pattern->getDeclContext(),
704cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                           TemplateArgs);
70511e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    }
70611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
707803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner    // Look for a previous declaration of the template in the owning
7086b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    // context.
709545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
7103c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
7116b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    SemaRef.LookupQualifiedName(R, DC);
7126b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
713bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    if (R.isSingleResult()) {
7147a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
7156b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      if (PrevClassTemplate)
7166b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        PrevDecl = PrevClassTemplate->getTemplatedDecl();
717bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    }
7186b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
719fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner    if (!PrevClassTemplate && QualifierLoc) {
7203c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7216b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
7226b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        << QualifierLoc.getSourceRange();
723bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      return 0;
724f5fe2925b87cf382f2f13983c81679e38067122bRafael Espindola    }
725f5fe2925b87cf382f2f13983c81679e38067122bRafael Espindola
726f5fe2925b87cf382f2f13983c81679e38067122bRafael Espindola    bool AdoptedPreviousTemplateParams = false;
727f5fe2925b87cf382f2f13983c81679e38067122bRafael Espindola    if (PrevClassTemplate) {
728f5fe2925b87cf382f2f13983c81679e38067122bRafael Espindola      bool Complain = true;
7296b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
730bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
731f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher      // template for struct std::tr1::__detail::_Map_base, where the
732f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher      // template parameters of the friend declaration don't match the
7336b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      // template parameters of the original declaration. In this one
7346b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      // case, we don't complain about the ill-formed friend
735dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar      // declaration.
736dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar      if (isFriend && Pattern->getIdentifier() &&
737dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar          Pattern->getIdentifier()->isStr("_Map_base") &&
738dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar          DC->isNamespace() &&
739dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar          cast<NamespaceDecl>(DC)->getIdentifier() &&
740dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
741dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar        DeclContext *DCParent = DC->getParent();
742dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar        if (DCParent->isNamespace() &&
743dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
744dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
745883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall          DeclContext *DCParent2 = DCParent->getParent();
746dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar          if (DCParent2->isNamespace() &&
747dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
748dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
749dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar              DCParent2->getParent()->isTranslationUnit())
750dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar            Complain = false;
751dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar        }
752bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      }
753af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar
754dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar      TemplateParameterList *PrevParams
755af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar        = PrevClassTemplate->getTemplateParameters();
7563c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
757af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar      // Make sure the parameter lists match.
758af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
7595bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson                                                  Complain,
760c51974328b3a378c3c40b1fa527ecb928ed2bfdaChris Lattner                                                  Sema::TPL_TemplateMatch)) {
7615bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson        if (Complain)
762883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall          return 0;
7635bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson
7645bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson        AdoptedPreviousTemplateParams = true;
765bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump        InstParams = PrevParams;
766cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      }
767af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar
768af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar      // Do some additional validation, then merge default arguments
76976168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn      // from the existing declarations.
770dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar      if (!AdoptedPreviousTemplateParams &&
77176168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
77276168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn                                             Sema::TPC_ClassTemplate))
77376168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn        return 0;
77476168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn    }
7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7762cff7d16fe58e6d6447ec9cad2af083beb20d6b5Ted Kremenek
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXRecordDecl *RecordInst
7782cff7d16fe58e6d6447ec9cad2af083beb20d6b5Ted Kremenek    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
779cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                            Pattern->getLocStart(), Pattern->getLocation(),
7802cff7d16fe58e6d6447ec9cad2af083beb20d6b5Ted Kremenek                            Pattern->getIdentifier(), PrevDecl,
7812cff7d16fe58e6d6447ec9cad2af083beb20d6b5Ted Kremenek                            /*DelayTypeCreation=*/true);
782fd6ad3cf9c8fc6904bd5f33212207aa69743fd45Ryan Flynn
783fd6ad3cf9c8fc6904bd5f33212207aa69743fd45Ryan Flynn  if (QualifierLoc)
7842cff7d16fe58e6d6447ec9cad2af083beb20d6b5Ted Kremenek    RecordInst->setQualifierInfo(QualifierLoc);
78576168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn
78676168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn  ClassTemplateDecl *Inst
78734c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
78834c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman                                D->getIdentifier(), InstParams, RecordInst,
78934c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman                                PrevClassTemplate);
79034c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman  RecordInst->setDescribedClassTemplate(Inst);
79134c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman
79234c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman  if (isFriend) {
79334c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman    if (PrevClassTemplate)
79434c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman      Inst->setAccess(PrevClassTemplate->getAccess());
79534c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman    else
79634c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman      Inst->setAccess(D->getAccess());
797a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher
798a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
799722109c1b7718d3e8aab075ce65007b372822199Eric Christopher    // TODO: do we want to track the instantiation progeny of this
800722109c1b7718d3e8aab075ce65007b372822199Eric Christopher    // friend target decl?
801722109c1b7718d3e8aab075ce65007b372822199Eric Christopher  } else {
802722109c1b7718d3e8aab075ce65007b372822199Eric Christopher    Inst->setAccess(D->getAccess());
803883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    if (!PrevClassTemplate)
804a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher      Inst->setInstantiatedFromMemberTemplate(D);
805a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher  }
806a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher
807a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher  // Trigger creation of the type for the instantiation.
808722109c1b7718d3e8aab075ce65007b372822199Eric Christopher  SemaRef.Context.getInjectedClassNameType(RecordInst,
809722109c1b7718d3e8aab075ce65007b372822199Eric Christopher                                    Inst->getInjectedClassNameSpecialization());
810722109c1b7718d3e8aab075ce65007b372822199Eric Christopher
811722109c1b7718d3e8aab075ce65007b372822199Eric Christopher  // Finish handling of friends.
812883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  if (isFriend) {
813a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
814a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher    return Inst;
815711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
816711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
817711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Owner->addDecl(Inst);
818711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
819711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!PrevClassTemplate) {
820711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Queue up any out-of-line partial specializations of this member
821711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // class template; the client will force their instantiation once
822883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    // the enclosing class has been instantiated.
823711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
824711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    D->getPartialSpecializations(PartialSpecs);
825711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
826711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      if (PartialSpecs[I]->isOutOfLine())
827711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
828711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
829711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
830711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  return Inst;
831711c52bb20d0c69063b52a99826fb7d2835501f1John McCall}
832711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
833711c52bb20d0c69063b52a99826fb7d2835501f1John McCallDecl *
834711c52bb20d0c69063b52a99826fb7d2835501f1John McCallTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
835711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                   ClassTemplatePartialSpecializationDecl *D) {
836711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
837b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek
838b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek  // Lookup the already-instantiated declaration in the instantiation
839b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek  // of the class template and return that.
840b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek  DeclContext::lookup_result Found
841b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek    = Owner->lookup(ClassTemplate->getDeclName());
842b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek  if (Found.first == Found.second)
843b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek    return 0;
844b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek
845545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  ClassTemplateDecl *InstClassTemplate
846e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    = dyn_cast<ClassTemplateDecl>(*Found.first);
847b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek  if (!InstClassTemplate)
8486b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    return 0;
849b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek
85019c30c00e5e01e4608a43c7deb504f343f09e46dMike Stump  if (ClassTemplatePartialSpecializationDecl *Result
85119c30c00e5e01e4608a43c7deb504f343f09e46dMike Stump        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
8523ee77640c722a70ab7e0181f36dc2af21cab3d23Mike Stump    return Result;
8533ee77640c722a70ab7e0181f36dc2af21cab3d23Mike Stump
854e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
855e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara}
856b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek
857883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCallDecl *
858b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted KremenekTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
85919c30c00e5e01e4608a43c7deb504f343f09e46dMike Stump  // Create a local instantiation scope for this function template, which
8606b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // will contain the instantiations of the template parameters and then get
861b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek  // merged with the local instantiation scope for the function template
862b56c1cc8ca593f832ca58d682876259c2ed9bec2Ted Kremenek  // itself.
8636b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  LocalInstantiationScope Scope(SemaRef);
8646b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
86535cc9627340b15232139b3c43fcde5973e7fad30John Thompson  TemplateParameterList *TempParams = D->getTemplateParameters();
86635cc9627340b15232139b3c43fcde5973e7fad30John Thompson  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
86735cc9627340b15232139b3c43fcde5973e7fad30John Thompson  if (!InstParams)
86835cc9627340b15232139b3c43fcde5973e7fad30John Thompson    return NULL;
86935cc9627340b15232139b3c43fcde5973e7fad30John Thompson
87035cc9627340b15232139b3c43fcde5973e7fad30John Thompson  FunctionDecl *Instantiated = 0;
871f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
872f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
873f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher                                                                 InstParams));
874f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher  else
875f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
87635cc9627340b15232139b3c43fcde5973e7fad30John Thompson                                                          D->getTemplatedDecl(),
87735cc9627340b15232139b3c43fcde5973e7fad30John Thompson                                                                InstParams));
87835cc9627340b15232139b3c43fcde5973e7fad30John Thompson
87935cc9627340b15232139b3c43fcde5973e7fad30John Thompson  if (!Instantiated)
88035cc9627340b15232139b3c43fcde5973e7fad30John Thompson    return 0;
88135cc9627340b15232139b3c43fcde5973e7fad30John Thompson
88235cc9627340b15232139b3c43fcde5973e7fad30John Thompson  Instantiated->setAccess(D->getAccess());
88335cc9627340b15232139b3c43fcde5973e7fad30John Thompson
88435cc9627340b15232139b3c43fcde5973e7fad30John Thompson  // Link the instantiated function template declaration to the function
88535cc9627340b15232139b3c43fcde5973e7fad30John Thompson  // template from which it was instantiated.
88635cc9627340b15232139b3c43fcde5973e7fad30John Thompson  FunctionTemplateDecl *InstTemplate
88735cc9627340b15232139b3c43fcde5973e7fad30John Thompson    = Instantiated->getDescribedFunctionTemplate();
88835cc9627340b15232139b3c43fcde5973e7fad30John Thompson  InstTemplate->setAccess(D->getAccess());
88935cc9627340b15232139b3c43fcde5973e7fad30John Thompson  assert(InstTemplate &&
89035cc9627340b15232139b3c43fcde5973e7fad30John Thompson         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
89101add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson
89235cc9627340b15232139b3c43fcde5973e7fad30John Thompson  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
893883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
89435cc9627340b15232139b3c43fcde5973e7fad30John Thompson  // Link the instantiation back to the pattern *unless* this is a
89535cc9627340b15232139b3c43fcde5973e7fad30John Thompson  // non-definition friend declaration.
89635cc9627340b15232139b3c43fcde5973e7fad30John Thompson  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
89735cc9627340b15232139b3c43fcde5973e7fad30John Thompson      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
89835cc9627340b15232139b3c43fcde5973e7fad30John Thompson    InstTemplate->setInstantiatedFromMemberTemplate(D);
89935cc9627340b15232139b3c43fcde5973e7fad30John Thompson
90035cc9627340b15232139b3c43fcde5973e7fad30John Thompson  // Make declarations visible in the appropriate context.
90135cc9627340b15232139b3c43fcde5973e7fad30John Thompson  if (!isFriend)
90201add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson    Owner->addDecl(InstTemplate);
90301add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson
90401add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson  return InstTemplate;
90501add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson}
90601add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson
90701add59bc8fd178960ad61169bc01566b0d6614cJohn ThompsonDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
90801add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson  CXXRecordDecl *PrevDecl = 0;
90901add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson  if (D->isInjectedClassName())
91001add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson    PrevDecl = cast<CXXRecordDecl>(Owner);
91101add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson  else if (D->getPreviousDeclaration()) {
91201add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
91301add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson                                                   D->getPreviousDeclaration(),
91401add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson                                                   TemplateArgs);
915f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    if (!Prev) return 0;
916f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    PrevDecl = cast<CXXRecordDecl>(Prev);
91701add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson  }
91801add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson
91901add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson  CXXRecordDecl *Record
92001add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
92101add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson                            D->getLocStart(), D->getLocation(),
92201add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson                            D->getIdentifier(), PrevDecl);
92301add59bc8fd178960ad61169bc01566b0d6614cJohn Thompson
924cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  // Substitute the nested name specifier, if any.
92535cc9627340b15232139b3c43fcde5973e7fad30John Thompson  if (SubstQualifier(D, Record))
92635cc9627340b15232139b3c43fcde5973e7fad30John Thompson    return 0;
927bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt
928bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  Record->setImplicit(D->isImplicit());
929bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  // FIXME: Check against AS_none is an ugly hack to work around the issue that
930883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  // the tag decls introduced by friend class declarations don't have an access
931bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  // specifier. Remove once this area of the code gets sorted out.
932bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  if (D->getAccess() != AS_none)
933bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    Record->setAccess(D->getAccess());
934bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  if (!D->isInjectedClassName())
935bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
93673798892751e378cbcdef43579c1d41685091fd0Ted Kremenek
93773798892751e378cbcdef43579c1d41685091fd0Ted Kremenek  // If the original function was part of a friend declaration,
93873798892751e378cbcdef43579c1d41685091fd0Ted Kremenek  // inherit its namespace state.
9393c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
94073798892751e378cbcdef43579c1d41685091fd0Ted Kremenek    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
94173798892751e378cbcdef43579c1d41685091fd0Ted Kremenek
942bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  // Make sure that anonymous structs and unions are recorded.
943aec586056d8670c99ba7c4833be13e4eb123cddbJohn McCall  if (D->isAnonymousStructOrUnion()) {
94457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Record->setAnonymousStructOrUnion(true);
945fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
946883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
94773798892751e378cbcdef43579c1d41685091fd0Ted Kremenek  }
94873798892751e378cbcdef43579c1d41685091fd0Ted Kremenek
949bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  Owner->addDecl(Record);
950cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  return Record;
95173798892751e378cbcdef43579c1d41685091fd0Ted Kremenek}
95273798892751e378cbcdef43579c1d41685091fd0Ted Kremenek
953b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar/// Normal class members are of more specific types and therefore
954b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar/// don't make it here.  This function serves two purposes:
955b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar///   1) instantiating function templates
956b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar///   2) substituting friend declarations
957b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar/// FIXME: preserve function definitions in case #2
958b805dad4aa386aeae0f72512895bd238678d37a5Daniel DunbarDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
959bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                       TemplateParameterList *TemplateParams) {
960b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar  // Check whether there is already a function template specialization for
961186204bfcf9c53d48143ec300d4c3d036fed4140Daniel Dunbar  // this declaration.
962b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
963b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar  void *InsertPos = 0;
964b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar  if (FunctionTemplate && !TemplateParams) {
965b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar    std::pair<const TemplateArgument *, unsigned> Innermost
966b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar      = TemplateArgs.getInnermost();
967883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
968b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar    FunctionDecl *SpecFunc
969b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
970bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                             InsertPos);
971cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
972b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar    // If we already have a function template specialization, return it.
973b805dad4aa386aeae0f72512895bd238678d37a5Daniel Dunbar    if (SpecFunc)
9743068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar      return SpecFunc;
9753068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  }
976bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCall
977bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCall  bool isFriend;
9783068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  if (FunctionTemplate)
979bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
9803068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  else
9813068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
9823068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar
9837a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne  bool MergeWithParentScope = (TemplateParams != 0) ||
9843068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar    Owner->isFunctionOrMethod() ||
985ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    !(isa<Decl>(Owner) &&
986ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
987fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9883c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
9893068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  llvm::SmallVector<ParmVarDecl *, 4> Params;
9903068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
9913068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  TInfo = SubstFunctionType(D, Params);
9923068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  if (!TInfo)
993bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    return 0;
994c51974328b3a378c3c40b1fa527ecb928ed2bfdaChris Lattner  QualType T = TInfo->getType();
995fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
996883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
9973068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  if (QualifierLoc) {
9983068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
9993068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar                                                       TemplateArgs);
1000f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    if (!QualifierLoc)
1001f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher      return 0;
10023068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  }
10033068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar
10043068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  // If we're instantiating a local function declaration, put the result
10053068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  // in the owner;  otherwise we need to find the instantiated context.
1006bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCall  DeclContext *DC;
1007bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCall  if (D->getDeclContext()->isFunctionOrMethod())
10083068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar    DC = Owner;
1009bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  else if (isFriend && QualifierLoc) {
10103068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar    CXXScopeSpec SS;
10113068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar    SS.Adopt(QualifierLoc);
10123068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar    DC = SemaRef.computeDeclContext(SS);
10137a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne    if (!DC) return 0;
10143068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  } else {
1015ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1016ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor                                         TemplateArgs);
1017fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  }
10183c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
10193068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  FunctionDecl *Function =
10203068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
10213068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar                           D->getLocation(), D->getDeclName(), T, TInfo,
10223068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar                           D->getStorageClass(), D->getStorageClassAsWritten(),
1023bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                           D->isInlineSpecified(), D->hasWrittenPrototype());
10246782fc6925a85c3772253e272745589a0c799c15Anders Carlsson
1025fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  if (QualifierLoc)
1026883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    Function->setQualifierInfo(QualifierLoc);
10273068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar
10283068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  DeclContext *LexicalDC = Owner;
10293068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  if (!isFriend && D->isOutOfLine()) {
1030f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    assert(D->getDeclContext()->isFileContext());
1031f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    LexicalDC = D->getDeclContext();
10323068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar  }
10333068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar
1034803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner  Function->setLexicalDeclContext(LexicalDC);
1035951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner
1036951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner  // Attach the parameters
1037bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCall  for (unsigned P = 0; P < Params.size(); ++P)
1038c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian    if (Params[P])
1039c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian      Params[P]->setOwningFunction(Function);
1040951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner  Function->setParams(Params.data(), Params.size());
1041c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian
1042951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner  SourceLocation InstantiateAtPOI;
1043951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner  if (TemplateParams) {
1044951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    // Our resulting instantiation is actually a function template, since we
1045c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian    // are substituting only the outer template parameters. For example, given
1046951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    //
1047951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    //   template<typename T>
1048c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian    //   struct X {
1049c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian    //     template<typename U> friend void f(T, U);
1050951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    //   };
10516b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    //
1052bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    //   X<int> x;
1053951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    //
10546b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    // We are instantiating the friend function template "f" within X<int>,
10556b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    // which means substituting int for T, but leaving "f" as a friend function
1056bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian    // template.
1057951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    // Build the function template itself.
1058951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1059bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCall                                                    Function->getLocation(),
1060bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                    Function->getDeclName(),
1061bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                    TemplateParams, Function);
1062951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    Function->setDescribedFunctionTemplate(FunctionTemplate);
1063c784dc1caf0df288a383700f7b57772103b3adabFariborz Jahanian
1064951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1065951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner
1066951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    if (isFriend && D->isThisDeclarationADefinition()) {
1067c784dc1caf0df288a383700f7b57772103b3adabFariborz Jahanian      // TODO: should we remember this connection regardless of whether
1068951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner      // the friend declaration provided a body?
1069c784dc1caf0df288a383700f7b57772103b3adabFariborz Jahanian      FunctionTemplate->setInstantiatedFromMemberTemplate(
1070c784dc1caf0df288a383700f7b57772103b3adabFariborz Jahanian                                           D->getDescribedFunctionTemplate());
1071c784dc1caf0df288a383700f7b57772103b3adabFariborz Jahanian    }
1072951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner  } else if (FunctionTemplate) {
1073c784dc1caf0df288a383700f7b57772103b3adabFariborz Jahanian    // Record this function template specialization.
1074951bbb2a6d9641ea11a6fe81cba429152a055b7cChris Lattner    std::pair<const TemplateArgument *, unsigned> Innermost
1075bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian      = TemplateArgs.getInnermost();
1076bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian    Function->setFunctionTemplateSpecialization(FunctionTemplate,
10770a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
10780a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                             Innermost.first,
10790a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                             Innermost.second),
10800a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                InsertPos);
10810a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (isFriend && D->isThisDeclarationADefinition()) {
10820a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    // TODO: should we remember this connection regardless of whether
10830a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    // the friend declaration provided a body?
10840a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
10850a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  }
10860a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
10870a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (InitFunctionInstantiation(Function, D))
10880a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    Function->setInvalidDecl();
10890a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
10900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool Redeclaration = false;
10910a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  bool isExplicitSpecialization = false;
10920a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
10930a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10940a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10950a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
10960a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  if (DependentFunctionTemplateSpecializationInfo *Info
10970a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor        = D->getDependentSpecializationInfo()) {
10980a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    assert(isFriend && "non-friend has dependent specialization info?");
10990a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
11000a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    // This needs to be set now for future sanity.
11010a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
11020a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
11030a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    // Instantiate the explicit template arguments.
11040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
11050a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                          Info->getRAngleLoc());
11060a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
11070a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                      ExplicitArgs, TemplateArgs))
11080a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      return 0;
11090a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
11100a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    // Map the candidate templates to their instantiations.
11110a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
11120a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
11130a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                Info->getTemplate(I),
11140a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                TemplateArgs);
11150a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      if (!Temp) return 0;
11160a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
11170a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
11180a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    }
11190a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
11200a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
11210a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                    &ExplicitArgs,
11220a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                                                    Previous))
11230a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Function->setInvalidDecl();
11240a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
11250a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    isExplicitSpecialization = true;
11260a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor
11270a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (TemplateParams || !FunctionTemplate) {
1128803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner    // Look only into the namespace where the friend would be declared to
11296b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    // find a previous declaration. This is the innermost enclosing namespace,
1130545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner    // as described in ActOnFriendFunctionDecl.
11313c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    SemaRef.LookupQualifiedName(Previous, DC);
11326b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
11336b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    // In C++, the previous declaration we find might be a tag type
1134bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    // (class or enum). In this case, the new declaration will hide the
11357a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne    // tag type. Note that this does does not apply if we're declaring a
11366b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    // typedef (C++ [dcl.typedef]p4).
11376b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    if (Previous.isSingleTagDecl())
1138bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      Previous.clear();
11396b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  }
1140fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
11413c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
11426b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                   isExplicitSpecialization, Redeclaration);
11436b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1144bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  NamedDecl *PrincipalDecl = (TemplateParams
1145c96f49417b2039d6227b042cd2d975f0869df79dBenjamin Kramer                              ? cast<NamedDecl>(FunctionTemplate)
1146cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                              : Function);
1147bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
1148c96f49417b2039d6227b042cd2d975f0869df79dBenjamin Kramer  // If the original function was part of a friend declaration,
1149cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  // inherit its namespace state and add it to the owner.
1150c96f49417b2039d6227b042cd2d975f0869df79dBenjamin Kramer  if (isFriend) {
1151cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    NamedDecl *PrevDecl;
1152c96f49417b2039d6227b042cd2d975f0869df79dBenjamin Kramer    if (TemplateParams)
1153cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      PrevDecl = FunctionTemplate->getPreviousDeclaration();
1154c96f49417b2039d6227b042cd2d975f0869df79dBenjamin Kramer    else
1155cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      PrevDecl = Function->getPreviousDeclaration();
11566b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
115708631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
11586b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
11596b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1160bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    bool queuedInstantiation = false;
1161cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
11626b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    if (!SemaRef.getLangOptions().CPlusPlus0x &&
11636b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        D->isThisDeclarationADefinition()) {
1164d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      // Check for a function body.
1165d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      const FunctionDecl *Definition = 0;
1166d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      if (Function->hasBody(Definition) &&
1167d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1168d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1169883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall          << Function->getDeclName();
1170d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1171d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        Function->setInvalidDecl();
1172d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      }
1173d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      // Check for redefinitions due to other instantiations of this or
1174d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      // a similar friend function.
1175d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1176d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                                           REnd = Function->redecls_end();
1177d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                R != REnd; ++R) {
1178d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        if (*R == Function)
1179d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall          continue;
1180d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        switch (R->getFriendObjectKind()) {
1181d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        case Decl::FOK_None:
1182d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall          if (!queuedInstantiation && R->isUsed(false)) {
1183d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall            if (MemberSpecializationInfo *MSInfo
1184d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                = Function->getMemberSpecializationInfo()) {
1185d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1186d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                SourceLocation Loc = R->getLocation(); // FIXME
1187d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                MSInfo->setPointOfInstantiation(Loc);
1188d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                SemaRef.PendingLocalImplicitInstantiations.push_back(
1189d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                                                 std::make_pair(Function, Loc));
1190d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                queuedInstantiation = true;
1191d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall              }
1192d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall            }
1193d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall          }
1194d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall          break;
1195d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        default:
1196d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall          if (const FunctionDecl *RPattern
1197d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall              = R->getTemplateInstantiationPattern())
1198d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall            if (RPattern->hasBody(RPattern)) {
1199d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1200d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall                << Function->getDeclName();
1201d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1202d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall              Function->setInvalidDecl();
1203d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall              break;
1204d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall            }
1205d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        }
1206d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall      }
1207d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall    }
1208d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall  }
12090db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner
12100db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner  if (Function->isOverloadedOperator() && !DC->isRecord() &&
12110db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
12120db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner    PrincipalDecl->setNonMemberOperator();
12130db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner
12140db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner  return Function;
1215bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump}
12160db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner
12170db29ece81d360dcefbe912339c34abe5917f6a9Chris LattnerDecl *
12180db29ece81d360dcefbe912339c34abe5917f6a9Chris LattnerTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
12190db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner                                      TemplateParameterList *TemplateParams) {
12200db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1221bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  void *InsertPos = 0;
1222cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  if (FunctionTemplate && !TemplateParams) {
12230db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner    // We are creating a function template specialization from a function
12240db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner    // template. Check whether there is already a function template
12250db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner    // specialization for this particular set of template arguments.
1226fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    std::pair<const TemplateArgument *, unsigned> Innermost
12272b7baf0816a40af3fde3a3e174192a549b785a50John McCall      = TemplateArgs.getInnermost();
1228fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
1229fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    FunctionDecl *SpecFunc
12300db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1231fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian                                             InsertPos);
1232fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
12336217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    // If we already have a function template specialization, return it.
1234fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (SpecFunc)
1235fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian      return SpecFunc;
1236fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
1237fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
1238cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  bool isFriend;
1239fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (FunctionTemplate)
1240fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1241bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  else
1242f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1243f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor
12442b7baf0816a40af3fde3a3e174192a549b785a50John McCall  bool MergeWithParentScope = (TemplateParams != 0) ||
1245f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor    !(isa<Decl>(Owner) &&
1246f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1247f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
1248f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor
1249f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor  // Instantiate enclosing template arguments for friends.
1250f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor  llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1251f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor  unsigned NumTempParamLists = 0;
1252f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1253cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    TempParamLists.set_size(NumTempParamLists);
1254f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor    for (unsigned I = 0; I != NumTempParamLists; ++I) {
1255f9201e0ff1779567150b70856753d9f2c6a91467Douglas Gregor      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
12569eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1257bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      if (!InstParams)
1258fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner        return NULL;
12593c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner      TempParamLists[I] = InstParams;
12609eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff    }
12619eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  }
1262bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
12639eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  llvm::SmallVector<ParmVarDecl *, 4> Params;
12643c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
12659eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  TInfo = SubstFunctionType(D, Params);
12669eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  if (!TInfo)
1267bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    return 0;
1268cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  QualType T = TInfo->getType();
126992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner
12709eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  // \brief If the type of this function, after ignoring parentheses,
12719eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  // is not *directly* a function type, then we're instantiating a function
1272fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  // that was declared via a typedef, e.g.,
12733c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  //
12749eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  //   typedef int functype(int, int);
12759eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  //   functype func;
1276bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  //
1277cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  // In this case, we'll just go instantiate the ParmVarDecls that we
12789eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  // synthesized in the method declaration.
12799eae5761c0691c0f11d7a823b8ee54f05786cbbeSteve Naroff  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
1280770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    assert(!Params.size() && "Instantiating type could not yield parameters");
1281770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    llvm::SmallVector<QualType, 4> ParamTypes;
1282770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1283bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCall                               D->getNumParams(), TemplateArgs, ParamTypes,
1284770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                               &Params))
1285bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      return 0;
1286bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  }
1287770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson
1288770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
12897a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne  if (QualifierLoc) {
1290770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1291ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor                                                 TemplateArgs);
1292ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    if (!QualifierLoc)
1293fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner      return 0;
12943c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  }
1295770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson
1296770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  DeclContext *DC = Owner;
1297770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  if (isFriend) {
1298bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    if (QualifierLoc) {
1299770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson      CXXScopeSpec SS;
1300fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner      SS.Adopt(QualifierLoc);
1301fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner      DC = SemaRef.computeDeclContext(SS);
1302770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson
1303770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1304770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson        return 0;
1305770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    } else {
1306770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1307770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                           D->getDeclContext(),
13087a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne                                           TemplateArgs);
1309770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    }
1310ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    if (!DC) return 0;
1311ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  }
1312fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
13133c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  // Build the instantiated method declaration.
1314770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1315770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  CXXMethodDecl *Method = 0;
1316770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson
1317bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  SourceLocation StartLoc = D->getInnerLocStart();
1318770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  DeclarationNameInfo NameInfo
1319770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1320770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1321fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1322fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner                                        StartLoc, NameInfo, T, TInfo,
1323770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                        Constructor->isExplicit(),
1324770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                        Constructor->isInlineSpecified(),
1325770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                        false);
1326770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1327770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1328183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                       StartLoc, NameInfo, T, TInfo,
1329897cd90fef4cd5139999585f3af31d85c2d07720Chris Lattner                                       Destructor->isInlineSpecified(),
1330bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                       false);
1331897cd90fef4cd5139999585f3af31d85c2d07720Chris Lattner  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
1332897cd90fef4cd5139999585f3af31d85c2d07720Chris Lattner    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1333897cd90fef4cd5139999585f3af31d85c2d07720Chris Lattner                                       StartLoc, NameInfo, T, TInfo,
1334897cd90fef4cd5139999585f3af31d85c2d07720Chris Lattner                                       Conversion->isInlineSpecified(),
1335bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                       Conversion->isExplicit(),
1336897cd90fef4cd5139999585f3af31d85c2d07720Chris Lattner                                       Conversion->getLocEnd());
13373bba33d6f58844d4924ab1e221dc2ff44c521624Fariborz Jahanian  } else {
1338770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1339bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                   StartLoc, NameInfo, T, TInfo,
1340770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                   D->isStatic(),
1341770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                   D->getStorageClassAsWritten(),
13423bba33d6f58844d4924ab1e221dc2ff44c521624Fariborz Jahanian                                   D->isInlineSpecified(),
1343770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                   D->getLocEnd());
13442f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian  }
13452f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian
1346bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  if (QualifierLoc)
1347bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    Method->setQualifierInfo(QualifierLoc);
13482f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian
13492f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian  if (TemplateParams) {
13502f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian    // Our resulting instantiation is actually a function template, since we
1351daf0415583e33d5d279197c65e9227c1ed92474bFariborz Jahanian    // are substituting only the outer template parameters. For example, given
1352bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    //
1353f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    //   template<typename T>
13542f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian    //   struct X {
13553bba33d6f58844d4924ab1e221dc2ff44c521624Fariborz Jahanian    //     template<typename U> void f(T, U);
13563bba33d6f58844d4924ab1e221dc2ff44c521624Fariborz Jahanian    //   };
13572f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian    //
13582f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian    //   X<int> x;
1359ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump    //
13602f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian    // We are instantiating the member template "f" within X<int>, which means
1361883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    // substituting int for T, but leaving "f" as a member function template.
13622f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian    // Build the function template itself.
13632f7c39246a968b921a6d95c7f8037fb3429e9501Fariborz Jahanian    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1364770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson                                                    Method->getLocation(),
1365fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner                                                    Method->getDeclName(),
1366883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                                                    TemplateParams, Method);
1367770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    if (isFriend) {
1368770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson      FunctionTemplate->setLexicalDeclContext(Owner);
1369f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher      FunctionTemplate->setObjectOfFriendDecl(true);
1370f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    } else if (D->isOutOfLine())
1371770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1372770918281c5bdc7b5b3942285c407e3d62270053Anders Carlsson    Method->setDescribedFunctionTemplate(FunctionTemplate);
1373026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner  } else if (FunctionTemplate) {
1374026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // Record this function template specialization.
1375026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    std::pair<const TemplateArgument *, unsigned> Innermost
1376026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner      = TemplateArgs.getInnermost();
1377026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1378026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1379026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                                                          Innermost.first,
1380f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian                                                          Innermost.second),
1381026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                                              InsertPos);
1382883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  } else if (!isFriend) {
1383026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    // Record that this is an instantiation of a member function.
1384026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
1385bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  }
1386f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian
1387f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  // If we are instantiating a member function defined
1388f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian  // out-of-line, the instantiation will have the same lexical
1389f857798fa77ac50c6d0a262d96ad6176187190e3Nuno Lopes  // context (which will be a namespace scope) as the template.
1390f857798fa77ac50c6d0a262d96ad6176187190e3Nuno Lopes  if (isFriend) {
1391f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    if (NumTempParamLists)
1392f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian      Method->setTemplateParameterListsInfo(SemaRef.Context,
1393f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian                                            NumTempParamLists,
1394f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian                                            TempParamLists.data());
1395f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian
1396f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    Method->setLexicalDeclContext(Owner);
1397f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian    Method->setObjectOfFriendDecl(true);
1398cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  } else if (D->isOutOfLine())
1399026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    Method->setLexicalDeclContext(D->getLexicalDeclContext());
1400026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner
1401332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  // Attach the parameters
14026b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  for (unsigned P = 0; P < Params.size(); ++P)
1403332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    Params[P]->setOwningFunction(Method);
1404332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  Method->setParams(Params.data(), Params.size());
14056b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
14066b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  if (InitMethodInstantiation(Method, D))
14076e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar    Method->setInvalidDecl();
1408332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
1409332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1410883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                        Sema::ForRedeclaration);
1411f23ecd91bf0205b776dfab2c5231e895019a7400Fariborz Jahanian
1412f23ecd91bf0205b776dfab2c5231e895019a7400Fariborz Jahanian  if (!FunctionTemplate || TemplateParams || isFriend) {
1413f23ecd91bf0205b776dfab2c5231e895019a7400Fariborz Jahanian    SemaRef.LookupQualifiedName(Previous, Record);
1414332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
1415332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    // In C++, the previous declaration we find might be a tag type
1416332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    // (class or enum). In this case, the new declaration will hide the
1417332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    // tag type. Note that this does does not apply if we're declaring a
1418332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall    // typedef (C++ [dcl.typedef]p4).
14196e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar    if (Previous.isSingleTagDecl())
14206e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar      Previous.clear();
1421bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  }
1422332bb2a2e3cd0a5af85758847a8050ae8ceee5f3John McCall
14236b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  bool Redeclaration = false;
14246b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
14256e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar
14266e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar  if (D->isPure())
14276e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar    SemaRef.CheckPureMethod(Method, SourceRange());
14286e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar
14296e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar  Method->setAccess(D->getAccess());
1430bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
14316e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar  SemaRef.CheckOverrideControl(Method);
14326e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar
14336e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar  if (FunctionTemplate) {
14340a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    // If there's a function template, let our caller handle it.
14350a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else if (Method->isInvalidDecl() && !Previous.empty()) {
14360a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    // Don't hide a (potentially) valid declaration with an invalid one.
14370a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  } else {
14380a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    NamedDecl *DeclToAdd = (TemplateParams
14390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                            ? cast<NamedDecl>(FunctionTemplate)
14400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                            : Method);
14410a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    if (isFriend)
14420a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor      Record->makeDeclVisibleInContext(DeclToAdd);
1443c034974f103873bdccc91da99a30ab30295b5226Fariborz Jahanian    else
1444883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall      Owner->addDecl(DeclToAdd);
14456e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar  }
14466e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar
14476e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar  return Method;
14486e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar}
1449cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
14506e775dbafba2ab6634decc489eb3b4301b4b506bDaniel DunbarDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
14516e775dbafba2ab6634decc489eb3b4301b4b506bDaniel Dunbar  return VisitCXXMethodDecl(D);
14526f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman}
14536f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman
14546f3d838867538638b9bbf412028e8537ae12f3e5Nate BegemanDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
14556f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  return VisitCXXMethodDecl(D);
14562b7baf0816a40af3fde3a3e174192a549b785a50John McCall}
14576f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman
14586f3d838867538638b9bbf412028e8537ae12f3e5Nate BegemanDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
14596f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  return VisitCXXMethodDecl(D);
14606f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman}
14616f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman
14627a73002783b30dcf613b06dbe618cfc1d1116ff8Peter CollingbourneParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
14636f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
1464ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor}
1465ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor
14666f3d838867538638b9bbf412028e8537ae12f3e5Nate BegemanDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
14676f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman                                                    TemplateTypeParmDecl *D) {
14686f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  // TODO: don't always clone when decls are refcounted.
14696f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  const Type* T = D->getTypeForDecl();
14706f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  assert(T->isTemplateTypeParmType());
14716f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
1472cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
1473cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  TemplateTypeParmDecl *Inst =
14746f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
14756f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman                                 D->getLocStart(), D->getLocation(),
14766f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman                                 TTPT->getDepth() - TemplateArgs.getNumLevels(),
1477026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner                                 TTPT->getIndex(), D->getIdentifier(),
147817f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar                                 D->wasDeclaredWithTypename(),
147917f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar                                 D->isParameterPack());
148017f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar  Inst->setAccess(AS_public);
148117f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
148217f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar  if (D->hasDefaultArgument())
148317f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
148417f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
148517f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar  // Introduce this template parameter's instantiation into the instantiation
14867a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne  // scope.
1487797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
148817f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
1489797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner  return Inst;
149017f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar}
149117f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1493797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner                                                 NonTypeTemplateParmDecl *D) {
1494bb377edda2656752016a0bc01fe4f9f8b6f80e19Benjamin Kramer  // Substitute into the type of the non-type template parameter.
1495a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1496a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner  llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1497a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner  llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1498797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner  bool IsExpandedParameterPack = false;
1499797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner  TypeSourceInfo *DI;
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualType T;
1501a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner  bool Invalid = false;
1502a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner
1503a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner  if (D->isExpandedParameterPack()) {
1504a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner    // The non-type template parameter pack is an already-expanded pack
1505a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner    // expansion of types. Substitute into each of the expanded types.
1506a1e1dc77e995b746826b64752751dbf35f323767Chris Lattner    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1507f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1508f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
150917f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
151017f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar                                               TemplateArgs,
15116b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                               D->getLocation(),
1512803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner                                               D->getDeclName());
15136b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      if (!NewDI)
1514545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner        return 0;
15153c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
15166b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
15176b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1518bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                                              D->getLocation());
1519cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      if (NewT.isNull())
15206b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        return 0;
15216b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      ExpandedParameterPackTypes.push_back(NewT);
1522232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    }
1523232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson
1524232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    IsExpandedParameterPack = true;
15253c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    DI = D->getTypeSourceInfo();
1526232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    T = DI->getType();
1527232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson  } else if (isa<PackExpansionTypeLoc>(TL)) {
1528bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    // The non-type template parameter pack's type is a pack expansion of types.
1529cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    // Determine whether we need to expand this parameter pack into separate
1530232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    // types.
1531232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1532232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    TypeLoc Pattern = Expansion.getPatternLoc();
1533232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1534232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
15353c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner
1536232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    // Determine whether the set of unexpanded parameter packs can and should
1537232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    // be expanded.
1538bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    bool Expand = true;
1539cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    bool RetainExpansion = false;
1540232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    llvm::Optional<unsigned> OrigNumExpansions
1541232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      = Expansion.getTypePtr()->getNumExpansions();
1542f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson    llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1543bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1544f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson                                                Pattern.getSourceRange(),
1545f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson                                                Unexpanded.data(),
1546f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson                                                Unexpanded.size(),
1547bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                                TemplateArgs,
1548f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson                                                Expand, RetainExpansion,
1549f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson                                                NumExpansions))
1550f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      return 0;
1551f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson
1552bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    if (Expand) {
1553f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      for (unsigned I = 0; I != *NumExpansions; ++I) {
1554bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1555f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1556f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson                                                  D->getLocation(),
1557f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson                                                  D->getDeclName());
1558f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson        if (!NewDI)
1559bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump          return 0;
1560f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson
1561c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1562f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1563f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis                                                              NewDI->getType(),
1564f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis                                                              D->getLocation());
1565f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson        if (NewT.isNull())
1566f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis          return 0;
1567f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson        ExpandedParameterPackTypes.push_back(NewT);
1568f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      }
1569f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson
1570bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      // Note that we have an expanded parameter pack. The "type" of this
1571f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      // expanded parameter pack is the original expansion type, but callers
1572f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      // will end up using the expanded parameter pack types for type-checking.
1573f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis      IsExpandedParameterPack = true;
1574f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis      DI = D->getTypeSourceInfo();
1575f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis      T = DI->getType();
1576f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson    } else {
1577f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      // We cannot fully expand the pack expansion now, so substitute into the
1578f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      // pattern and create a new pack expansion type.
1579f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1580f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1581f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis                                                     D->getLocation(),
1582f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis                                                     D->getDeclName());
1583f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson      if (!NewPattern)
1584f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson        return 0;
1585bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
158689941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
158789941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson                                      NumExpansions);
158889941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson      if (!DI)
158989941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson        return 0;
1590b608b987718c6d841115464f79ab2d1820a63e17Douglas Gregor
1591b608b987718c6d841115464f79ab2d1820a63e17Douglas Gregor      T = DI->getType();
1592f0b0ccce127857e7e4fb829e017dbcb7487884c4Argyrios Kyrtzidis    }
159389941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson  } else {
159489941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson    // Simple case: substitution into a parameter that is not a parameter pack.
159589941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
159689941c1c68d8e4eec3c8ea8ee68e34d9e3c7b083Anders Carlsson                           D->getLocation(), D->getDeclName());
1597bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    if (!DI)
1598cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      return 0;
1599223ae5c26654e5fd7dacdafe43aff28a096ba63bArgyrios Kyrtzidis
1600f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson    // Check that this type is acceptable for a non-type template parameter.
1601f6e35d0b9f1e9f1b4c5d3ef924415fa5e7c89849Anders Carlsson    bool Invalid = false;
1602bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1603bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                                  D->getLocation());
1604bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    if (T.isNull()) {
16055b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian      T = SemaRef.Context.IntTy;
16065b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian      Invalid = true;
16075b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    }
16085b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  }
16095b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian
16105b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  NonTypeTemplateParmDecl *Param;
1611883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  if (IsExpandedParameterPack)
16125b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
16135b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                            D->getInnerLocStart(),
161407d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                                            D->getLocation(),
161507d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                                    D->getDepth() - TemplateArgs.getNumLevels(),
161607d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                                            D->getPosition(),
161707d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                                            D->getIdentifier(), T,
161807d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                                            DI,
16195b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                            ExpandedParameterPackTypes.data(),
162007d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                                            ExpandedParameterPackTypes.size(),
16215b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                    ExpandedParameterPackTypesAsWritten.data());
16227a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne  else
16235b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1624ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor                                            D->getInnerLocStart(),
1625ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor                                            D->getLocation(),
16265b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                    D->getDepth() - TemplateArgs.getNumLevels(),
16275b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                            D->getPosition(),
16285b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                            D->getIdentifier(), T,
16295b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                            D->isParameterPack(), DI);
1630bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
16315b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  Param->setAccess(AS_public);
16325b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  if (Invalid)
16335b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    Param->setInvalidDecl();
16345b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian
16355b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  Param->setDefaultArgument(D->getDefaultArgument(), false);
1636bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
16375b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  // Introduce this template parameter's instantiation into the instantiation
1638bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  // scope.
163907d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
164007d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  return Param;
164107d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth}
164207d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
164307d7e7a6b10f798459f350b792713db2fb3e9365Chandler CarruthDecl *
164407d7e7a6b10f798459f350b792713db2fb3e9365Chandler CarruthTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
164507d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth                                                  TemplateTemplateParmDecl *D) {
164607d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  // Instantiate the template parameter list of the template template parameter.
164707d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  TemplateParameterList *TempParams = D->getTemplateParameters();
16485b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  TemplateParameterList *InstParams;
16495b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  {
1650bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    // Perform the actual substitution of template parameters within a new,
16515b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    // local instantiation scope.
16525b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    LocalInstantiationScope Scope(SemaRef);
16535b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    InstParams = SubstTemplateParams(TempParams);
16545b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    if (!InstParams)
16556217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      return NULL;
16565b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  }
16575b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian
1658bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  // Build the template template parameter.
16595b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  TemplateTemplateParmDecl *Param
16605b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1661bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                   D->getDepth() - TemplateArgs.getNumLevels(),
16625b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                       D->getPosition(), D->isParameterPack(),
16635b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian                                       D->getIdentifier(), InstParams);
16645b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  Param->setDefaultArgument(D->getDefaultArgument(), false);
16655b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  Param->setAccess(AS_public);
16666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek
16675b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  // Introduce this template parameter's instantiation into the instantiation
16685b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  // scope.
1669bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
16705b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian
16715b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  return Param;
1672bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump}
1673bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
167407d7e7a6b10f798459f350b792713db2fb3e9365Chandler CarruthDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
167507d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  // Using directives are never dependent (and never contain any types or
16765b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian  // expressions), so they require no explicit instantiation work.
16775b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian
16782b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  UsingDirectiveDecl *Inst
16792b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
16802b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar                                 D->getNamespaceKeyLocation(),
16812b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar                                 D->getQualifierLoc(),
16822b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar                                 D->getIdentLocation(),
16833c989027f68e2d9dfd57c018ccc550bd9fb79920Chris Lattner                                 D->getNominatedNamespace(),
16842b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar                                 D->getCommonAncestor());
16852b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  Owner->addDecl(Inst);
16862b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  return Inst;
16872b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar}
16882b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar
16892b0d9a247ab375ca316bf04feede73de6672fc31Daniel DunbarDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
16902b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar
16912b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // The nested name specifier may be dependent, for example
16922b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  //     template <typename T> struct t {
16932b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  //       struct s1 { T f1(); };
16942b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  //       struct s2 : s1 { using s1::f1; };
16952b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  //     };
16962b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  //     template struct t<int>;
16972b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // Here, in using s1::f1, s1 refers to t<T>::s1;
16982b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // we need to substitute for t<int>::s1.
16992b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  NestedNameSpecifierLoc QualifierLoc
17002b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
17012b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar                                          TemplateArgs);
1702cd5b306f1b79c8a82fb0bdb4cf353021ea452fedChris Lattner  if (!QualifierLoc)
1703cd5b306f1b79c8a82fb0bdb4cf353021ea452fedChris Lattner    return 0;
17042b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar
17052b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // The name info is non-dependent, so no transformation
1706bc52595e01323ca22d65c68aafd53a1acb8c1fb6Duncan Sands  // is required.
1707bc52595e01323ca22d65c68aafd53a1acb8c1fb6Duncan Sands  DeclarationNameInfo NameInfo = D->getNameInfo();
17083c989027f68e2d9dfd57c018ccc550bd9fb79920Chris Lattner
17093c989027f68e2d9dfd57c018ccc550bd9fb79920Chris Lattner  // We only need to do redeclaration lookups if we're in a class
17102b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // scope (in fact, it's not really even possible in non-class
17112b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // scopes).
17122b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  bool CheckRedeclaration = Owner->isRecord();
1713521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian
1714521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1715521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian                    Sema::ForRedeclaration);
1716521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian
1717521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1718521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian                                       D->getUsingLocation(),
1719521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian                                       QualifierLoc,
1720521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian                                       NameInfo,
1721521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian                                       D->isTypeName());
1722b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian
1723b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian  CXXScopeSpec SS;
1724b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian  SS.Adopt(QualifierLoc);
1725b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian  if (CheckRedeclaration) {
1726b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian    Prev.setHideTags(false);
1727b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian    SemaRef.LookupQualifiedName(Prev, Owner);
1728b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian
1729b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian    // Check for invalid redeclarations.
1730b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1731b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian                                            D->isTypeName(), SS,
1732b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian                                            D->getLocation(), Prev))
1733b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian      NewUD->setInvalidDecl();
1734b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian
1735b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian  }
1736521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian
1737521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  if (!NewUD->isInvalidDecl() &&
1738521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1739521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian                                      D->getLocation()))
1740521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian    NewUD->setInvalidDecl();
17417a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne
1742b9d5c22529c3f2bf3b03348021c0fd1c723d8516Fariborz Jahanian  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1743521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  NewUD->setAccess(D->getAccess());
1744521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  Owner->addDecl(NewUD);
1745521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian
1746521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  // Don't process the shadow decls for an invalid decl.
1747521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  if (NewUD->isInvalidDecl())
1748521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian    return NewUD;
1749521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian
1750521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  bool isFunctionScope = Owner->isFunctionOrMethod();
17519f967c5e4bbeb48caf6d0e62056b3d3fee20bf7cFariborz Jahanian
1752521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  // Process the shadow decls.
1753521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1754521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian         I != E; ++I) {
1755521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian    UsingShadowDecl *Shadow = *I;
1756521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian    NamedDecl *InstTarget =
1757521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
1758f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher                                                          Shadow->getLocation(),
1759f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher                                                        Shadow->getTargetDecl(),
1760521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian                                                           TemplateArgs));
1761521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian    if (!InstTarget)
1762bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      return 0;
1763bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
1764803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner    if (CheckRedeclaration &&
17656b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1766545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner      continue;
1767fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
17683c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    UsingShadowDecl *InstShadow
17696b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
17706b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
17716b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1772545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner    if (isFunctionScope)
17733c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
17746b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  }
17756b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
17766b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  return NewUD;
1777620d89ca4eb5dcb6be13a42aafa4849eaa9b834bFariborz Jahanian}
1778fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
1779883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
17806b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // Ignore these;  we handle them in bulk when processing the UsingDecl.
17816b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  return 0;
17826b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner}
178307d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
178407d7e7a6b10f798459f350b792713db2fb3e9365Chandler CarruthDecl * TemplateDeclInstantiator
178507d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
178607d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  NestedNameSpecifierLoc QualifierLoc
17876b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
17886b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                          TemplateArgs);
178901eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar  if (!QualifierLoc)
17906b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    return 0;
17916b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
17922b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  CXXScopeSpec SS;
17932b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  SS.Adopt(QualifierLoc);
17942b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar
17952b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // Since NameInfo refers to a typename, it cannot be a C++ special name.
17962b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  // Hence, no tranformation is required for it.
17973c989027f68e2d9dfd57c018ccc550bd9fb79920Chris Lattner  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
17983c989027f68e2d9dfd57c018ccc550bd9fb79920Chris Lattner  NamedDecl *UD =
17993c989027f68e2d9dfd57c018ccc550bd9fb79920Chris Lattner    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
18003c989027f68e2d9dfd57c018ccc550bd9fb79920Chris Lattner                                  D->getUsingLoc(), SS, NameInfo, 0,
18012b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar                                  /*instantiation*/ true,
1802fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner                                  /*typename*/ true, D->getTypenameLoc());
180301eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar  if (UD)
18046b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
18056b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
18066b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  return UD;
18076b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner}
18087a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne
1809803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris LattnerDecl * TemplateDeclInstantiator
1810ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1811ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  NestedNameSpecifierLoc QualifierLoc
1812fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
18133c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  if (!QualifierLoc)
18146b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    return 0;
18156b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
18166b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  CXXScopeSpec SS;
18176b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  SS.Adopt(QualifierLoc);
1818fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
18193c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  DeclarationNameInfo NameInfo
18206b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
18216b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
18226b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  NamedDecl *UD =
18236b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
18246b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                  D->getUsingLoc(), SS, NameInfo, 0,
1825bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                  /*instantiation*/ true,
18264a2614e94672c47395abcde60518776fbebec589Sebastian Redl                                  /*typename*/ false, SourceLocation());
18274a2614e94672c47395abcde60518776fbebec589Sebastian Redl  if (UD)
182807d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
182907d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth
183007d7e7a6b10f798459f350b792713db2fb3e9365Chandler Carruth  return UD;
18314a2614e94672c47395abcde60518776fbebec589Sebastian Redl}
18324a2614e94672c47395abcde60518776fbebec589Sebastian Redl
18334a2614e94672c47395abcde60518776fbebec589Sebastian RedlDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
18344a2614e94672c47395abcde60518776fbebec589Sebastian Redl                      const MultiLevelTemplateArgumentList &TemplateArgs) {
18351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
18366b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  if (D->isInvalidDecl())
18373568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar    return 0;
18386b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
18392b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  return Instantiator.Visit(D);
1840085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar}
1841fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
1842fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner/// \brief Instantiates a nested template parameter list in the current
1843085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar/// instantiation context.
1844085e8f7da37a227ceee7f98b724e0a42e04d01caDaniel Dunbar///
18452b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar/// \param L The parameter list to instantiate
1846390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump///
1847390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump/// \returns NULL if there was an error
1848803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris LattnerTemplateParameterList *
1849390b4cc8b45a05612349269ef08faab3e4688f06Mike StumpTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1850fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  // Get errors for all the parameters before bailing out.
1851fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  bool Invalid = false;
18526b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1853bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  unsigned N = L->size();
18546b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
18556217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  ParamVector Params;
1856390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  Params.reserve(N);
1857fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1858fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner       PI != PE; ++PI) {
18596b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
18606b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    Params.push_back(D);
18616b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    Invalid = Invalid || !D || D->isInvalidDecl();
18626b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  }
18637a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne
1864803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner  // Clean up if we had an error.
1865ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor  if (Invalid)
1866ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor    return NULL;
1867fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
18683c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  TemplateParameterList *InstL
18696b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
18706b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                    L->getLAngleLoc(), &Params.front(), N,
18716b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                    L->getRAngleLoc());
18726b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  return InstL;
18736b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner}
18743568249c2d72d58b835a22d9186f5a6b4fc4bcd6Daniel Dunbar
18756b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner/// \brief Instantiate the declaration of a class template partial
18766b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner/// specialization.
1877803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner///
18786b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner/// \param ClassTemplate the (instantiated) class template that is partially
18796b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner// specialized by the instantiation of \p PartialSpec.
18806b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner///
18816b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner/// \param PartialSpec the (uninstantiated) class template partial
18823c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner/// specialization that we are instantiating.
18833c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner///
18842b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar/// \returns The instantiated partial specialization, if successful; otherwise,
18856b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner/// NULL to indicate an error.
1886fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris LattnerClassTemplatePartialSpecializationDecl *
1887fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris LattnerTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
18886b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                            ClassTemplateDecl *ClassTemplate,
18896b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
18906b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // Create a local instantiation scope for this class template partial
18916b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // specialization, which will contain the instantiations of the template
1892fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner  // parameters.
18933c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  LocalInstantiationScope Scope(SemaRef);
18946b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
18956b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // Substitute into the template parameters of the class template partial
18966b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // specialization.
1897cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1898cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
18992b0d9a247ab375ca316bf04feede73de6672fc31Daniel Dunbar  if (!InstParams)
19006b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    return 0;
19016b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
19020b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner  // Substitute into the template arguments of the class template partial
19030b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner  // specialization.
19046b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1905545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
19063c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner                    PartialSpec->getNumTemplateArgsAsWritten(),
19076b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                    InstTemplateArgs, TemplateArgs))
19086b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    return 0;
19096b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
19100c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // Check that the template argument list is well-formed for this
19110c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // class template.
1912bc88745b43f440341e60ed93b0d27bac7c418029Eli Friedman  llvm::SmallVector<TemplateArgument, 4> Converted;
19130c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
19140c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                        PartialSpec->getLocation(),
19150c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                        InstTemplateArgs,
19160c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                        false,
19170c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                        Converted))
19180c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    return 0;
1919fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner
1920883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  // Figure out where to insert this class template partial specialization
19216b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // in the member template's set of class template partial specializations.
19226b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  void *InsertPos = 0;
19236b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  ClassTemplateSpecializationDecl *PrevDecl
19240c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
1925bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                               Converted.size(), InsertPos);
19260c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor
19270c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // Build the canonical type that describes the converted template
19280c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // arguments of the class template partial specialization.
19290c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  QualType CanonType
193017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
193117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                                    Converted.data(),
19320c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                                    Converted.size());
19330c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor
19340c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // Build the fully-sugared type for this class template
19350c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // specialization as the user wrote in the specialization
1936bc88745b43f440341e60ed93b0d27bac7c418029Eli Friedman  // itself. This means that we'll pretty-print the type retrieved
19370c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // from the specialization's declaration the way that the user
19380c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  // actually wrote the specialization, rather than formatting the
193990cd672ed107d5986936c577ce47ad7374096bd2Douglas Gregor  // name based on the "canonical" representation used to store the
1940bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  // template arguments in the specialization.
194190cd672ed107d5986936c577ce47ad7374096bd2Douglas Gregor  TypeSourceInfo *WrittenTy
194290cd672ed107d5986936c577ce47ad7374096bd2Douglas Gregor    = SemaRef.Context.getTemplateSpecializationTypeInfo(
19430c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                                    TemplateName(ClassTemplate),
19440c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                                    PartialSpec->getLocation(),
1945bc88745b43f440341e60ed93b0d27bac7c418029Eli Friedman                                                    InstTemplateArgs,
19460c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor                                                    CanonType);
19470c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor
19480c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor  if (PrevDecl) {
19490c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    // We've already seen a partial specialization with the same template
19500c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    // parameters and template arguments. This can happen, for example, when
19510c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    // substituting the outer template arguments ends up causing two
19520c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    // class template partial specializations of a member class template
19530c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    // to have identical forms, e.g.,
1954bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    //
19550c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    //   template<typename T, typename U>
1956bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    //   struct Outer {
19570c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    //     template<typename X, typename Y> struct Inner;
19580c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
19590c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1960bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    //   };
19610c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    //
19620c74e8a4e8865ec9ebb8efc0af247a3c077236c4Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1963bc88745b43f440341e60ed93b0d27bac7c418029Eli Friedman    //                          // have the same signature.
1964bc88745b43f440341e60ed93b0d27bac7c418029Eli Friedman    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1965bc88745b43f440341e60ed93b0d27bac7c418029Eli Friedman      << WrittenTy->getType();
19666b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1967cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      << SemaRef.Context.getTypeDeclType(PrevDecl);
19686b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    return 0;
19696b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  }
19700b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner
19716b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1972545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  // Create the class template partial specialization declaration.
19733c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  ClassTemplatePartialSpecializationDecl *InstPartialSpec
19746b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
19756b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                                     PartialSpec->getTagKind(),
19767a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne                                                     Owner,
1977797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner                                                     PartialSpec->getLocStart(),
1978bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                                     PartialSpec->getLocation(),
19796b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                                     InstParams,
19806b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                                     ClassTemplate,
19816b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                                     Converted.data(),
1982797c3c4f5dc4fda735e55c6b5d6270a54cf6d263Chris Lattner                                                     Converted.size(),
19836b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                                     InstTemplateArgs,
19846b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner                                                     CanonType,
1985f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher                                                     0,
1986f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher                             ClassTemplate->getNextPartialSpecSequenceNumber());
19876b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // Substitute the nested name specifier, if any.
19886b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  if (SubstQualifier(PartialSpec, InstPartialSpec))
19894ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    return 0;
19906b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
1991545dd3401e7f31c256d69cb948a45d5ca781064cChris Lattner  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
19923c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner  InstPartialSpec->setTypeAsWritten(WrittenTy);
19936b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
19946b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  // Add this partial specialization to the set of class template partial
1995bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  // specializations.
1996bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
1997bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt  return InstPartialSpec;
1998bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt}
19996b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
2000545dd3401e7f31c256d69cb948a45d5ca781064cChris LattnerTypeSourceInfo*
2001cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean HuntTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
20024ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
20034ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
20044ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  assert(OldTInfo && "substituting function without type source info");
20057a73002783b30dcf613b06dbe618cfc1d1116ff8Peter Collingbourne  assert(Params.empty() && "parameter vector is non-empty at start");
20064ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  TypeSourceInfo *NewTInfo
20074ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
20084ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth                                    D->getTypeSpecStartLoc(),
20094ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth                                    D->getDeclName());
20104ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  if (!NewTInfo)
2011cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    return 0;
20126b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
20136b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner  if (NewTInfo != OldTInfo) {
2014bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump    // Get parameters from the new type info.
2015cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
201649e2d34f74f98bef23e37c415ce90cd783cdea24Chris Lattner    if (FunctionProtoTypeLoc *OldProtoLoc
20174ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
20184ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
20194ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
202049e2d34f74f98bef23e37c415ce90cd783cdea24Chris Lattner      assert(NewProtoLoc && "Missing prototype?");
202149e2d34f74f98bef23e37c415ce90cd783cdea24Chris Lattner      unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
2022396b2a22788b0134018760d6a476de1e20f81334Daniel Dunbar      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
20234ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth           OldIdx != NumOldParams; ++OldIdx) {
20244ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
2025396b2a22788b0134018760d6a476de1e20f81334Daniel Dunbar        if (!OldParam->isParameterPack() ||
2026396b2a22788b0134018760d6a476de1e20f81334Daniel Dunbar            (NewIdx < NumNewParams &&
2027396b2a22788b0134018760d6a476de1e20f81334Daniel Dunbar             NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
2028cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          // Simple case: normal parameter, or a parameter pack that's
2029cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          // instantiated to a (still-dependent) parameter pack.
2030cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2031cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          Params.push_back(NewParam);
2032cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
2033cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                                               NewParam);
2034cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          continue;
2035cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
20366b6b5372f4b60b1c5ee101709e71a04642c835f4Chris Lattner
2037fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner        // Parameter pack: make the instantiation an argument pack.
2038bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump        SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2039bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                                                      OldParam);
2040fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner        unsigned NumArgumentsInExpansion
2041bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump          = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2042bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                               TemplateArgs);
2043bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump        while (NumArgumentsInExpansion--) {
20440b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2045fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          Params.push_back(NewParam);
2046fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2047fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner                                                                      NewParam);
2048fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner        }
2049fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner      }
20503c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    }
2051fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  } else {
2052fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    // The function type itself was not dependent and therefore no
2053fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    // substitution occurred. However, we still need to instantiate
2054fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    // the function parameters themselves.
2055fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
20560b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner    if (FunctionProtoTypeLoc *OldProtoLoc
2057fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2058fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2059210ae989a51dcb115b928829abd7c4c4ae0c01bdDaniel Dunbar        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
206001eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar        if (!Parm)
2061fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          return 0;
2062fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner        Params.push_back(Parm);
2063210ae989a51dcb115b928829abd7c4c4ae0c01bdDaniel Dunbar      }
2064210ae989a51dcb115b928829abd7c4c4ae0c01bdDaniel Dunbar    }
2065fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  }
2066fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  return NewTInfo;
2067fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner}
206873397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman
2069210ae989a51dcb115b928829abd7c4c4ae0c01bdDaniel Dunbar/// \brief Initializes the common fields of an instantiation function
2070fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner/// declaration (New) from the corresponding fields of its template (Tmpl).
207173397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman///
207273397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman/// \returns true if there was an error
207373397496fec250f565f49e27f8ba79f94f4e7427Eli Friedmanbool
207473397496fec250f565f49e27f8ba79f94f4e7427Eli FriedmanTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
207573397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman                                                    FunctionDecl *Tmpl) {
207673397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  if (Tmpl->isDeleted())
207773397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman    New->setDeleted();
207873397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman
207973397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // If we are performing substituting explicitly-specified template arguments
208073397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // or deduced template arguments into a function template and we reach this
208173397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // point, we are now past the point where SFINAE applies and have committed
208273397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // to keeping the new function template specialization. We therefore
208373397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // convert the active template instantiation for the function template
208473397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // into a template instantiation for this specific function template
208573397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // specialization, which is not a SFINAE context, so that we diagnose any
208673397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman  // further errors in the declaration itself.
2087fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2088fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2089fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2090fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
2091210ae989a51dcb115b928829abd7c4c4ae0c01bdDaniel Dunbar    if (FunctionTemplateDecl *FunTmpl
20920b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
2093210ae989a51dcb115b928829abd7c4c4ae0c01bdDaniel Dunbar      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
20940b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner             "Deduction from the wrong function template?");
2095fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner      (void) FunTmpl;
2096fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2097210ae989a51dcb115b928829abd7c4c4ae0c01bdDaniel Dunbar      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
20980b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner      --SemaRef.NonInstantiationEntries;
2099fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    }
2100fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  }
2101fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
2102fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2103fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  assert(Proto && "Function template without prototype?");
2104fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
2105fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
2106fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    // The function has an exception specification or a "noreturn"
2107fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    // attribute. Substitute into each of the exception types.
2108fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner    llvm::SmallVector<QualType, 4> Exceptions;
2109fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2110fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner      // FIXME: Poor location information!
2111fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner      if (const PackExpansionType *PackExpansion
211273397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman            = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2113183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        // We have a pack expansion. Instantiate it.
211473397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
211573397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
21162ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor                                                Unexpanded);
211773397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        assert(!Unexpanded.empty() &&
211873397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman               "Pack expansion without parameter packs?");
211973397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman
212073397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        bool Expand = false;
212173397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        bool RetainExpansion = false;
212273397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        llvm::Optional<unsigned> NumExpansions
212373397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman                                          = PackExpansion->getNumExpansions();
212473397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
212573397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman                                                    SourceRange(),
2126390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump                                                    Unexpanded.data(),
2127390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump                                                    Unexpanded.size(),
2128390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump                                                    TemplateArgs,
2129390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump                                                    Expand,
2130f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman                                                    RetainExpansion,
2131f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman                                                    NumExpansions))
2132fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          break;
2133fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
2134fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner        if (!Expand) {
21353c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner          // We can't expand this pack expansion into separate arguments yet;
2136fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          // just substitute into the pattern and create a new pack expansion
2137fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          // type.
21383c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2139fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2140fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner                                         TemplateArgs,
214173397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman                                       New->getLocation(), New->getDeclName());
214273397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman          if (T.isNull())
214373397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman            break;
214473397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman
2145fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
21460b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner          Exceptions.push_back(T);
2147fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          continue;
21480b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner        }
2149fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
2150fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner        // Substitute into the pack expansion pattern for each template
215173397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        bool Invalid = false;
215273397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman        for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
215373397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
215473397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman
2155fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
21560b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner                                         TemplateArgs,
2157fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner                                       New->getLocation(), New->getDeclName());
21580b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner          if (T.isNull()) {
2159fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner            Invalid = true;
2160fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner            break;
2161fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          }
21620b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner
2163fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          Exceptions.push_back(T);
21640b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner        }
2165fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
21660b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner        if (Invalid)
2167fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          break;
2168fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
2169fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner        continue;
21700b2f4da7651feb6edab5e4a657fce058f0dd514aChris Lattner      }
2171fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
2172aec7caa3c40891727164167ece11d552422803d2Chandler Carruth      QualType T
2173aec7caa3c40891727164167ece11d552422803d2Chandler Carruth        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2174aec7caa3c40891727164167ece11d552422803d2Chandler Carruth                            New->getLocation(), New->getDeclName());
2175aec7caa3c40891727164167ece11d552422803d2Chandler Carruth      if (T.isNull() ||
2176fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2177aec7caa3c40891727164167ece11d552422803d2Chandler Carruth        continue;
2178aec7caa3c40891727164167ece11d552422803d2Chandler Carruth
2179aec7caa3c40891727164167ece11d552422803d2Chandler Carruth      Exceptions.push_back(T);
2180aec7caa3c40891727164167ece11d552422803d2Chandler Carruth    }
2181fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    Expr *NoexceptExpr = 0;
218273397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman    if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
218373397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman      ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
218473397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman      if (E.isUsable())
2185f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman        NoexceptExpr = E.take();
2186f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    }
2187f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2188f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // Rebuild the function type
2189f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2190f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2191f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson    EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2192f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson    EPI.NumExceptions = Exceptions.size();
2193f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson    EPI.Exceptions = Exceptions.data();
219473397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman    EPI.NoexceptExpr = NoexceptExpr;
2195fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    EPI.ExtInfo = Proto->getExtInfo();
2196fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
219773397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman    const FunctionProtoType *NewProto
219873397496fec250f565f49e27f8ba79f94f4e7427Eli Friedman      = New->getType()->getAs<FunctionProtoType>();
2199fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    assert(NewProto && "Template instantiation without function prototype?");
2200fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2201fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner                                                 NewProto->arg_type_begin(),
2202ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                                                 NewProto->getNumArgs(),
2203ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                                                 EPI));
2204a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  }
2205ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
2206fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner  SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
2207fbf1347e1e225cbc206563bba3f0a75f9ceaa571Chris Lattner
22080744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner  return false;
22091feade8e520be483293dbf55eb57a51720899589Mike Stump}
2210d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson
2211d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson/// \brief Initializes common fields of an instantiated method
2212d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson/// declaration (New) from the corresponding fields of its template
2213d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson/// (Tmpl).
2214d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson///
2215e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson/// \returns true if there was an error
22165bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlssonbool
2217d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders CarlssonTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
2218883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                                                  CXXMethodDecl *Tmpl) {
2219d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson  if (InitFunctionInstantiation(New, Tmpl))
2220d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson    return true;
2221bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
2222cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  New->setAccess(Tmpl->getAccess());
2223d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson  if (Tmpl->isVirtualAsWritten())
2224d87df37e0adaba0d5e33da7b1a14d7f1d94c5eefAnders Carlsson    New->setVirtualAsWritten(true);
22251feade8e520be483293dbf55eb57a51720899589Mike Stump
22265bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson  // FIXME: attributes
22275bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson  // FIXME: New needs a pointer to Tmpl
22285bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson  return false;
22295bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson}
22305bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson
2231bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump/// \brief Instantiate the definition of the given function from its
2232c51974328b3a378c3c40b1fa527ecb928ed2bfdaChris Lattner/// template.
22335bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson///
2234883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall/// \param PointOfInstantiation the point at which the instantiation was
22355bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson/// required. Note that this is not precisely a "point of instantiation"
22365bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson/// for the function, but it's close.
2237bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump///
2238cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt/// \param Function the already-instantiated declaration of a
22395bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson/// function template specialization or member function of a class template
22405bab788d40026ad6e932a3cd9b86bc13f8a27661Anders Carlsson/// specialization.
22417255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner///
22427255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// \param Recursive if true, recursively instantiates any functions that
22437255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// are required by this instantiation.
22447255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner///
22457255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// \param DefinitionRequired if true, then we are performing an explicit
22467255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// instantiation where the body of the function is required. Complain if
22477255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner/// there is no such body.
22487255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnervoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
22497255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner                                         FunctionDecl *Function,
22507255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner                                         bool Recursive,
2251883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall                                         bool DefinitionRequired) {
22527255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  if (Function->isInvalidDecl() || Function->hasBody())
22537255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return;
22547255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2255f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher  // Never instantiate an explicit specialization.
2256f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
22577255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner    return;
22587255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
2259ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // Find the function body that we'll be substituting.
2260ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
2261ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  Stmt *Pattern = 0;
2262ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  if (PatternDecl)
2263ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    Pattern = PatternDecl->getBody(PatternDecl);
2264ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2265ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // Postpone late parsed template instantiations.
2266ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  if (PatternDecl->isLateTemplateParsed() && !LateTemplateParser) {
2267ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    PendingInstantiations.push_back(
2268ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      std::make_pair(Function, PointOfInstantiation));
2269883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    return;
2270ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  }
2271ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2272ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // Call the LateTemplateParser callback if there a need to late parse
2273ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // a templated function definition.
2274ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  if (!Pattern && PatternDecl && PatternDecl->isLateTemplateParsed() &&
2275ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      LateTemplateParser) {
2276ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    LateTemplateParser(OpaqueParser, (FunctionDecl*)PatternDecl);
2277ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    Pattern = PatternDecl->getBody(PatternDecl);
2278ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  }
2279ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2280ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  if (!Pattern) {
2281ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    if (DefinitionRequired) {
2282ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      if (Function->getPrimaryTemplate())
2283ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne        Diag(PointOfInstantiation,
2284ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne             diag::err_explicit_instantiation_undefined_func_template)
2285ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne          << Function->getPrimaryTemplate();
2286ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      else
2287ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne        Diag(PointOfInstantiation,
2288ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne             diag::err_explicit_instantiation_undefined_member)
2289883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall          << 1 << Function->getDeclName() << Function->getDeclContext();
2290ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2291ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      if (PatternDecl)
2292ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne        Diag(PatternDecl->getLocation(),
2293ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne             diag::note_explicit_instantiation_here);
2294ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      Function->setInvalidDecl();
2295ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    } else if (Function->getTemplateSpecializationKind()
2296ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne                 == TSK_ExplicitInstantiationDefinition) {
2297ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      PendingInstantiations.push_back(
2298ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne        std::make_pair(Function, PointOfInstantiation));
2299ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    }
2300ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2301ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    return;
2302ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  }
2303ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2304ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // C++0x [temp.explicit]p9:
2305ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  //   Except for inline functions, other explicit instantiation declarations
2306ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  //   have the effect of suppressing the implicit instantiation of the entity
2307ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  //   to which they refer.
2308ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  if (Function->getTemplateSpecializationKind()
2309883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall        == TSK_ExplicitInstantiationDeclaration &&
2310ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      !PatternDecl->isInlined())
2311ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    return;
2312ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
23132c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
23142c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  if (Inst)
2315723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara    return;
23162c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne
23172c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  // If we're performing recursive template instantiation, create our own
23182c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  // queue of pending implicit instantiations that we will instantiate later,
23192c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  // while we're still within our own instantiation context.
23202c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  llvm::SmallVector<VTableUse, 16> SavedVTableUses;
23212c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
23222c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  if (Recursive) {
23232c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne    VTableUses.swap(SavedVTableUses);
23242c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne    PendingInstantiations.swap(SavedPendingInstantiations);
23252c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  }
23262c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne
23272c2c8dd0acb2a51067299bfcec9ff2145f2031c8Peter Collingbourne  EnterExpressionEvaluationContext EvalContext(*this,
2328ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne                                               Sema::PotentiallyEvaluated);
2329ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  ActOnStartOfFunctionDef(0, Function);
2330ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2331ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // Introduce a new scope where local variable instantiations will be
2332ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // recorded, unless we're actually a member function within a local
2333ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // class, in which case we need to merge our results with the parent
2334ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // scope (of the enclosing function).
2335ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  bool MergeWithParentScope = false;
2336ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2337ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    MergeWithParentScope = Rec->isLocalClass();
2338ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2339ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  LocalInstantiationScope Scope(*this, MergeWithParentScope);
2340ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2341ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // Introduce the instantiated function parameters into the local
2342ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // instantiation scope, and set the parameter names to those used
2343ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // in the template.
2344883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  unsigned FParamIdx = 0;
2345ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2346ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2347ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    if (!PatternParam->isParameterPack()) {
2348ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      // Simple case: not a parameter pack.
2349ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      assert(FParamIdx < Function->getNumParams());
2350ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2351ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      FunctionParam->setDeclName(PatternParam->getDeclName());
2352ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      Scope.InstantiatedLocal(PatternParam, FunctionParam);
2353ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      ++FParamIdx;
2354ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      continue;
2355ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    }
2356ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2357ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    // Expand the parameter pack.
2358ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2359ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    for (unsigned NumFParams = Function->getNumParams();
2360ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne         FParamIdx < NumFParams;
2361ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne         ++FParamIdx) {
2362ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2363ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne      FunctionParam->setDeclName(PatternParam->getDeclName());
2364883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2365ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    }
2366ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  }
2367ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2368ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // Enter the scope of this instantiation. We don't use
2369ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  // PushDeclContext because we don't have a scope.
2370ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  Sema::ContextRAII savedContext(*this, Function);
2371ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
2372ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne  MultiLevelTemplateArgumentList TemplateArgs =
2373ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2374cf2a7211b4785068c7efa836baab90b198a4d2a6Chris Lattner
237526e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner  // If this is a constructor, instantiate the member initializers.
237626e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner  if (const CXXConstructorDecl *Ctor =
237726e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
237826e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
237926e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner                               TemplateArgs);
2380bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  }
2381c51974328b3a378c3c40b1fa527ecb928ed2bfdaChris Lattner
2382c51974328b3a378c3c40b1fa527ecb928ed2bfdaChris Lattner  // Instantiate the function body.
238326e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner  StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2384883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
238526e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner  if (Body.isInvalid())
238626e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner    Function->setInvalidDecl();
2387bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
23880130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor  ActOnFinishFunctionBody(Function, Body.get(),
2389cf2a7211b4785068c7efa836baab90b198a4d2a6Chris Lattner                          /*IsInstantiation=*/true);
2390c51974328b3a378c3c40b1fa527ecb928ed2bfdaChris Lattner
2391c51974328b3a378c3c40b1fa527ecb928ed2bfdaChris Lattner  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2392bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
2393cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  savedContext.pop();
239426e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner
239526e25545b26ec06f5d674dbae00fb168e6688d90Chris Lattner  DeclGroupRef DG(Function);
2396711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Consumer.HandleTopLevelDecl(DG);
2397711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2398711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // This class may have local implicit instantiations that need to be
2399711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // instantiation within this scope.
2400e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  PerformPendingInstantiations(/*LocalOnly=*/true);
2401711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  Scope.Exit();
2402711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2403711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (Recursive) {
2404e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Define any pending vtables.
2405711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DefineUsedVTables();
2406711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2407883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    // Instantiate any pending implicit instantiations found during the
2408711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // instantiation of this template.
2409711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    PerformPendingInstantiations();
2410711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2411711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // Restore the set of pending vtables.
2412e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    VTableUses.swap(SavedVTableUses);
2413711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2414e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    // Restore the set of pending implicit instantiations.
2415e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara    PendingInstantiations.swap(SavedPendingInstantiations);
2416711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
2417e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara}
2418f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor
2419711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \brief Instantiate the definition of the given variable from its
242004633eb86621747bece5643f5909222e2dd6884fDouglas Gregor/// template.
2421e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara///
2422711c52bb20d0c69063b52a99826fb7d2835501f1John McCall/// \param PointOfInstantiation the point at which the instantiation was
2423e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara/// required. Note that this is not precisely a "point of instantiation"
242452fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik/// for the function, but it's close.
2425711c52bb20d0c69063b52a99826fb7d2835501f1John McCall///
242652fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik/// \param Var the already-instantiated declaration of a static member
2427e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara/// variable of a class template specialization.
2428e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara///
2429e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara/// \param Recursive if true, recursively instantiates any functions that
2430e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara/// are required by this instantiation.
2431e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara///
2432e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara/// \param DefinitionRequired if true, then we are performing an explicit
2433f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne/// instantiation where an out-of-line definition of the member variable
2434f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne/// is required. Complain if there is no such definition.
2435f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbournevoid Sema::InstantiateStaticDataMemberDefinition(
2436f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne                                          SourceLocation PointOfInstantiation,
2437f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne                                                 VarDecl *Var,
2438711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                                 bool Recursive,
2439711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                                 bool DefinitionRequired) {
2440711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (Var->isInvalidDecl())
2441711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
2442711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2443711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Find the out-of-line definition of this static data member.
2444711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
2445711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  assert(Def && "This data member was not instantiated from a template?");
2446ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian  assert(Def->isStaticDataMember() && "Not a static data member?");
244755d3aaf9a537888734762170823daf750ea9036dEli Friedman  Def = Def->getOutOfLineDefinition();
2448711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2449711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (!Def) {
2450711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // We did not find an out-of-line definition of this static data member,
2451711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // so we won't perform any instantiation. Rather, we rely on the user to
2452711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // instantiate this definition (or provide a specialization for it) in
2453711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    // another translation unit.
2454711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    if (DefinitionRequired) {
2455711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      Def = Var->getInstantiatedFromStaticDataMember();
2456711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      Diag(PointOfInstantiation,
2457711c52bb20d0c69063b52a99826fb7d2835501f1John McCall           diag::err_explicit_instantiation_undefined_member)
2458711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        << 2 << Var->getDeclName() << Var->getDeclContext();
2459711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2460711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    } else if (Var->getTemplateSpecializationKind()
2461711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                 == TSK_ExplicitInstantiationDefinition) {
2462711c52bb20d0c69063b52a99826fb7d2835501f1John McCall      PendingInstantiations.push_back(
2463711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        std::make_pair(Var, PointOfInstantiation));
2464711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    }
2465711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2466711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
2467711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
2468711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2469711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Never instantiate an explicit specialization.
2470883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2471ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian    return;
2472ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian
247355d3aaf9a537888734762170823daf750ea9036dEli Friedman  // C++0x [temp.explicit]p9:
2474711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  //   Except for inline functions, other explicit instantiation declarations
2475711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  //   have the effect of suppressing the implicit instantiation of the entity
2476711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  //   to which they refer.
2477711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (Var->getTemplateSpecializationKind()
2478711c52bb20d0c69063b52a99826fb7d2835501f1John McCall        == TSK_ExplicitInstantiationDeclaration)
2479711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
2480711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2481711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2482711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (Inst)
2483711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    return;
2484711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2485711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // If we're performing recursive template instantiation, create our own
2486711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // queue of pending implicit instantiations that we will instantiate later,
2487711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // while we're still within our own instantiation context.
2488711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
2489711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (Recursive)
249055d3aaf9a537888734762170823daf750ea9036dEli Friedman    PendingInstantiations.swap(SavedPendingInstantiations);
2491ac06a0e1e3feb95c2ffd352c086882b492a65b99Douglas Gregor
2492711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // Enter the scope of this instantiation. We don't use
2493711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  // PushDeclContext because we don't have a scope.
249455d3aaf9a537888734762170823daf750ea9036dEli Friedman  ContextRAII previousContext(*this, Var->getDeclContext());
2495711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2496711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  VarDecl *OldVar = Var;
249755d3aaf9a537888734762170823daf750ea9036dEli Friedman  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
249855d3aaf9a537888734762170823daf750ea9036dEli Friedman                                        getTemplateInstantiationArgs(Var)));
2499711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2500711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  previousContext.pop();
250155d3aaf9a537888734762170823daf750ea9036dEli Friedman
2502711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (Var) {
2503711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
250455d3aaf9a537888734762170823daf750ea9036dEli Friedman    assert(MSInfo && "Missing member specialization information?");
250555d3aaf9a537888734762170823daf750ea9036dEli Friedman    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2506711c52bb20d0c69063b52a99826fb7d2835501f1John McCall                                       MSInfo->getPointOfInstantiation());
2507711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    DeclGroupRef DG(Var);
2508711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    Consumer.HandleTopLevelDecl(DG);
2509711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  }
2510711c52bb20d0c69063b52a99826fb7d2835501f1John McCall
2511711c52bb20d0c69063b52a99826fb7d2835501f1John McCall  if (Recursive) {
251255d3aaf9a537888734762170823daf750ea9036dEli Friedman    // Instantiate any pending implicit instantiations found during the
251355d3aaf9a537888734762170823daf750ea9036dEli Friedman    // instantiation of this template.
2514711c52bb20d0c69063b52a99826fb7d2835501f1John McCall    PerformPendingInstantiations();
2515ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian
2516ee760330a415635369556796a97afcfd6207f4dcFariborz Jahanian    // Restore the set of pending implicit instantiations.
25177b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    PendingInstantiations.swap(SavedPendingInstantiations);
25187b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne  }
25197b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne}
25207b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne
2521bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCallvoid
2522bdc49d360f98c1194d50b8bbb24885bf8d4c1ac4John McCallSema::InstantiateMemInitializers(CXXConstructorDecl *New,
25237b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                 const CXXConstructorDecl *Tmpl,
25247b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                           const MultiLevelTemplateArgumentList &TemplateArgs) {
25257b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne
25267b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne  llvm::SmallVector<MemInitTy*, 4> NewInits;
25277b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne  bool AnyErrors = false;
2528883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall
25297b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne  // Instantiate all the initializers.
25307b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
25317b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                            InitsEnd = Tmpl->init_end();
25327b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne       Inits != InitsEnd; ++Inits) {
25337b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    CXXCtorInitializer *Init = *Inits;
25347b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne
25357b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    // Only instantiate written initializers, let Sema re-construct implicit
25367b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    // ones.
25377b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    if (!Init->isWritten())
25387b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      continue;
25397b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne
25407b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    SourceLocation LParenLoc, RParenLoc;
25417b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    ASTOwningVector<Expr*> NewArgs(*this);
25427b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne
25437b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    SourceLocation EllipsisLoc;
25447b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne
25457b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne    if (Init->isPackExpansion()) {
25467b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      // This is a pack expansion. We should expand it now.
25477b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
25487b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
25497b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
25507b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      bool ShouldExpand = false;
25517b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      bool RetainExpansion = false;
25527b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      llvm::Optional<unsigned> NumExpansions;
25537b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
25547b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                          BaseTL.getSourceRange(),
25557b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                          Unexpanded.data(),
25567b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                          Unexpanded.size(),
25577b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                          TemplateArgs, ShouldExpand,
25587b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                          RetainExpansion,
25597b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne                                          NumExpansions)) {
25607b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne        AnyErrors = true;
25617b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne        New->setInvalidDecl();
25620744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner        continue;
2563b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      }
2564b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      assert(ShouldExpand && "Partial instantiation of base initializer?");
2565b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek
2566c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      // Loop over all of the arguments in the argument pack(s),
2567c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      for (unsigned I = 0; I != *NumExpansions; ++I) {
2568c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2569c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2570c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        // Instantiate the initializer.
2571c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2572c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                   LParenLoc, NewArgs, RParenLoc)) {
2573c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall          AnyErrors = true;
2574c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall          break;
2575c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        }
2576c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2577883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall        // Instantiate the base type.
2578c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2579c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                              TemplateArgs,
2580c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                              Init->getSourceLocation(),
2581c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                              New->getDeclName());
2582c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        if (!BaseTInfo) {
2583c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall          AnyErrors = true;
2584c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall          break;
2585c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        }
2586c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2587c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        // Build the initializer.
2588c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2589c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     BaseTInfo,
2590c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     (Expr **)NewArgs.data(),
2591c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     NewArgs.size(),
2592c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     Init->getLParenLoc(),
2593c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     Init->getRParenLoc(),
2594c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     New->getParent(),
2595c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     SourceLocation());
2596c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        if (NewInit.isInvalid()) {
2597c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall          AnyErrors = true;
2598c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall          break;
2599c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        }
2600c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2601c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        NewInits.push_back(NewInit.get());
2602c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        NewArgs.clear();
2603c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      }
2604c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2605c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      continue;
2606883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall    }
2607c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2608c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    // Instantiate the initializer.
2609c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2610c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                               LParenLoc, NewArgs, RParenLoc)) {
2611c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      AnyErrors = true;
2612c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      continue;
2613c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    }
2614b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek
2615b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek    MemInitResult NewInit;
2616c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    if (Init->isBaseInitializer()) {
2617bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
26185dc53c9c2328b5bea5422005b04960c18afd83adTed Kremenek                                            TemplateArgs,
2619c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                            Init->getSourceLocation(),
26205dc53c9c2328b5bea5422005b04960c18afd83adTed Kremenek                                            New->getDeclName());
2621c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      if (!BaseTInfo) {
26225dc53c9c2328b5bea5422005b04960c18afd83adTed Kremenek        AnyErrors = true;
262321531fa592cd76e5d3df839ce469bea918404ac8Ted Kremenek        New->setInvalidDecl();
2624c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        continue;
2625883cc2ca5ff06d7f1d89a9ab24a2da37f095243bJohn McCall      }
2626b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek
2627b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
2628bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                     (Expr **)NewArgs.data(),
2629c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                     NewArgs.size(),
2630c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                     Init->getLParenLoc(),
2631c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                     Init->getRParenLoc(),
2632c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                     New->getParent(),
2633c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                     EllipsisLoc);
2634c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    } else if (Init->isMemberInitializer()) {
2635c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
2636c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     Init->getMemberLocation(),
2637c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     Init->getMember(),
2638c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                                     TemplateArgs));
2639c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      if (!Member) {
2640c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        AnyErrors = true;
2641c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        New->setInvalidDecl();
2642c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        continue;
2643c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      }
2644c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2645c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2646c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                       NewArgs.size(),
2647c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                       Init->getSourceLocation(),
264821531fa592cd76e5d3df839ce469bea918404ac8Ted Kremenek                                       Init->getLParenLoc(),
2649c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                       Init->getRParenLoc());
2650c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    } else if (Init->isIndirectMemberInitializer()) {
2651bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      IndirectFieldDecl *IndirectMember =
26525dc53c9c2328b5bea5422005b04960c18afd83adTed Kremenek         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
2653bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                                 Init->getMemberLocation(),
2654c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                 Init->getIndirectMember(), TemplateArgs));
2655b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek
2656b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      if (!IndirectMember) {
2657b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek        AnyErrors = true;
2658c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        New->setInvalidDecl();
2659c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        continue;
2660c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      }
2661c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
266231c780d95a830f0187cfcbb1016ce88f50a7dfe1Ted Kremenek      NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2663c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall                                       NewArgs.size(),
2664f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher                                       Init->getSourceLocation(),
266531c780d95a830f0187cfcbb1016ce88f50a7dfe1Ted Kremenek                                       Init->getLParenLoc(),
266631c780d95a830f0187cfcbb1016ce88f50a7dfe1Ted Kremenek                                       Init->getRParenLoc());
2667c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    }
2668f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher
266931c780d95a830f0187cfcbb1016ce88f50a7dfe1Ted Kremenek    if (NewInit.isInvalid()) {
2670b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      AnyErrors = true;
2671c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      New->setInvalidDecl();
2672f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher    } else {
2673b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      // FIXME: It would be nice if ASTOwningVector had a release function.
2674b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      NewArgs.take();
2675c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2676f48f367cfe096fd307d36aff27d2d5a00e830571Eric Christopher      NewInits.push_back((MemInitTy *)NewInit.get());
2677b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek    }
2678b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek  }
2679b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek
2680b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek  // Assign all the initializers to the new constructor.
2681f0122fe49329cb439d55a6712bfcaad9a6570428Charles Davis  ActOnMemInitializers(New,
2682f0122fe49329cb439d55a6712bfcaad9a6570428Charles Davis                       /*FIXME: ColonLoc */
268311542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet                       SourceLocation(),
268411542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet                       NewInits.data(), NewInits.size(),
268511542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet                       AnyErrors);
268611542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet}
268711542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet
268811542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet// TODO: this could be templated if the various decl types used the
268911542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet// same method name.
269011542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichetstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
269111542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet                              ClassTemplateDecl *Instance) {
269211542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet  Pattern = Pattern->getCanonicalDecl();
269311542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet
269411542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet  do {
269511542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet    Instance = Instance->getCanonicalDecl();
269611542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet    if (Pattern == Instance) return true;
269711542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet    Instance = Instance->getInstantiatedFromMemberTemplate();
269811542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet  } while (Instance);
269911542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet
2700d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  return false;
2701d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet}
2702d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet
2703d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichetstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2704d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet                              FunctionTemplateDecl *Instance) {
2705d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  Pattern = Pattern->getCanonicalDecl();
2706d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet
2707d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  do {
2708d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet    Instance = Instance->getCanonicalDecl();
2709d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet    if (Pattern == Instance) return true;
2710d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet    Instance = Instance->getInstantiatedFromMemberTemplate();
2711d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  } while (Instance);
2712d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet
2713d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  return false;
2714d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet}
2715d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet
2716d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichetstatic bool
2717d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois PichetisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2718d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet                  ClassTemplatePartialSpecializationDecl *Instance) {
2719d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  Pattern
2720d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2721d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  do {
2722d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2723f89e0424b8903438179f4a2f16dddd5e5bdc814eAnders Carlsson                                                Instance->getCanonicalDecl());
2724f89e0424b8903438179f4a2f16dddd5e5bdc814eAnders Carlsson    if (Pattern == Instance)
2725f89e0424b8903438179f4a2f16dddd5e5bdc814eAnders Carlsson      return true;
2726f89e0424b8903438179f4a2f16dddd5e5bdc814eAnders Carlsson    Instance = Instance->getInstantiatedFromMember();
2727f89e0424b8903438179f4a2f16dddd5e5bdc814eAnders Carlsson  } while (Instance);
2728d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet
2729d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  return false;
2730d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet}
2731d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet
2732d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichetstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
2733d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet                              CXXRecordDecl *Instance) {
2734d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  Pattern = Pattern->getCanonicalDecl();
2735d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet
2736d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  do {
2737d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet    Instance = Instance->getCanonicalDecl();
2738d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet    if (Pattern == Instance) return true;
273911542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet    Instance = Instance->getInstantiatedFromMemberClass();
274011542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet  } while (Instance);
274111542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet
2742d3d3be9bc717b37366324e9711f1ea22dea42caaFrancois Pichet  return false;
274311542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet}
2744f0122fe49329cb439d55a6712bfcaad9a6570428Charles Davis
2745f0122fe49329cb439d55a6712bfcaad9a6570428Charles Davisstatic bool isInstantiationOf(FunctionDecl *Pattern,
2746b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek                              FunctionDecl *Instance) {
27470744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner  Pattern = Pattern->getCanonicalDecl();
27480744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner
27490744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner  do {
275060700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne    Instance = Instance->getCanonicalDecl();
275160700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne    if (Pattern == Instance) return true;
275260700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne    Instance = Instance->getInstantiatedFromMemberFunction();
275360700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne  } while (Instance);
275460700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne
275560700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne  return false;
275660700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne}
275760700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne
275860700390a787471d3396f380e0679a6d08c27f1fPeter Collingbournestatic bool isInstantiationOf(EnumDecl *Pattern,
275960700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne                              EnumDecl *Instance) {
2760e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  Pattern = Pattern->getCanonicalDecl();
276160700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne
276260700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne  do {
2763803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner    Instance = Instance->getCanonicalDecl();
276463e5d7c85299134f088033614afd9eb213c50b48Ted Kremenek    if (Pattern == Instance) return true;
2765857e918a8a40deb128840308a318bf623d68295fTed Kremenek    Instance = Instance->getInstantiatedFromMemberEnum();
2766857e918a8a40deb128840308a318bf623d68295fTed Kremenek  } while (Instance);
2767857e918a8a40deb128840308a318bf623d68295fTed Kremenek
2768803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner  return false;
2769207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
2770ba372b85524f712e5b97a176f6ce0197d365835dFariborz Jahanian
27716e132aab867c189b1c3ee7463ef9d2b1f03a294dJohn Thompsonstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
27724211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                              UsingShadowDecl *Instance,
27734211bb68cff1f310be280f66a59520548ef99d8fBob Wilson                              ASTContext &C) {
2774bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2775bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump}
2776803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner
277760700390a787471d3396f380e0679a6d08c27f1fPeter Collingbournestatic bool isInstantiationOf(UsingDecl *Pattern,
277860700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne                              UsingDecl *Instance,
277960700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne                              ASTContext &C) {
278060700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
278160700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne}
278260700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne
27837725e67639fa2fe74f8775b7ed884a076ffdbffcSean Huntstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
27847725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt                              UsingDecl *Instance,
2785bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump                              ASTContext &C) {
2786af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2787b725232b46e92f3e36b03a32a6fc75748c312122Ted Kremenek}
2788bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump
27897725e67639fa2fe74f8775b7ed884a076ffdbffcSean Huntstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
27900a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor                              UsingDecl *Instance,
2791bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt                              ASTContext &C) {
27927725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2793a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher}
2794ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
27957725e67639fa2fe74f8775b7ed884a076ffdbffcSean Huntstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
27967725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt                                              VarDecl *Instance) {
27977725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  assert(Instance->isStaticDataMember());
27983068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar
27999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
28003068ae0feb5d477477f45045f7ec9d0414fe57f3Daniel Dunbar
28017725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  do {
28027725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    Instance = Instance->getCanonicalDecl();
2803ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne    if (Pattern == Instance) return true;
28047725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    Instance = Instance->getInstantiatedFromStaticDataMember();
28057b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne  } while (Instance);
28067b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne
28077b381985353304a7723acb05911ff91634fa1f27Peter Collingbourne  return false;
28087725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt}
28097725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt
281034c26300b384286c544e0b9fd45e7a3648ac79e3Dan Gohman// Other is the prospective instantiation
2811a6cf1e709b96865210b81bd611d41e9a2d41500aEric Christopher// D is the prospective pattern
28127725e67639fa2fe74f8775b7ed884a076ffdbffcSean Huntstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
2813dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek  if (D->getKind() != Other->getKind()) {
2814dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    if (UnresolvedUsingTypenameDecl *UUD
2815dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2816dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2817dd0cb22bd62e1e835327f478a2dbf0b8fa439713Daniel Dunbar        return isInstantiationOf(UUD, UD, Ctx);
28187725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt      }
28197725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    }
2820ced7671c18e115ac3c3f54abfaaafcc6d33edc4cPeter Collingbourne
282135cc9627340b15232139b3c43fcde5973e7fad30John Thompson    if (UnresolvedUsingValueDecl *UUD
2822b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
2823b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2824c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall        return isInstantiationOf(UUD, UD, Ctx);
2825c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall      }
2826c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    }
2827c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall
2828c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall    return false;
2829c7ad38168d329d778e884a8b6400bcbed8dc85eeJohn McCall  }
283031c780d95a830f0187cfcbb1016ce88f50a7dfe1Ted Kremenek
283131c780d95a830f0187cfcbb1016ce88f50a7dfe1Ted Kremenek  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2832b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
2833b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek
2834b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2835b71368d28532908ae1c2dc23f91761781205b3d0Ted Kremenek    return isInstantiationOf(cast<FunctionDecl>(D), Function);
28366f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman
28376f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
28386f3d838867538638b9bbf412028e8537ae12f3e5Nate Begeman    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2839521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian
2840521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
2841521f12d3dfdbb0e93d1bcb503d074e67acdc489cFariborz Jahanian    if (Var->isStaticDataMember())
28427725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
28437725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt
28447725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
28457725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
28467725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt
28477725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2848026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2849026dc96ac6ece60da5e1b98e2a71bd0ff0939fd8Chris Lattner
28507725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  if (ClassTemplatePartialSpecializationDecl *PartialSpec
285111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
28527725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2853803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner                             PartialSpec);
2854803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner
2855803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
28560db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner    if (!Field->getDeclName()) {
28570db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner      // This is an unnamed field.
28580db29ece81d360dcefbe912339c34abe5917f6a9Chris Lattner      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2859d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall        cast<FieldDecl>(D);
2860d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall    }
2861d5313b0bbf3948fe7c63bf46a7da330c96d07309John McCall  }
28627725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt
28637725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
28647725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
28657725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt
28667725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
28677725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
28687725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt
28697725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt  return D->getDeclName() && isa<NamedDecl>(Other) &&
28707725e67639fa2fe74f8775b7ed884a076ffdbffcSean Hunt    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2871bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump}
287205f8e471aae971c9867dbac148eba1275a570814Anders Carlsson
287305f8e471aae971c9867dbac148eba1275a570814Anders Carlssontemplate<typename ForwardIterator>
28747255a2d997b15beae82e627052fdb1b2474495c2Chris Lattnerstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
28757255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner                                      NamedDecl *D,
28767255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner                                      ForwardIterator first,
287704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall                                      ForwardIterator last) {
287804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  for (; first != last; ++first)
287904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    if (isInstantiationOf(Ctx, D, *first))
2880f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor      return cast<NamedDecl>(*first);
288152fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik
2882e215f7232dd4aa65ebf2a1ecd07cd95fe1ce3481Abramo Bagnara  return 0;
288304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
2884f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne
2885f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne/// \brief Finds the instantiation of the given declaration context
2886f315fa81eef1977b3457fd7a7d4639e060fe7278Peter Collingbourne/// within the current instantiation.
288711542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet///
288811542141e385859df6b4f1a8f1f01856ad193b5bFrancois Pichet/// \returns NULL if there was an error
288911542141e385859df6b4f1a8f1f01856ad193b5bFrancois PichetDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2890803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner                          const MultiLevelTemplateArgumentList &TemplateArgs) {
289182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
289282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
289382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return cast_or_null<DeclContext>(ID);
28947d5c45ed9dc2842ce8e65ea26ced0957be36a569Chandler Carruth  } else return DC;
28957d5c45ed9dc2842ce8e65ea26ced0957be36a569Chandler Carruth}
2896803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner
2897803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner/// \brief Find the instantiation of the given declaration within the
2898803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner/// current instantiation.
2899803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner///
290060700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// This routine is intended to be used when \p D is a declaration
290160700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// referenced from within a template, that needs to mapped into the
290260700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// corresponding declaration within an instantiation. For example,
290360700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// given:
290460700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///
290560700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// \code
290660700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// template<typename T>
290760700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// struct X {
290860700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///   enum Kind {
290960700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///     KnownValue = sizeof(T)
291060700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///   };
291160700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///
291260700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///   bool getKind() const { return KnownValue; }
291360700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// };
291460700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///
291560700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// template struct X<int>;
291660700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// \endcode
291760700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne///
291860700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// In the instantiation of X<int>::getKind(), we need to map the
291960700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// EnumConstantDecl for KnownValue (which refers to
292060700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne/// X<T>::<Kind>::KnownValue) to its instantiation
2921803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2922803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner/// this mapping from within the instantiation of X<int>.
2923f48f367cfe096fd307d36aff27d2d5a00e830571Eric ChristopherNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
292460700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne                          const MultiLevelTemplateArgumentList &TemplateArgs) {
292560700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne  DeclContext *ParentDC = D->getDeclContext();
292611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
292760700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
292811e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
292911e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    // D is a local of some kind. Look into the map of local
293011e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    // declarations to their instantiations.
293111e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
293211e8ce7380856abee188b237c2600272df2ed09dRafael Espindola    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
293360700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne      = CurrentInstantiationScope->findInstantiationOf(D);
293411e8ce7380856abee188b237c2600272df2ed09dRafael Espindola
2935dd0e490c24aeade2c59ca4cae171199f6af9f02eTed Kremenek    if (Found) {
293611e8ce7380856abee188b237c2600272df2ed09dRafael Espindola      if (Decl *FD = Found->dyn_cast<Decl *>())
2937803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner        return cast<NamedDecl>(FD);
2938803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner
2939803d08039c5194cf51071ed1d8fbc5b18b3ec38bChris Lattner      unsigned PackIdx = ArgumentPackSubstitutionIndex;
2940e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
2941e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    }
29421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29437b1fdbda2757cc4a7f25664475be44119d7f8e59Ryan Flynn    // If we didn't find the decl, then we must have a label decl that hasn't
2944e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    // been found yet.  Lazily instantiate it and return it now.
2945e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    assert(isa<LabelDecl>(D));
2946e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
2947ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
2948e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    assert(Inst && "Failed to instantiate label??");
2949a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall
2950b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
2951b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return cast<LabelDecl>(Inst);
2952c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  }
2953b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2954e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2955e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    if (!Record->isDependentContext())
2956ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      return D;
2957a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall
295816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
295916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor    // "templated" declaration for a class template, class template
2960b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // partial specialization, or a member class of a class template,
2961b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // substitute into the injected-class-name of the class template
2962c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    // or partial specialization to find the new DeclContext.
2963b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    QualType T;
2964e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2965e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn
2966e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    if (ClassTemplate) {
2967e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn      T = ClassTemplate->getInjectedClassNameSpecialization();
2968e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2969e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
29707b1fdbda2757cc4a7f25664475be44119d7f8e59Ryan Flynn      ClassTemplate = PartialSpec->getSpecializedTemplate();
2971c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner
2972c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // If we call SubstType with an InjectedClassNameType here we
2973c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // can end up in an infinite loop.
2974c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      T = Context.getTypeDeclType(Record);
2975c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      assert(isa<InjectedClassNameType>(T) &&
2976cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt             "type of partial specialization is not an InjectedClassNameType");
2977cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2978cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    }
2979c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner
2980c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner    if (!T.isNull()) {
2981c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // Substitute into the injected-class-name to get the type
2982c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // corresponding to the instantiation we want, which may also be
2983c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // the current instantiation (if we're in a template
2984c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // definition). This substitution should never fail, since we
2985c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // know we can instantiate the injected-class-name or we
2986c4f1fb125d4fe2c8879030d6f6e8b2f75cb681f1Chris Lattner      // wouldn't have gotten to the injected-class-name!
2987cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
2988e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn      // FIXME: Can we use the CurrentInstantiationScope to avoid this
2989e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn      // extra instantiation in the common case?
2990e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn      T = SubstType(T, TemplateArgs, Loc, DeclarationName());
29910744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
29920744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner
29930744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      if (!T->isDependentType()) {
299460700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne        assert(T->isRecordType() && "Instantiation must produce a record type");
299560700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne        return T->getAs<RecordType>()->getDecl();
2996d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      }
2997d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall
299860700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne      // We are performing "partial" template instantiation to create
2999d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      // the member declarations for the members of a class template
3000d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      // specialization. Therefore, D is actually referring to something
3001d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      // in the current instantiation. Look through the current
3002d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      // context, which contains actual instantiations, to find the
3003d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      // instantiation of the "current instantiation" that D refers
3004d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      // to.
3005d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      bool SawNonDependentContext = false;
3006d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall      for (DeclContext *DC = CurContext; !DC->isFileContext();
3007d4aff0e2b77879e27e7e4eac8c972aaaa293fa12John McCall           DC = DC->getParent()) {
3008e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn        if (ClassTemplateSpecializationDecl *Spec
3009e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
3010e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn          if (isInstantiationOf(ClassTemplate,
3011e25ff83fb7eee9eeda89b6f2371bc33a37bf1028Ryan Flynn                                Spec->getSpecializedTemplate()))
30120744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner            return Spec;
30137f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall
301460700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne        if (!DC->isDependentContext())
3015bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump          SawNonDependentContext = true;
30160744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      }
30170744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner
30180744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      // We're performing "instantiation" of a member of the current
30190744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      // instantiation while we are type-checking the
30200744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      // definition. Compute the declaration context and return that.
30210744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      assert(!SawNonDependentContext &&
302260700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne             "No dependent context while instantiating record");
3023bf91650557d9afa08e3d311a9c0a10d73c62cbfcMike Stump      DeclContext *DC = computeDeclContext(T);
30240744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner      assert(DC &&
30250744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner             "Unable to find declaration for the current instantiation");
302660700390a787471d3396f380e0679a6d08c27f1fPeter Collingbourne      return cast<CXXRecordDecl>(DC);
30270744e5f3325e2d2107506002e43c37ea0155a5acChris Lattner    }
302854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
3029eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    // Fall through to deal with other dependent record types (e.g.,
3030eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    // anonymous unions in class templates).
3031eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  }
3032eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
3033eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  if (!ParentDC->isDependentContext())
3034eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    return D;
3035eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
3036eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
3037eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  if (!ParentDC)
3038eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    return 0;
3039eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
3040eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  if (ParentDC != D->getDeclContext()) {
3041eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    // We performed some kind of instantiation in the parent context,
3042eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    // so now we need to look into the instantiated parent context to
3043eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    // find the instantiation of the declaration D.
3044eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
3045eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    // If our context used to be dependent, we may need to instantiate
3046eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    // it before performing lookup into that context.
3047eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    bool IsBeingInstantiated = false;
3048eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
3049eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall      if (!Spec->isDependentContext()) {
305054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall        QualType T = Context.getTypeDeclType(Spec);
305154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall        const RecordType *Tag = T->getAs<RecordType>();
3052eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
3053eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        if (Tag->isBeingDefined())
3054eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall          IsBeingInstantiated = true;
305554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall        if (!Tag->isBeingDefined() &&
3056eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
3057eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall          return 0;
3058eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
3059eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        ParentDC = Tag->getDecl();
306054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall      }
3061eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    }
3062eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
306354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    NamedDecl *Result = 0;
3064eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    if (D->getDeclName()) {
3065eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
3066eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall      Result = findInstantiationOf(Context, D, Found.first, Found.second);
306758e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall    } else {
30682f514480c448708ec382a684cf5e035d3a827ec8John McCall      // Since we don't have a name for the entity we're looking for,
30692f514480c448708ec382a684cf5e035d3a827ec8John McCall      // our only option is to walk through all of the declarations to
3070eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall      // find that name. This will occur in a few cases:
3071eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall      //
3072eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall      //   - anonymous struct/union within a template
30732f514480c448708ec382a684cf5e035d3a827ec8John McCall      //   - unnamed class/struct/union/enum within a template
30742f514480c448708ec382a684cf5e035d3a827ec8John McCall      //
30752f514480c448708ec382a684cf5e035d3a827ec8John McCall      // FIXME: Find a better way to find these instantiations!
30762f514480c448708ec382a684cf5e035d3a827ec8John McCall      Result = findInstantiationOf(Context, D,
30772f514480c448708ec382a684cf5e035d3a827ec8John McCall                                   ParentDC->decls_begin(),
3078eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall                                   ParentDC->decls_end());
3079eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall    }
3080eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall
30812f514480c448708ec382a684cf5e035d3a827ec8John McCall    if (!Result) {
30822f514480c448708ec382a684cf5e035d3a827ec8John McCall      if (isa<UsingShadowDecl>(D)) {
3083eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        // UsingShadowDecls can instantiate to nothing because of using hiding.
30842f514480c448708ec382a684cf5e035d3a827ec8John McCall      } else if (Diags.hasErrorOccurred()) {
3085eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        // We've already complained about something, so most likely this
30862f514480c448708ec382a684cf5e035d3a827ec8John McCall        // declaration failed to instantiate. There's no point in complaining
30872f514480c448708ec382a684cf5e035d3a827ec8John McCall        // further, since this is normal in invalid code.
30882f514480c448708ec382a684cf5e035d3a827ec8John McCall      } else if (IsBeingInstantiated) {
3089eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        // The class in which this member exists is currently being
30902f514480c448708ec382a684cf5e035d3a827ec8John McCall        // instantiated, and we haven't gotten around to instantiating this
309154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall        // member yet. This can happen when the code uses forward declarations
309254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall        // of member classes, and introduces ordering dependencies via
309354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall        // template instantiation.
309454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall        Diag(Loc, diag::err_member_not_yet_instantiated)
309558e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall          << D->getDeclName()
3096eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3097eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
309858e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall      } else {
3099eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall        // We should have found something, but didn't.
31002f514480c448708ec382a684cf5e035d3a827ec8John McCall        llvm_unreachable("Unable to find instantiation of declaration!");
31012f514480c448708ec382a684cf5e035d3a827ec8John McCall      }
31022f514480c448708ec382a684cf5e035d3a827ec8John McCall    }
31032f514480c448708ec382a684cf5e035d3a827ec8John McCall
31040a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor    D = Result;
31052f514480c448708ec382a684cf5e035d3a827ec8John McCall  }
31062f514480c448708ec382a684cf5e035d3a827ec8John McCall
31072f514480c448708ec382a684cf5e035d3a827ec8John McCall  return D;
31082f514480c448708ec382a684cf5e035d3a827ec8John McCall}
31092f514480c448708ec382a684cf5e035d3a827ec8John McCall
31109c3087b0b0bea2fd782205c1274ebfc4290265e0John McCall/// \brief Performs template instantiation for all implicit template
31112f514480c448708ec382a684cf5e035d3a827ec8John McCall/// instantiations we have seen until this point.
31122f514480c448708ec382a684cf5e035d3a827ec8John McCallvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
31132f514480c448708ec382a684cf5e035d3a827ec8John McCall  while (!PendingLocalImplicitInstantiations.empty() ||
31142f514480c448708ec382a684cf5e035d3a827ec8John McCall         (!LocalOnly && !PendingInstantiations.empty())) {
31152f514480c448708ec382a684cf5e035d3a827ec8John McCall    PendingImplicitInstantiation Inst;
3116ce2d186a421526e94d9e417ced141ae6c891cf48Benjamin Kramer
3117c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian    if (PendingLocalImplicitInstantiations.empty()) {
3118ce2d186a421526e94d9e417ced141ae6c891cf48Benjamin Kramer      Inst = PendingInstantiations.front();
3119ce2d186a421526e94d9e417ced141ae6c891cf48Benjamin Kramer      PendingInstantiations.pop_front();
3120c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian    } else {
3121c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian      Inst = PendingLocalImplicitInstantiations.front();
3122ce2d186a421526e94d9e417ced141ae6c891cf48Benjamin Kramer      PendingLocalImplicitInstantiations.pop_front();
312354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    }
312454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
3125ce2d186a421526e94d9e417ced141ae6c891cf48Benjamin Kramer    // Instantiate function definitions
31268e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3127743b82bf3c500de45715498dbf25f0fb39e71462Peter Collingbourne      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
312854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall                                          "instantiating function definition");
3129eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3130eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall                                TSK_ExplicitInstantiationDefinition;
313154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
313254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall                                    DefinitionRequired);
313354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall      continue;
313454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    }
313554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
313654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    // Instantiate static data member definitions.
3137ce2d186a421526e94d9e417ced141ae6c891cf48Benjamin Kramer    VarDecl *Var = cast<VarDecl>(Inst.first);
3138c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian    assert(Var->isStaticDataMember() && "Not a static data member?");
3139c4b35cfdb977f6427fe0d5725bf104e1b425d72eFariborz Jahanian
31408e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian    // Don't try to instantiate declarations if the most recent redeclaration
3141743b82bf3c500de45715498dbf25f0fb39e71462Peter Collingbourne    // is invalid.
31428e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian    if (Var->getMostRecentDeclaration()->isInvalidDecl())
31438e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian      continue;
31448e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian
31458e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian    // Check if the most recent declaration has changed the specialization kind
314654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    // and removed the need for implicit instantiation.
3147    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3148    case TSK_Undeclared:
3149      assert(false && "Cannot instantitiate an undeclared specialization.");
3150    case TSK_ExplicitInstantiationDeclaration:
3151    case TSK_ExplicitSpecialization:
3152      continue;  // No longer need to instantiate this type.
3153    case TSK_ExplicitInstantiationDefinition:
3154      // We only need an instantiation if the pending instantiation *is* the
3155      // explicit instantiation.
3156      if (Var != Var->getMostRecentDeclaration()) continue;
3157    case TSK_ImplicitInstantiation:
3158      break;
3159    }
3160
3161    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3162                                        "instantiating static data member "
3163                                        "definition");
3164
3165    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3166                              TSK_ExplicitInstantiationDefinition;
3167    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3168                                          DefinitionRequired);
3169  }
3170}
3171
3172void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3173                       const MultiLevelTemplateArgumentList &TemplateArgs) {
3174  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3175         E = Pattern->ddiag_end(); I != E; ++I) {
3176    DependentDiagnostic *DD = *I;
3177
3178    switch (DD->getKind()) {
3179    case DependentDiagnostic::Access:
3180      HandleDependentAccessCheck(*DD, TemplateArgs);
3181      break;
3182    }
3183  }
3184}
3185