SemaTemplateInstantiateDecl.cpp revision 500d729e85028944355a119f9823ac99fa5ddcab
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
122d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h"
14f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
16aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
200c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
22a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
2483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
28b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
30c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
31c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
32a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
33c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
34a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
35c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                          TemplateArgs);
36a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
38b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
39a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
41b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
42b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
43b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
44b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
46c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
47c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
48a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
49c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
50a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
51c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                        TemplateArgs);
52a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
53c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
55a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
56c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
57b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
58b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
59b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
607b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins// Include attribute instantiation code.
617b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins#include "clang/Sema/AttrTemplateInstantiate.inc"
627b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins
631d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
6423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            const Decl *Tmpl, Decl *New,
6523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LateInstantiatedAttrVec *LateAttrs,
6623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LocalInstantiationScope *OuterMostScope) {
67cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
68cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
69cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
7023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
714ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
724ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
73cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      if (Aligned->isAlignmentDependent()) {
74cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        if (Aligned->isAlignmentExpr()) {
75f6702a3927147655206ae729a84339c4fda4c651Richard Smith          // The alignment expression is a constant expression.
76f6702a3927147655206ae729a84339c4fda4c651Richard Smith          EnterExpressionEvaluationContext Unevaluated(*this,
77f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                       Sema::ConstantEvaluated);
78f6702a3927147655206ae729a84339c4fda4c651Richard Smith
7960d7b3a319d84d688752be3870615ac0f111fb16John McCall          ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
807663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                        TemplateArgs);
81cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (!Result.isInvalid())
82fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman            AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(),
83fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman                           Aligned->getIsMSDeclSpec());
84f6702a3927147655206ae729a84339c4fda4c651Richard Smith        } else {
85cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
867663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             TemplateArgs,
87a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                             Aligned->getLocation(),
887663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             DeclarationName());
89cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (Result)
90fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman            AddAlignedAttr(Aligned->getLocation(), New, Result,
91fc685ace387734599c475426b1a8efdb491054b8Aaron Ballman                           Aligned->getIsMSDeclSpec());
92cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
934ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        continue;
944ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      }
954ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
964ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
9723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    if (TmplAttr->isLateParsed() && LateAttrs) {
9823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // Late parsed attributes must be instantiated and attached after the
9923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // enclosing class has been instantiated.  See Sema::InstantiateClass.
10023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LocalInstantiationScope *Saved = 0;
10123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (CurrentInstantiationScope)
10223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
10323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
10423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    } else {
1055bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer      Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
1065bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer                                                         *this, TemplateArgs);
10731c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola      if (NewAttr)
10831c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola        New->addAttr(NewAttr);
10923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
110d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
111d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
112d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1134f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1144f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
115b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
1164f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1174f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1184f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
11957ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
12057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
12157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
12257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
12357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
12457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
12557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
12657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1274f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
128b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
1294f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1304f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1313dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1323dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1333dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1343dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1353dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1363dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1370cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1380cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1393dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1403dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1413dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1423dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1433dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1443dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
1463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
1478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
148a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
149561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
150836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
151ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
152ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
153ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
155a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
157b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
158b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
162162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
163162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
164162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
165162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
166162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
167162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
168162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
1698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
172cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
173cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
174cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
175cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
176162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (oldTag->getTypedefNameForAnonDecl() == D) {
177cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
178162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl());
179162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
180cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
181d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
182a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
183ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
1847c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1857c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
186b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
187b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
188a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1895df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
1905df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
1915df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    // If the typedef types are not identical, reject them.
1925df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
1935df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
1945df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    Typedef->setPreviousDeclaration(InstPrevTypedef);
1955126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1965126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
1971d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
198d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
19946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
204162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
2063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
208162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
209162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
210162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
2113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
2123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
2143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
2153e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
2173e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
2193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
2203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
2213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
2233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
2253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
2283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *PrevAliasTemplate = 0;
230ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (Pattern->getPreviousDecl()) {
2313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
2323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (Found.first != Found.second) {
2333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(*Found.first);
2343e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
2353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2363e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2373e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
2383e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
2393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
2403e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2413e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2423e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
2433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
2453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
2463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setPreviousDeclaration(PrevAliasTemplate);
2473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
2493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
2513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
252a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2533e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
2543e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2553e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
256162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
257162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2583d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
2599901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
2609901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
2619901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2629901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2639901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2649901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
2659901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
266ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
267a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
2680a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
2690a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
2700a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
2710a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
2723d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
2733d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
274c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
275c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
276c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
277c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
278c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
279a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
280b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
2813d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
282ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 D->getInnerLocStart(),
2833d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
2840a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
28516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
28616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
2873d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
2885b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  Var->setInitStyle(D->getInitStyle());
289ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
290796c1a1e3e63e459e371383ac878aa5f40b02a8cRichard Smith  Var->setConstexpr(D->isConstexpr());
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
292b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
293b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
294b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
295b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
303a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3046b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (!D->isStaticDataMember()) {
305c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3066b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    Var->setReferenced(D->isReferenced());
3076b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  }
308a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
309390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
310390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3116826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3126826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
313449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
31460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
31560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3169aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3179aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
3184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().ObjCAutoRefCount &&
3199aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
3209aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
3219aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3222c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain  SemaRef.CheckVariableDeclaration(Var, Previous);
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
3253e9ea0b8cd7c4691d62e385245556be5fded58a7Richard Smith    D->getLexicalDeclContext()->addDecl(Var);
3267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
329f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
330f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
332dd5756c04c98e354b85c4f7eb660ae60c6d341ecDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Var, LateAttrs, StartingScope);
333a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
334251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
335251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
336251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
337a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
338cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
339a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
34060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
34160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
34260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3431f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
344f6702a3927147655206ae729a84339c4fda4c651Richard Smith      SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated);
3451f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3461f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3471f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3486b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3495b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs,
3505b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                        D->getInitStyle() == VarDecl::CallInit);
3515b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (!Init.isInvalid()) {
35234b41d939a1328f484511c6002ba2456db879a29Richard Smith      bool TypeMayContainAuto = true;
3535b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      if (Init.get()) {
3545b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        bool DirectInit = D->isDirectInit();
3555b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit,
3565b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                     TypeMayContainAuto);
3575b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      } else
3586aeaa60217e1ed11a621409acf1b53df0d14b591Eli Friedman        SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
3596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
3606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
3616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
3626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
3636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
364a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3651f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
366ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
367ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith             !Var->isCXXForRangeDecl())
368d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
3693d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
370e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // Diagnose unused local variables with dependent types, where the diagnostic
371e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // will have been deferred.
372e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
373e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith      D->getType()->isDependentType())
3745764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
375bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
3763d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3773d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3783d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3796206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3806206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3816206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3826206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3836206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3846206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3856206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3866206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
389a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
390561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
391836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
39207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
39307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
39407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
395a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
39607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
39707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
40507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
408b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
409b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
416f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
417f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
418f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
421ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
426e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
42907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
43007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
435ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                            D->getInClassInitStyle(),
436703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith                                            D->getInnerLocStart(),
4378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
439663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
440663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
441f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
442663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
445a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
446f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
447f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
450f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
451f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
452a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
4539901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4549901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4557a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4569901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
459f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
46046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
461f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
46687c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
46787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
46887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
46987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
47087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
47187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
47287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
473b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
474a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
475b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
476b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
477b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
478a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
479b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
480b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
48187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
48240e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
48387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
48487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
48540e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
48687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
48787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
48887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
48987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
49087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
49187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
49287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
49387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
49487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
49502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
49602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
49706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
49832f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
4994fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5004fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5014fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5024fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5034fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5044fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5054fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5064fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5074fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5084fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5094fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5104fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
51106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
512fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5130216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocation(),
5140216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
51506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
51606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
517a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
51806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5199a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
52006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
52106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
522a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
523a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
52406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
52506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
52632f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
527af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
528af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
529af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
530af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
531af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
53206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
535a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
53606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5375fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5389a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
53902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
54002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
541fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
542fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
546f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
547f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
548f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
551ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
555e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith  return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
5569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
557e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getMessage(),
558e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getRParenLoc(),
559e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->isFailed());
5608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
56338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  EnumDecl *PrevDecl = 0;
56438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (D->getPreviousDecl()) {
56538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
56638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   D->getPreviousDecl(),
56738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   TemplateArgs);
56838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    if (!Prev) return 0;
56938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    PrevDecl = cast<EnumDecl>(Prev);
57038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  }
57138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith
572ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
5738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
57438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                    PrevDecl, D->isScoped(),
575a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
5761274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
577f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
5781274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
5791274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
5801274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
5811274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
582f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
583f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                                DeclarationName());
584f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
5851274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
586f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      else
587f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith        Enum->setIntegerTypeSourceInfo(NewTI);
588f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    } else {
5891274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
5901274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
5911274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
5921274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5931274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
5941274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5955b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
5965b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
597f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
59806c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
599b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
60017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
601f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6024ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  EnumDecl *Def = D->getDefinition();
6034ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  if (Def && Def != D) {
6044ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // If this is an out-of-line definition of an enum member template, check
6054ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // that the underlying types match in the instantiation of both
6064ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // declarations.
6074ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
6084ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6094ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      QualType DefnUnderlying =
6104ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith        SemaRef.SubstType(TI->getType(), TemplateArgs,
6114ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                          UnderlyingLoc, DeclarationName());
6124ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
6134ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                                     DefnUnderlying, Enum);
6144ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    }
6154ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  }
6168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
61796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
61896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
619a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
620f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // C++11 [temp.inst]p1: The implicit instantiation of a class template
621f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // specialization causes the implicit instantiation of the declarations, but
622f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // not the definitions of scoped member enumerations.
623f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: There appears to be no wording for what happens for an enum defined
62438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // within a block scope, but we treat that much like a member template. Only
62538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // instantiate the definition when visiting the definition in that case, since
62638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // we will visit all redeclarations.
62738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (!Enum->isScoped() && Def &&
62838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith      (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
6294ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    InstantiateEnumDefinition(Enum, Def);
630f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
631f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return Enum;
632f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
633f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
634f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid TemplateDeclInstantiator::InstantiateEnumDefinition(
635f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    EnumDecl *Enum, EnumDecl *Pattern) {
636f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->startDefinition();
637f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6381af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  // Update the location to refer to the definition.
6391af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  Enum->setLocation(Pattern->getLocation());
6401af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
6415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
6428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
644f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
645f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith         ECEnd = Pattern->enumerator_end();
6468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
64860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
649ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
650f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
652f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
654ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
655ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
6618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
6628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6679ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
6688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
676581deb3da481053c4993c7600f97acf7768caac5David Blaikie      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
6775b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
6783b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
67917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
680d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
6818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
682a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
683f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (Pattern->getDeclContext()->isFunctionOrMethod() &&
684f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith          !Enum->isScoped()) {
68596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
68696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
687581deb3da481053c4993c7600f97acf7768caac5David Blaikie        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
68896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
692f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: Fixup LBraceLoc
693f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
694f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                        Enum->getRBraceLoc(), Enum,
695de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
696fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6996477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
700b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7016477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7026477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
703e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
70493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
70593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
706550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
707550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
7082a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
709e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
710ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
712d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
713e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
714e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
719c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
720c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
721c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
722c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
723c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
724c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
72593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
72693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
72893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
72993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
730ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
73137574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
73237574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (Found.first != Found.second) {
73337574f55cd637340f651330f5cfda69742880d36Nick Lewycky      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
73437574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
73537574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
73637574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
73737574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
73837574f55cd637340f651330f5cfda69742880d36Nick Lewycky
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
74393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
745c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
74693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
747c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
74993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
75093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
75193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
75293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
75493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
75893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
75993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
76093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
76193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
76393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
768c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7701eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
771c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
77293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
77493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
775c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
777c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
778c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
779c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
780c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
781c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
782c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
783c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
784c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
785a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
786c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
787c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
788c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
789c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
790c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
791c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
792c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
793c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
794c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
795c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
796c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
797c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
798c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
799c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
800c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
801c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
802c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
80793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
808a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
809c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
810c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
811c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
812c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
813c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
814c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
815c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
81693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
81793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
81893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
819c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
820c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
82193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
82293ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
82393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
82493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
82593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
826e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
82793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
828ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
829ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
830f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
831e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
832c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
833c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
834b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
835e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
83693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
83793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
83893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
839e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
840ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
84193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
842ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
843ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
844ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
845ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
846ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
84793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
84893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
84993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
85093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
851e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
85237574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
85337574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
85493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
855a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
856f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
85824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
859ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
860259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
86193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
8621b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(Inst);
8634c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
8644c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
865e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
866259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
867a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
8684c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
8694c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
8704c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
8714c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
8724c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
873e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
874d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
875d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
876d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
877d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
878d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
8795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
880d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
881d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
882d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
883d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
884d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
885d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
886e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
887e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
888e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
889d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8907974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8917974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
892ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
893a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
894ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
895ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
896ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
897ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
898ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
899ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
900a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
901ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
902ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
903ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
904ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
905a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
906d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
907d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
908d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
909d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
910d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
9117974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
9127974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
9137974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
914d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
915550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
916550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
917a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
918550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
9192a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
920895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
921d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
922d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
924d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
925a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
926a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
927a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
928a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
929a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
930a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
931a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
932a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
933a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
934a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
935a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
936d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
937d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
93846460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
93946460a68f6508775e98c19b4bb8454bb471aac24John McCall
9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
941d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
942a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
943a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
94437d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
945a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
946a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
947e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
948b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
949b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
950e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
951e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
952e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
953b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
954a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
955a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
956b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
957b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
958a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
959b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
960d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
961d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
962d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
963d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
964d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
965d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
966d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
967ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
9687c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
969ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
9706c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9716c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9726c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
9736c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
974d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
975d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
9761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
977ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
978ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
979b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
980b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
981b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
982b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
983b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
984d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
985eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
986eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
987eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
988eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
989eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
990d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
991f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
992d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
99302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
99402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
99502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
99602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
99702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9989901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9999901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
10009901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
10017a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
10029901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
10039901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
1004d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
100517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
1006d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1007d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1008d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
100902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
101002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
101102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
101202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
101302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
10147557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1015a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1016127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1017127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1018127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1019b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1020a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
102124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
10221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10231e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
10242c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
10252c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
10262c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
10271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1028127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
10292c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
10302c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1031127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
10321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1033b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1034b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1035b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1036b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1037b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1038b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
103979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1040b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1041a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
104279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
10432a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
10441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
104664b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
104721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
10482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
104921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
1050fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1051c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1052c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1053c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1054c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1055c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1056c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1057d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1058d325daa506338ab86f9dd468b48fd010673f49a6John McCall
105968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
106068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
106168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
106268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
106368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1064c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  else if (isFriend && QualifierLoc) {
1065d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1066c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1067d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1068d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1069d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1070a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10717c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1072d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
107368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
107402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1075ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1076ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                           D->getLocation(), D->getDeclName(), T, TInfo,
107716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
1078af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
107986c3ae46250cdcc57778c27826060779a92f3815Richard Smith                           D->isConstexpr());
1080b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1081c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1082c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1083b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1084b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1085b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1086b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1087b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1088b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1089b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1090b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1092e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
10935cbe101b502e06d16bc77df45a27ce8bc13f33c8Douglas Gregor  if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
10941d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Adopt the already-instantiated parameters into our own context.
10951d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (unsigned P = 0; P < Params.size(); ++P)
10961d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      if (Params[P])
10971d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        Params[P]->setOwningFunction(Function);
10981d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  } else {
10991d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Since we were instantiated via a typedef of a function type, create
11001d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // new parameters.
11011d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    const FunctionProtoType *Proto
11021d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      = Function->getType()->getAs<FunctionProtoType>();
11031d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    assert(Proto && "No function prototype in template instantiation?");
11041d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
11051d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor         AE = Proto->arg_type_end(); AI != AE; ++AI) {
11061d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      ParmVarDecl *Param
11071d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
11081d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor                                             *AI);
11091d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Param->setScopeInfo(0, Params.size());
11101d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Params.push_back(Param);
11111d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    }
11121d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  }
11134278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
111402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1115ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1116a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1117a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1118a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1119a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1120a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1121a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1122a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1123a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1124a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1125a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1126a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1127a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1128a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1129a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1130a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1131d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1132a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1133a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1134a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1135a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1136b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1137b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1138d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1139d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1140d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1141d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1142d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1143d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1144d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
114566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
114666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1147a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
114824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1149838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1150910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
115124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
115224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
11531e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                                /*InsertPos=*/0);
115480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
115580f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
115680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
115780f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
115880f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
115980f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
116080f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1161d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
116202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1163a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1164e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1165e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1167af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1168a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
11696826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
11706826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
11716826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1172af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1173af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1174af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1175af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1176af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1177af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1178af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1179af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1180af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1181af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1182e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1183e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1184e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1185af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1186af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1187af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1188af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1189af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1190af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1191af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1192af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1193af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1194af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1195af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1196af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1197af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1198af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1199af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1200a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1201af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1202af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1203af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1204a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1205a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1206a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
12076826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1208a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1209a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1210a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1211a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1212a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
12136826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
12146826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1215a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1216a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
12179f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
12182c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1219e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
122076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
122176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
122276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
122376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1224a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1225a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1226d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
12276826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
122876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1229ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = FunctionTemplate->getPreviousDecl();
123076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1231ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = Function->getPreviousDecl();
123276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
123376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
12341b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1235ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
123677535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1237ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
123853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++98 [temp.friend]p5: When a function is defined in a friend function
123953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is defined at each
124053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   instantiation of the class template. The function is defined even if it
124153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   is never used.
124253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++11 [temp.friend]p4: When a function is defined in a friend function
124353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is instantiated when the
124453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   function is odr-used.
124553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //
124653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // If -Wc++98-compat is enabled, we go through the motions of checking for a
124753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // redefinition, but don't instantiate the function.
12484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if ((!SemaRef.getLangOpts().CPlusPlus0x ||
124953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith         SemaRef.Diags.getDiagnosticLevel(
125053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             diag::warn_cxx98_compat_friend_redefinition,
125153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             Function->getLocation())
125253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith           != DiagnosticsEngine::Ignored) &&
1253238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1254238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1255238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
125610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1257238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
125853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        SemaRef.Diag(Function->getLocation(),
12594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                     SemaRef.getLangOpts().CPlusPlus0x ?
126053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::warn_cxx98_compat_friend_redefinition :
126153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::err_redefinition) << Function->getDeclName();
1262238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
12634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (!SemaRef.getLangOpts().CPlusPlus0x)
126453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          Function->setInvalidDecl();
1265a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1266238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1267238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1268238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1269238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1270238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
127113a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
127213a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1273ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1274ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
12754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          if (!SemaRef.getLangOpts().CPlusPlus0x &&
127653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              !queuedInstantiation && R->isUsed(false)) {
1277ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1278ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1279ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1280ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1281ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1282ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1283ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1284ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1285ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1286ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1287ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1288ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1289ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1290238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
12916a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
129210620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
129353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              SemaRef.Diag(Function->getLocation(),
12944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                           SemaRef.getLangOpts().CPlusPlus0x ?
129553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::warn_cxx98_compat_friend_redefinition :
129653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::err_redefinition)
1297238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
12986a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
12994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie              if (!SemaRef.getLangOpts().CPlusPlus0x)
130053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                Function->setInvalidDecl();
1301238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1302238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1303238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1304238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1305238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1306a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1307a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
130876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
130976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
131076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
131176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1312eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1313e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1314e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
13152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1316d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1317d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1318af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1319af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
13206b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1321d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
13221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1324d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1325a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
132624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
13271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13281e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
13292c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
13302c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
13312c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
13321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13336b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
13342c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
13352c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
13366b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
13376b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1338b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1339b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1340b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1341b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1342b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1343b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
134479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1345a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
134679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
13472a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
134848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
13494eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
13505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
13514eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
13524eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
13534eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
13544eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
13554eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
13564eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
13574eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
13584eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
13594eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
13604eab39f0745fb1949dbb40c4145771b927888242John McCall    }
13614eab39f0745fb1949dbb40c4145771b927888242John McCall  }
13624eab39f0745fb1949dbb40c4145771b927888242John McCall
13635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1364dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
136521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
13662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
136721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
13682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1369723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1370723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1371723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
13725f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
13735f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
13745f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
13755f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
13765f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
13775f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1378723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
13795f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
13805f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 4> ParamTypes;
1381a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1382a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                               D->getNumParams(), TemplateArgs, ParamTypes,
138312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               &Params))
1384a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      return 0;
13855f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
13865f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1387c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1388c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1389c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1390b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1391a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1392c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1393b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1394b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1395b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1396b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1397c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1398b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1399c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1400b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1401c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1402c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1403c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1404b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1405b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1406b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1407b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1408b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1409b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1410b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1411b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
14122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1413b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1414dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1416ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
14172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
14182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
141917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1421ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
142316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
142486c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                        false, Constructor->isConstexpr());
142517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
142617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1427ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
14282577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
142916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
143065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
143165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1432ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
14330130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1434f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
143586c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                       Conversion->isConstexpr(),
14369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1437dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
14382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1439ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
144016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
144116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
1442f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                   D->isInlineSpecified(),
144386c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                   D->isConstexpr(), D->getLocEnd());
1444dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14456b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1446c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1447c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1448b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1449d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1450d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1451d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
14521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1453d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1454d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1455d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1456d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1457d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1458d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1459d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1460d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1461d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1462d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1463d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1464d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1466d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1467b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1468b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1469b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1470b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
14711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1472d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
147366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
147466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1475a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
147624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1477838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1478910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1479910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.first,
1480910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.second),
14811e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                              /*InsertPos=*/0);
1482b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
148366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
14842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
148566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1486a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
14887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
14897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1490b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
14914eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
14924eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
14934eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
14944eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
14954eab39f0745fb1949dbb40c4145771b927888242John McCall
1496b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1497b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1498b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
14997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
15025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
15035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
15044278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
15055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
15075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
15082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
15092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
15102577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
15111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1512b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1513b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
15141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1515dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1516dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1517dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1518dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
15196826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
15206826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1521dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
15222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1523af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
15242c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
152565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
15264ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
15274ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
15284ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
152946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
153046460a68f6508775e98c19b4bb8454bb471aac24John McCall
15319eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
15329eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
15333bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
15343bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDefaulted())
15353bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman    Method->setDefaulted();
15363bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
15373bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman    Method->setDeletedAsWritten();
15383bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
1539b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1540b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1541b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1542b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1543b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1544b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1545b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1546b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1547b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1548b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1549af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    else if (!IsClassScopeSpecialization)
1550b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1551b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1552eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
1553eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  if (D->isExplicitlyDefaulted()) {
1554eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
1555eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  } else {
1556eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt    assert(!D->isDefaulted() &&
1557eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt           "should not implicitly default uninstantiated function");
1558eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  }
1559eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
15602dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
15612dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
15622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1563615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1564dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1565615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1566615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
156703b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
156817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
156903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
157003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1571bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
157265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1573bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1574bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
15756477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1576fb44de956f27875def889482b5393475060392afJohn McCall  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
1577d1bb4ae6cbc0f8bea4b329e040f58b18c03388e7Douglas Gregor                                  llvm::Optional<unsigned>(),
1578d1bb4ae6cbc0f8bea4b329e040f58b18c03388e7Douglas Gregor                                  /*ExpectParameterPack=*/false);
15792dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
15802dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1581e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1582e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1583e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
15844fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
15851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1586e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1587344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1588344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
15894fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
15904fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1591e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1592e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
15939a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1594a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
15950f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
1596a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1597e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1598a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1599550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1600550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1601a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1602e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1603e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1604e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
160533642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
160633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
160733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
16086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
16095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
16105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
16116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1612a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
161333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
161433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
16156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
16166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1617a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
16186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
16196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
16206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
16216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
16226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
16236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1624a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
16256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
16266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
16276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1628a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
16306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
16316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
16326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
16336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
16346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
16356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1636a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
16386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
16396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
16406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else if (isa<PackExpansionTypeLoc>(TL)) {
16416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
16426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
16436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
16446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
16456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
16465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
16476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1648a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
16506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
16516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
16526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
16536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> OrigNumExpansions
16546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
16556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
16566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
16576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1658a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
16596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1660a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
16616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
16626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1663a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
16656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
16666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
16676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1668a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
16696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
16706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
16716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1672a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
16746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
16756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
16766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
16776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
16786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
16796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
16806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1681a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
16836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
16846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
16856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
16866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
16876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
16886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
16896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
16906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
16916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
16926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1693a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
16946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
16956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
16966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1697a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
16996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
17006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
17016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1702a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
17046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
17056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
17066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1707a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
17086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
17096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
17106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1711a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1713a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
17146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
17156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
17166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
17176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
17186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
171933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1720a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
17226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1723a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1724ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1725ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1726a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1727a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
17286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
17296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
17306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
17316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
17326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
17336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1734a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1735ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
17366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1737a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1738a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1739a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
17406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1741a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17429a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
174333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
174433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1745a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1746d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1747a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1748a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1749550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1750550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
175133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
175233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
175333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
17540dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
17559106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
17569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
17579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
17589106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
17599106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
17609106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
17619106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
17629106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
17632a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
17649106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
17659106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
17669106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
1767a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
1768a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17699106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
17709106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
17719106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1772a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
1773a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                       D->getPosition(), D->isParameterPack(),
177461c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getIdentifier(), InstParams);
1775d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
17769a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
1777a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1778a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
17799106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
17809106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1781a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17829106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
17839106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
17849106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
178548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1786db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
1787db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
1788a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
178948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
179048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1791a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
1792db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
1793a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
1794a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
179548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
179648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
179748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
179848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
179948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1800ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
18011b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
18021b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
18031b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
18041b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
18051b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
18061b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
18071b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
18081b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
18091b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
18105149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
18115149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
18125149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
18135149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
1814dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
18151b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
18161b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
18171b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1818ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1819ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18209f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
18219f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
18229f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
18239f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
18249f54ad4381370c6b771424b53d219e661d6d6706John McCall
1825ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1826ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
18279f54ad4381370c6b771424b53d219e661d6d6706John McCall
1828ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1829ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
18305149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
1831ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1832ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1833ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18345149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
18355149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
18369f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
18379f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
18389f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
18399f54ad4381370c6b771424b53d219e661d6d6706John McCall
18409f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
18419f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
18429f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
18439f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
18449f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
18459f54ad4381370c6b771424b53d219e661d6d6706John McCall
18469f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
18479f54ad4381370c6b771424b53d219e661d6d6706John McCall
18489f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
18499f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1850ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1851ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
18529f54ad4381370c6b771424b53d219e661d6d6706John McCall
1853ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1854ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1855ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1856ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18579f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
18589f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
18599f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
18609f54ad4381370c6b771424b53d219e661d6d6706John McCall
1861c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
1862c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
1863c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith      NewUD->setInvalidDecl();
1864c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    return NewUD;
1865c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  }
1866c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith
1867323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1868323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
18699f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
18709f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
18719f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
18729f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
18739f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
1874b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
1875b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
1876b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
1877b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
1878b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
1879b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
18809f54ad4381370c6b771424b53d219e661d6d6706John McCall
18819f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
18829f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
18839f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
18849f54ad4381370c6b771424b53d219e661d6d6706John McCall
18859f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
18869f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
18879f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1888323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1889323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1890323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
18919f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1892ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1893ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1894ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1895ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1896ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
18979f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
18989f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1899ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1900ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
19027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
19035149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
1904a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
19055149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
19065149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
19077ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
19087ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
19097ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
19105149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
19117ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1912ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1913ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1914ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
19157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
19167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1917ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
19187ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
19197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
19204469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1921ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1922ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
19247ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
19257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
19267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
19277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
19285149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
19295149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
19305149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
19310dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
1932a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19330dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
19345149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1936ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1937ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1938ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
19391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
19409488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1941ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
19427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
19437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
19444469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1945ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1946ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19470d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
19480dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
19490dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1950af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1951af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
1952af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
1953af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
19546b02009359a462ffe633696a4441313b462e6566Nico Weber  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
19556b02009359a462ffe633696a4441313b462e6566Nico Weber                                                                0, true));
1956af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1957af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
1958af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
1959af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
19606b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo TemplateArgs;
19616b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo* TemplateArgsPtr = 0;
19626b02009359a462ffe633696a4441313b462e6566Nico Weber  if (Decl->hasExplicitTemplateArgs()) {
19636b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgs = Decl->templateArgs();
19646b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgsPtr = &TemplateArgs;
19656b02009359a462ffe633696a4441313b462e6566Nico Weber  }
19666b02009359a462ffe633696a4441313b462e6566Nico Weber
1967af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
19686b02009359a462ffe633696a4441313b462e6566Nico Weber  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
19696b02009359a462ffe633696a4441313b462e6566Nico Weber                                                  Previous)) {
1970af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
1971af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
1972af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
1973af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1974af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
1975af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
1976af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
1977af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
1978af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1979af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
1980af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1981af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1982ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1983d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
19847e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
19852fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
19862fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
19872fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
19888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
19898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
19908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1991e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1992e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1993e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1994e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1995e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1996e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1997e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1998ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1999e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2000e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2001e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2002e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
20035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2004e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2005e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2006e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2007e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2008bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2009e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
20109148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2011e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2012e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2013e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2014ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
2015e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
2016e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2017e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2018e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2019e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2020e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2021e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
20221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2023e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2024a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2025ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2026ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2027ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2028ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2029ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2030a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2031ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2032ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2033d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2034d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2035d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2036ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2037ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2038ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2039550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2040550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2041550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
20422a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2043a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2044ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2045ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2046ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2047ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2048ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2049d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2050a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2051ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2052ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2053d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
2054a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2055a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                    PartialSpec->getNumTemplateArgsAsWritten(),
2056e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2057e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2058a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2059ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2060ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
20615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2062a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2063ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2064a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2065ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2066ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2067d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2068ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2069ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2070ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2071ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2072ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2073910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2074910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2075a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2076ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2077ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2078a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2079ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2080910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2081910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2082ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2083ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2084ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2085ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2086ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2087ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2088ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2089ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
20903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
20913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
20923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
20933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2094d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2095ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2096a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2097ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2098ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2099ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2102ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2103ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2104ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2105ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2106ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2107ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2108ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2110ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2111ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2112ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2113ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2114d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2115ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2116ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2117d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2118ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2119a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2120a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2121ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2122ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2123a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
212413c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2125a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2126ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2127ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2128ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2129a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2130910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2131910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2132d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
21333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
2134dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
2135cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
2136b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2140ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
21414469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2142a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2143ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2144ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
21451e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor  ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
2146d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2147ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2148ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
214921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
215021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
21515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
215221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
215321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
215421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
2155cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
2156cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *ThisContext = 0;
2157cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ThisTypeQuals = 0;
2158cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2159cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisContext = Method->getParent();
2160cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisTypeQuals = Method->getTypeQualifiers();
2161cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
2162cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
21636cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
21646cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
21656cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
2166cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    D->getDeclName(),
2167cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    ThisContext, ThisTypeQuals);
216821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
216921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
21705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2171cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
2172cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
2173140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
21746920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
21756920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2176140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
21776920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
21786920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
2179500d729e85028944355a119f9823ac99fa5ddcabRichard Smith      unsigned NewIdx = 0;
218012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
218112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
218212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
2183500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2184500d729e85028944355a119f9823ac99fa5ddcabRichard Smith
2185500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        llvm::Optional<unsigned> NumArgumentsInExpansion;
2186500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (OldParam->isParameterPack())
2187500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          NumArgumentsInExpansion =
2188500d729e85028944355a119f9823ac99fa5ddcabRichard Smith              SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2189500d729e85028944355a119f9823ac99fa5ddcabRichard Smith                                                 TemplateArgs);
2190500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (!NumArgumentsInExpansion) {
2191a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
219212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
219312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
219412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
2195500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->InstantiatedLocal(OldParam, NewParam);
2196500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        } else {
2197500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          // Parameter pack expansion: make the instantiation an argument pack.
2198500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->MakeInstantiatedLocalArgPack(OldParam);
2199500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
2200500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2201500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Params.push_back(NewParam);
2202500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2203500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          }
220412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
22056920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2206895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
2207cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
2208cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
2209cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
2210cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
2211140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
22126920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
22136920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
22146920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
22156920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
22166920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
22176920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
22186920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
22196920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2220cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2221cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
222221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
22235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
22245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2225e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// Introduce the instantiated function parameters into the local
2226e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// instantiation scope, and set the parameter names to those used
2227e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// in the template.
2228e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2229e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             const FunctionDecl *PatternDecl,
2230e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             LocalInstantiationScope &Scope,
2231e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
2232e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned FParamIdx = 0;
2233e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2234e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2235e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (!PatternParam->isParameterPack()) {
2236e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Simple case: not a parameter pack.
2237e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(FParamIdx < Function->getNumParams());
2238e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2239e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2240e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocal(PatternParam, FunctionParam);
2241e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2242e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2243e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2244e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2245e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Expand the parameter pack.
2246e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2247500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    llvm::Optional<unsigned> NumArgumentsInExpansion
2248e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
2249500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    assert(NumArgumentsInExpansion &&
2250500d729e85028944355a119f9823ac99fa5ddcabRichard Smith           "should only be called when all template arguments are known");
2251500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
2252e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2253e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2254e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2255e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2256e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2257e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2258e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2259e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2260e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2261e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     const FunctionProtoType *Proto,
2262e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
226313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
226413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2265e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // C++11 [expr.prim.general]p3:
2266e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   If a declaration declares a member function or member function
2267e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   template of a class X, the expression this is a prvalue of type
2268e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2269e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   and the end of the function-definition, member-declarator, or
2270e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   declarator.
2271e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  CXXRecordDecl *ThisContext = 0;
2272e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned ThisTypeQuals = 0;
2273e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2274e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisContext = Method->getParent();
2275e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisTypeQuals = Method->getTypeQualifiers();
2276e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2277e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
2278e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                   SemaRef.getLangOpts().CPlusPlus0x);
2279e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2280e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // The function has an exception specification or a "noreturn"
2281e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // attribute. Substitute into each of the exception types.
2282e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  SmallVector<QualType, 4> Exceptions;
2283e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2284e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // FIXME: Poor location information!
2285e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (const PackExpansionType *PackExpansion
2286e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2287e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // We have a pack expansion. Instantiate it.
2288e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2289e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2290e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                              Unexpanded);
2291e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(!Unexpanded.empty() &&
2292e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith             "Pack expansion without parameter packs?");
2293e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2294e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Expand = false;
2295e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool RetainExpansion = false;
2296e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      llvm::Optional<unsigned> NumExpansions
2297e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                        = PackExpansion->getNumExpansions();
2298e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2299e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  SourceRange(),
2300e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Unexpanded,
2301e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  TemplateArgs,
2302e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Expand,
2303e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  RetainExpansion,
2304e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  NumExpansions))
2305e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2306e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2307e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!Expand) {
2308e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // We can't expand this pack expansion into separate arguments yet;
2309e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // just substitute into the pattern and create a new pack expansion
2310e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // type.
2311e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2312e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2313e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2314e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2315e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull())
2316e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2317e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2318e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2319e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2320e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        continue;
2321e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2322e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2323e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Substitute into the pack expansion pattern for each template
2324e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Invalid = false;
2325e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2326e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2327e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2328e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2329e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2330e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2331e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull()) {
2332e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          Invalid = true;
2333e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2334e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        }
2335e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2336e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2337e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2338e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2339e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (Invalid)
2340e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2341e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2342e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2343e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2344e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2345e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    QualType T
2346e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2347e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                          New->getLocation(), New->getDeclName());
2348e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (T.isNull() ||
2349e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2350e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2351e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2352e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Exceptions.push_back(T);
2353e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2354e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Expr *NoexceptExpr = 0;
2355e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2356e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
2357e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                 Sema::ConstantEvaluated);
2358e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2359e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable())
2360e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2361e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2362e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable()) {
2363e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      NoexceptExpr = E.take();
2364e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!NoexceptExpr->isTypeDependent() &&
2365e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          !NoexceptExpr->isValueDependent())
2366ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        NoexceptExpr
2367ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2368ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              0, diag::err_noexcept_needs_constant_expression,
2369ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              /*AllowFold*/ false).take();
2370e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2371e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2372e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2373e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Rebuild the function type
2374e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionProtoType *NewProto
2375e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    = New->getType()->getAs<FunctionProtoType>();
2376e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  assert(NewProto && "Template instantiation without function prototype?");
2377e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2378e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2379e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2380e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NumExceptions = Exceptions.size();
2381e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.Exceptions = Exceptions.data();
2382e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NoexceptExpr = NoexceptExpr;
2383e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2384e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2385e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                               NewProto->arg_type_begin(),
2386e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                               NewProto->getNumArgs(),
2387e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                               EPI));
2388e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2389e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2390e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithvoid Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2391e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                    FunctionDecl *Decl) {
239213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
239313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  if (Proto->getExceptionSpecType() != EST_Uninstantiated)
2394e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2395e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2396e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2397e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                             InstantiatingTemplate::ExceptionSpecification());
2398e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Inst)
2399e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2400e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2401e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Enter the scope of this instantiation. We don't use
2402e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // PushDeclContext because we don't have a scope.
2403e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::ContextRAII savedContext(*this, Decl);
2404e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  LocalInstantiationScope Scope(*this);
2405e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2406e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
2407e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2408e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
240913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  FunctionDecl *Template = Proto->getExceptionSpecTemplate();
241013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
2411e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
241213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  ::InstantiateExceptionSpec(*this, Decl,
241313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             Template->getType()->castAs<FunctionProtoType>(),
241413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             TemplateArgs);
2415e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2416e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
24171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2418e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2419e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2420e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
24211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
24221eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2423e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
242485f485a70fbec54c9b4562dfc4d95188ea6c9b48David Blaikie  if (Tmpl->isDeleted())
242510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
24261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2427cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2428cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2429cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
24301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
24311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2432cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2433cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2434cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2435cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2436cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2437cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2438cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
24391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
2440cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
24411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2442cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2443bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2444cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2445cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
2446cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
2447cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
24481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
24500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
24510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
245260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
2453e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2454e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2455e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // DR1330: In C++11, defer instantiation of a non-trivial
2456e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
2457e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (SemaRef.getLangOpts().CPlusPlus0x &&
2458e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_None &&
2459e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_DynamicNone &&
2460e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_BasicNoexcept) {
246113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      FunctionDecl *ExceptionSpecTemplate = Tmpl;
246213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      if (EPI.ExceptionSpecType == EST_Uninstantiated)
246313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith        ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
246413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2465e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Mark the function has having an uninstantiated exception specification.
2466e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      const FunctionProtoType *NewProto
2467e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        = New->getType()->getAs<FunctionProtoType>();
2468e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(NewProto && "Template instantiation without function prototype?");
2469e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI = NewProto->getExtProtoInfo();
2470e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecType = EST_Uninstantiated;
2471e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecDecl = New;
247213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
2473e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2474e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                   NewProto->arg_type_begin(),
2475e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                   NewProto->getNumArgs(),
2476e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                   EPI));
2477e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    } else {
2478e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2479e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
24800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
24810ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
248219f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
2483e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionDecl *Definition = Tmpl;
248419f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
248519f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
248623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
248723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
24887cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2489e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2490e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2491e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
24925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
24935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
24945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
24955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
24965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
24971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
24981eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
24995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2500e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2501e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
25021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2504e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
250585606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
25065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
25075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
25085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
25095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
25105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2511a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2512a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2513a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2514a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2515b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2516b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2517b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2518b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2519a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2520b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2521b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2522b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2523b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2524b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2525e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2526e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2527e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2528e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2529f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2530b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2531e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2532e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
253310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
253454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
253554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2536af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
2537af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
2538af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2539af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
2540251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
25416cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
25421eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
25433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
2544f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
2545f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
2546f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2547f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
2548f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
2549f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
2550f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
2551dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
2552f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
25531eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
25548387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
2555f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
25568a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
25578387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
25588387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
25598387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
25608387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
25618387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
25628387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Call the LateTemplateParser callback if there a need to late parse
2563a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
2564f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
25658387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
25664a47e8d35dc1778ef7e428d9edd7676be67e725fFrancois Pichet    LateTemplateParser(OpaqueParser, PatternDecl);
25678387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
25688387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
25698387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
2570f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
2571e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2572e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2573a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2574e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2575e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2576e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2577a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2578e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2579e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2580a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2581e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2582a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
2583e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2584cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
258558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
258658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
258762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
258858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2589e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
259058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
25911eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2592e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
25931eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2594d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2595d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
25961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2597d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
25981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2599d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
26007ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2601d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
26021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2603f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2604f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2605a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
2606a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2607e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
2608e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2609e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
2610b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2611b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2612b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
26135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
261462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
26152a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
26162a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
261762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
26182a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
26191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2620a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
2621f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2622d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2623e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
262454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
262560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
262660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
262760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
262860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
262960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
263060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
263160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
263260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
26331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26347c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith  // Enter the scope of this instantiation. We don't use
26357c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith  // PushDeclContext because we don't have a scope.
26367c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith  Sema::ContextRAII savedContext(*this, Function);
26377c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
26387c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith  MultiLevelTemplateArgumentList TemplateArgs =
26397c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
26407c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
2641e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
2642e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                   TemplateArgs);
264354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2644cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (PatternDecl->isDefaulted()) {
2645cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, 0, /*IsInstantiation=*/true);
26461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2647cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SetDeclDefaulted(Function, PatternDecl->getLocation());
2648cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  } else {
2649cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
2650cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
2651cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2652cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2653cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
2654cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
2655cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2656cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
2657cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2658cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2659cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
2660cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
2661a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2662cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
2663cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
2664cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
2665b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
26660c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
26670c01d18094100db92d38daa923c95661512db203John McCall
2668eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  savedContext.pop();
2669aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2670aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2671aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
26721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
267360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
267460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
267562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
267660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
267760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2678b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
26792a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
26802a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
26812a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2682b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
26831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
268462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
26851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26862a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
26878155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
26888155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
26892a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
26902a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2691b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
26928155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
26938155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
269462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2695b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2696a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2697a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2698a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2699a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2700a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
27017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
27027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
27037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
27047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
27057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
27067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
27077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
27087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
27097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2710e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2711e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2712e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2713e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
27147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
27157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
27167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2717e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2718e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
27197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
27207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
27211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
27237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
27247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
2725a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(Def->isStaticDataMember() && "Not a static data member?");
27260d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
27271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27280d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
27297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
27307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
27311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
27321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2733e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
27340d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2735a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      Diag(PointOfInstantiation,
2736e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2737e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2738e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
273958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
274058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
274162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
274258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
274358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
274458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
27457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
27467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
27477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2748234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2749234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola
2750251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2751234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitSpecialization)
2752251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2753a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2754251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2755251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2756251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2757251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
2758234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitInstantiationDeclaration)
2759251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
27601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2761025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola  Consumer.HandleCXXStaticMemberVarInstantiation(Var);
2762025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola
2763f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor  // If we already have a definition, we're done.
276495e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  if (VarDecl *Def = Var->getDefinition()) {
276595e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    // We may be explicitly instantiating something we've already implicitly
276695e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    // instantiated.
276795e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
276895e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky                                       PointOfInstantiation);
2769f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor    return;
277095e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  }
2771f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
27727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
27737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
27747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
27751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
27777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
27787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
27795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
278062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
27818155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
27828155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
278362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
27848155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
27851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
27877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
2788f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  ContextRAII previousContext(*this, Var->getDeclContext());
27897bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  LocalInstantiationScope Local(*this);
27907bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
27911028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2792ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
27936bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
2794f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
2795f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  previousContext.pop();
27967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
27977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2798583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2799583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2800583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2801583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
28027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
28037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
28047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
28057bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  Local.Exit();
28067bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
28077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
28088155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
28098155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
28108155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
28117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
28121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
281362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
28141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28158155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
28168155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
28178155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded, "
28188155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
28198155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
28208155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
28217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
28228155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
28238155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded, "
28248155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
282562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
28261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2827a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2828815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2829090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2830090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2831090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2832090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
28331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
283490ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
28359db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
2836a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2837090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2838090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
283972f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
284072f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2841cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
2842090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2843030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2844030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2845030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2846030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2847030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
28483fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
2849a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
28503fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
28513fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
285276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
28535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
28543fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
28553fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
2856d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
2857cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      llvm::Optional<unsigned> NumExpansions;
2858a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
28593fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
2860a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
2861a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
2862d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
28633fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
28643fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
28653fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
28663fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
28673fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
28683fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
2869a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2870a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
2871cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
28723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
28733fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
28743fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
28755b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
28765b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                               /*CXXDirectInit=*/true);
28775b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        if (TempInit.isInvalid()) {
28783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
28793fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
28803fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
28813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
28823fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
288376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
2884a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
2885a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
28863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
28873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
28883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
28893fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
28903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
28913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
28923fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
28936df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
28945b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                                     BaseTInfo, TempInit.take(),
28953fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
28963fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
28973fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
28983fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
28993fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
29003fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
2901a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
29023fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
29033fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
2904a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
29053fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
29063fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
29073fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
29086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
29095b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
29105b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                           /*CXXDirectInit=*/true);
29115b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (TempInit.isInvalid()) {
29126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
29136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2914090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2915a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2916090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
291776852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
291876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
291976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
292076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
292176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
292276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
29239db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2924802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2925802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2926802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
29276df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
292876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
29295b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
293076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
293176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
29325b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
293376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
2934090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
2935b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
293600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
293700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
293800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
2939b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
2940b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2941b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
2942b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
2943b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
29441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29455b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(Member, TempInit.take(),
29466df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
294700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
294800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
2949b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
295000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
295100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
295200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
2953b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
2954b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2955b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
29566df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
2957b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
29586df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
29595b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
29606df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
2961090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2962090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
29639db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
29649db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2965090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
29669db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
296790ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
2968090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2969090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
29701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2971090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2972d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2973090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2974090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
29759db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
29769db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2977090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2978090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
29795b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian RedlExprResult Sema::SubstInitializer(Expr *Init,
29805b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                          const MultiLevelTemplateArgumentList &TemplateArgs,
29815b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                          bool CXXDirectInit) {
29825b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  // Initializers are instantiated like expressions, except that various outer
29835b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  // layers are stripped.
29845b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  if (!Init)
29855b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    return Owned(Init);
29865b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
29875b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
29885b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    Init = ExprTemp->getSubExpr();
29895b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
29905b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
29915b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    Init = Binder->getSubExpr();
29925b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
29935b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
29945b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    Init = ICE->getSubExprAsWritten();
29955b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
29965b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  // If this is a direct-initializer, we take apart CXXConstructExprs.
29975b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  // Everything else is passed through.
29985b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  CXXConstructExpr *Construct;
29995b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  if (!CXXDirectInit || !(Construct = dyn_cast<CXXConstructExpr>(Init)) ||
30005b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      isa<CXXTemporaryObjectExpr>(Construct))
30015b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    return SubstExpr(Init, TemplateArgs);
30025b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
30035b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  ASTOwningVector<Expr*> NewArgs(*this);
30045b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  if (SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
30055b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                 TemplateArgs, NewArgs))
30065b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    return ExprError();
30075b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
30085b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  // Treat an empty initializer like none.
30095b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  if (NewArgs.empty())
30105b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    return Owned((Expr*)0);
30115b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
30125b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  // Build a ParenListExpr to represent anything else.
30135b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  // FIXME: Fake locations!
30145b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  SourceLocation Loc = PP.getLocForEndOfToken(Init->getLocStart());
30155b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  return ActOnParenListExpr(Loc, Loc, move_arg(NewArgs));
30165b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl}
30175b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl
301852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
301952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
302052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
302152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
302252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
302352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
302452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
302552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
302652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
302752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
302852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
302952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
303052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
303152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
303252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
30330d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
30340d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
30350d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
3036a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
30370d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
30380d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
30390d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
30400d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
30410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
3042a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
30430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
30440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
30450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3046a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
3047ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3048ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
3049a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
3050ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3051ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
3052ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
3053ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
3054ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
3055ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
3056ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
3057ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
3058a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3059ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
3060ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
3061ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
306252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
306352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
306452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
306552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
306652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
306752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
306852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
306952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
307052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
307152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
307252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
307352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
307452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
307552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
307652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
307752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
307852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
307952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
308052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
308152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
308252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
308352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
308452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
308552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
308652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
308752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
308852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
308952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
309052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
309152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
309252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
309352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
309452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
309552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
309652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
309752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
309852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
309952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
310052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3101ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
3102ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
3103ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3104ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3105ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3106ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3107ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
3108ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
3109ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3110ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3111ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3112ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
31137ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
31147ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
31157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
3116ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
31177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
31187ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
31197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
31200d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
31210d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
3122ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
31230d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
31240d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
312552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
312652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
312752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
312852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
312952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
313052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
313152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
313252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
313352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
313452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
313552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
313652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
313752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
313852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
313952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3140ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
3141ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
3142815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
31430d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
31447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
31457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
31467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
31477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
31487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
31497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
31507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
31517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
31527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
31530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
31540d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
31550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
31560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
3157815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
31580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
31590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
31601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
316152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
316252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
31631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
316452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
316552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
3166815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
316752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
316852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
3169815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
31707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
317152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
317252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
317352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
317452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
317552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
3176a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
31770d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
31780d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
31790d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3180ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
3181ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3182ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3183ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
3184ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
3185d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3186d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
3187d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
31881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
3189d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
3190d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
3191d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
31921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3193ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3194ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3195ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3196ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3197ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3198ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3199815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
3200815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3201815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3202815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3203815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
32041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
3205815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
3206815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
3207815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
3208815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
3209815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
3210815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
3211815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3212815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
3213815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3214815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
321502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
321602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
321702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
321802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
32197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
3220e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
322102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
32227c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
322302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
322402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
322502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
322602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
3227ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
3228ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
3229815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3230815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
3231815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
3232815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
3233815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
3234815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3235815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
3236815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
3237815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
3238815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
3239815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
3240815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
3241815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3242815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
3243815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
3244815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3245815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
3246815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
3247815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3248815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
3249815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
3250f198d126ecec1e07a474c419de1bf4dadf92a490James Dennett/// X<T>::\<Kind>\::KnownValue) to its instantiation
3251ef2b5b327b524f9ea3243c07e04fb24706e63120James Dennett/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
3252ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
32537c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
3254e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
3255815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
3256550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
32576d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
32587bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
32597bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
32602bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
32612bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
3262d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3263d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3264d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
3265a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
326657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
326757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
326857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
3269a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
327057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      unsigned PackIdx = ArgumentPackSubstitutionIndex;
327157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
327257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
327357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
327457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
327557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
327657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
3277a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
327857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
327957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
3280a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
328157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
328257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
32832bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
3284815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3285e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3286e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
3287e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
3288a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
32892c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
32902c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
3291e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
32922c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
32932c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
32942c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
32952c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
32962c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
32972c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
32982c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
32992c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
33002c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
33012c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
33022c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
33032c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
33042c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
33052c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
33062c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
33072c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
33082c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
33092c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
33102c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
33112c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
33122c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
33132c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
33142c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
33152c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
33162c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
33172c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
33182c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
33192c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
3320e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
33212c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
33222c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
33232c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
33242c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
33252c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
33262c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
33272c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
33282c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
332952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
33302c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
33312c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
333252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
33338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
3334e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
3335e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
3336e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
333752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3338e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
3339e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
3340a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
33417c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
33421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
334344c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
33441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3345815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
3346815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
3347815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
3348815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
33497c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
33503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
33513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
3352eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
33533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
33547c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
33557c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
33563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
33573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
3358eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
3359eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
33603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
33613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
33623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
3363a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
3364a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
33657c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
33667c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
33677c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
3368815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
3369815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
337017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
3371815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
3372815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
3373815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
3374815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
3375815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
3376815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3377815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
3378815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
3379815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3380815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
33811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
338217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
338317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
3384815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
33851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3386eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
3387eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
3388eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
3389eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
3390eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
3391eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
3392eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
3393eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
3394a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
3395eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
3396eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
3397eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
3398eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
3399eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
3400eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
3401eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3402eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
34030724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith      } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
34040724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // This enumeration constant was found when the template was defined,
34050724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // but can't be found in the instantiation. This can happen if an
34060724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // unscoped enumeration member is explicitly specialized.
34070724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
34080724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
34090724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                                                             TemplateArgs));
34100724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        assert(Spec->getTemplateSpecializationKind() ==
34110724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                 TSK_ExplicitSpecialization);
34120724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Loc, diag::err_enumerator_does_not_exist)
34130724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << D->getDeclName()
34140724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
34150724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Spec->getLocation(), diag::note_enum_specialized_here)
34160724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(Spec);
3417eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
3418eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
3419eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
3420eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
3421eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
3422a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3423815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
3424815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
3425815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3426815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
3427815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3428d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
34291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
3430d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
34318155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
34326e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
34336e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
34346e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    SmallVector<std::pair<ValueDecl *, SourceLocation>, 4> Pending;
34356e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
34366e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
34376e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
34386e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
3439a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
344060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
344162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
344260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
344360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
344460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
344562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
344662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
344760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
344860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
344960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
345060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
34511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
34537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3454f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3455f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
345658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
345758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
345858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
345958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
34607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
34617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
34621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
34647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
34657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
3466c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
3467291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
3468291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
3469ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
3470291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
3471291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3472291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
3473291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
3474ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
3475291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
3476b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
3477291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
3478291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
347958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
348058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
348158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
348258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
3483ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
3484291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
3485291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
3486291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
3487291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3488f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3489f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
3490f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
34911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
349258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
349358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
349458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
349558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
3496d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
3497d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
34980c01d18094100db92d38daa923c95661512db203John McCall
34990c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
35000c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
35010c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
35020c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
35030c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
35040c01d18094100db92d38daa923c95661512db203John McCall
35050c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
35060c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
35070c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
35080c01d18094100db92d38daa923c95661512db203John McCall      break;
35090c01d18094100db92d38daa923c95661512db203John McCall    }
35100c01d18094100db92d38daa923c95661512db203John McCall  }
35110c01d18094100db92d38daa923c95661512db203John McCall}
3512