SemaTemplateInstantiateDecl.cpp revision 23323e0253716ff03c95a00fb6903019daafe3aa
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())
82cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
83f6702a3927147655206ae729a84339c4fda4c651Richard Smith        } else {
84cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
857663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             TemplateArgs,
86a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                             Aligned->getLocation(),
877663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             DeclarationName());
88cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (Result)
89cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result);
90cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
914ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        continue;
924ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      }
934ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
944ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
9523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    if (TmplAttr->isLateParsed() && LateAttrs) {
9623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // Late parsed attributes must be instantiated and attached after the
9723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // enclosing class has been instantiated.  See Sema::InstantiateClass.
9823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LocalInstantiationScope *Saved = 0;
9923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (CurrentInstantiationScope)
10023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
10123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
10223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    } else {
10323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      Attr *NewAttr =
10423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        instantiateTemplateAttribute(TmplAttr, Context, *this, TemplateArgs);
10523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      New->addAttr(NewAttr);
10623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
107d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
108d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
109d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1104f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1114f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
112b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
1134f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1144f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1154f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
11657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
11757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
11857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
11957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
12057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
12157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
12257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
12357ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1244f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
125b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
1264f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1274f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1283dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1293dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1303dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1313dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1323dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1333dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1340cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1350cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1363dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1373dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1383dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1393dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1403dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1413dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1423e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
1433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
1448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
145a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
146561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
147836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
148ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
149ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
150ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
152a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
154b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
155b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
159162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
160162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
161162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
162162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
163162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
164162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
165162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
1668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
169cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
170cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
171cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
172cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
173162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (oldTag->getTypedefNameForAnonDecl() == D) {
174cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
175162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl());
176162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
177cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
178d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
179a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
180ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
1817c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1827c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
183b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
184b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
185a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1865df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
1875df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
1885df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    // If the typedef types are not identical, reject them.
1895df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
1905df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
1915df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    Typedef->setPreviousDeclaration(InstPrevTypedef);
1925126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1935126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
1941d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
195d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
19646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
201162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
2033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
205162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
206162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
207162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
2083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
2093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
2113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
2123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
2143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2153e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
2163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
2173e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
2183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
2203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
2223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
2253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *PrevAliasTemplate = 0;
227ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (Pattern->getPreviousDecl()) {
2283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
2293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (Found.first != Found.second) {
2303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(*Found.first);
2313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
2323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2343e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
2353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
2363e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
2373e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2383e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
2403e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2413e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
2423e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
2433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setPreviousDeclaration(PrevAliasTemplate);
2443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
2463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
2483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
249a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
2513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2523e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
253162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
254162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2566b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2576b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2660ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smithbool Sema::InstantiateInitializer(Expr *Init,
2676b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2680ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                  SourceLocation &LParenLoc,
2690ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                  ASTOwningVector<Expr*> &NewArgs,
2700ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                  SourceLocation &RParenLoc) {
2716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2784765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
2900ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith    return SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
2910ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                      true, TemplateArgs, NewArgs);
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
29528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
2960ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith      if (SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
2970ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                     TemplateArgs, NewArgs))
29828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
29928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
30028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
3010ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith      LParenLoc = PP.getLocForEndOfToken(Init->getLocStart());
302a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor      RParenLoc = LParenLoc;
30328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
30428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
306a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3070ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith  ExprResult Result = SubstExpr(Init, TemplateArgs);
3086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
3096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
3106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
3126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
3136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3153d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
3169901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3179901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3189901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3199901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3209901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3219901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3229901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
323ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
324a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3250a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3260a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3270a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3280a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3303d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
331c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
332c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
333c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
334c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
335c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
336a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
337b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3383d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
339ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 D->getInnerLocStart(),
3403d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3410a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
34216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
34316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
346ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
347796c1a1e3e63e459e371383ac878aa5f40b02a8cRichard Smith  Var->setConstexpr(D->isConstexpr());
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
349b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
350b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
351b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
352b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
360a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3616b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (!D->isStaticDataMember()) {
362c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3636b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    Var->setReferenced(D->isReferenced());
3646b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  }
365a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
366390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
367390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3686826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3696826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
370449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
37160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
37260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3739aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3749aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
3759aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  if (SemaRef.getLangOptions().ObjCAutoRefCount &&
3769aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
3779aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
3789aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3792c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain  SemaRef.CheckVariableDeclaration(Var, Previous);
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
3823e9ea0b8cd7c4691d62e385245556be5fded58a7Richard Smith    D->getLexicalDeclContext()->addDecl(Var);
3837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
386f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
387f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3891d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
390a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
391251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
392251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
393251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
394a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
395cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
396a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
39760c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
39860c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
39960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
4001f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
401f6702a3927147655206ae729a84339c4fda4c651Richard Smith      SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated);
4021f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
4031f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
4041f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
4066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
407ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> InitArgs(SemaRef);
4080ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith    if (!SemaRef.InstantiateInitializer(D->getInit(), TemplateArgs, LParenLoc,
4090ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                        InitArgs, RParenLoc)) {
41034b41d939a1328f484511c6002ba2456db879a29Richard Smith      bool TypeMayContainAuto = true;
4116aeaa60217e1ed11a621409acf1b53df0d14b591Eli Friedman      if (D->hasCXXDirectInitializer()) {
4126eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
413d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
4146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
4156eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
41634b41d939a1328f484511c6002ba2456db879a29Richard Smith                                              RParenLoc,
41734b41d939a1328f484511c6002ba2456db879a29Richard Smith                                              TypeMayContainAuto);
4186aeaa60217e1ed11a621409acf1b53df0d14b591Eli Friedman      } else if (InitArgs.size() == 0) {
4196aeaa60217e1ed11a621409acf1b53df0d14b591Eli Friedman        SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
42007a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      } else {
42107a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor        assert(InitArgs.size() == 1);
4229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        Expr *Init = InitArgs.take()[0];
42334b41d939a1328f484511c6002ba2456db879a29Richard Smith        SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
42483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
4256eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
430a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4311f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
432ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
433ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith             !Var->isCXXForRangeDecl())
434d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
4353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
436e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // Diagnose unused local variables with dependent types, where the diagnostic
437e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // will have been deferred.
438e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
439e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith      D->getType()->isDependentType())
4405764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
441bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
4423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4433d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4456206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4466206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4476206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4486206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4496206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4506206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4516206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4526206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
455a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
456561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
457836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
45807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
45907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
46007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
461a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
46207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
46307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
47107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
474b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
475b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
482f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
483f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
484f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
487ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
492e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
49507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
49607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
5008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
5017a614d8380297fcd2bc23986241905d97222948cRichard Smith                                            D->hasInClassInitializer(),
502ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
5038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
5048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
505663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
506663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
507f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
508663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
511a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
512f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
513f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
515f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
516f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
517f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
518a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
5199901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
5209901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
5217a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
5229901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
525f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
52646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
527f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
53287c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
53387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
53487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
53587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
53787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
53887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
539b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
540a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
541b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
542b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
543b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
544a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
545b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
546b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
54787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
54840e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
54987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
55087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
55140e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
55287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
55387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
55487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
55587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
55687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
55787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
55887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
55987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
56087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
56102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
56202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
56306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
56432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
5654fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5664fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5674fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5684fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5694fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5704fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5714fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5724fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5734fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5744fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5754fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5764fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
57706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
578fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5790216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocation(),
5800216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
58106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
58206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
583a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
58406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5859a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
58606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
58706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
588a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
589a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
59006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
59106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
59232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
593af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
594af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
595af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
596af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
597af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
59806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
601a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
60206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
6035fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
6049a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
60502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
60602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
607fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
608fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
6098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
6108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
612f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
613f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
614f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
617ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
6188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
6198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
6208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
62160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Message(D->getMessage());
6223fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall  D->getMessage();
623d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
6249ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
625a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara                                              Message.get(),
626a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara                                              D->getRParenLoc());
6278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
630ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
632a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    /*PrevDecl=*/0, D->isScoped(),
633a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
6351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
6361274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6371274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6381274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6391274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6401274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
6411274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       TemplateArgs,
6421274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       UnderlyingLoc,
6431274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       DeclarationName()));
644a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
6451274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      if (!Enum->getIntegerTypeSourceInfo())
6461274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
6471274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6481274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    else {
6491274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6501274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6511274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6521274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6531274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6541274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6555b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
6565b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
6578dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
65806c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
659b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
66017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
6618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
6628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
66396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
66496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
665a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
6665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
6678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
66917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
67017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
6718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
67360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
674ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
675f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
677f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
679ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
680ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
6868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
6878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6929ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
6938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
7015b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
7025b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
7033b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
70417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
705d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
7068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
707a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
70896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
70996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
71096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
71196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
71296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
7138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
716c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
717fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
718c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
719d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
720de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
721fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
7228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
7248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7266477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
727b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7286477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7296477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
733550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
734550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
7352a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
736e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
737ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
739d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
740e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
74393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
74593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
746c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
747c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
748c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
749c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
750c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
751c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
75293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
75493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
757ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
75837574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
75937574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (Found.first != Found.second) {
76037574f55cd637340f651330f5cfda69742880d36Nick Lewycky      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
76137574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
76237574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
76337574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
76437574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
76537574f55cd637340f651330f5cfda69742880d36Nick Lewycky
76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
77093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
77193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
772c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
774c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
77593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
77793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
77993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
78193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
78293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
78393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
78493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
78593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
78693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
78793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
79193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
79393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
79493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
795c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
79693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7971eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
798c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
79993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
802c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
804c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
805c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
806c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
807c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
808c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
809c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
810c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
811c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
812a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
813c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
814c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
815c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
816c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
817c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
818c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
819c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
820c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
821c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
822c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
823c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
824c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
825c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
826c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
827c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
828c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
829c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
83093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
83193ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
83293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
83393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
83493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
835a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
836c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
837c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
838c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
839c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
840c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
841c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
842c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
84393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
84493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
84593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
846c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
847c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
84893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
84993ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
85093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
85193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
85293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
853e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
85493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
855ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
856ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
857f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
858e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
859c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
860c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
861b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
862e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
86393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
86493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
86593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
866e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
867ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
86893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
869ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
870ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
871ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
872ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
873ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
87493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
87593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
87693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
87793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
878e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
87937574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
88037574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
88193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
882a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
883f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
88524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
886ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
887259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
88893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
88993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
8904c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
8914c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
892e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
893259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
894a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
8954c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
8964c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
8974c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
8984c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
8994c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
900e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
901d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
902d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
903d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
904d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
905d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
9065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
907d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
908d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
909d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
910d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
911d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
912d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
913e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
914e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
915e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
916d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
9177974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
9187974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
919ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
920a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
921ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
922ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
923ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
924ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
925ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
926ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
927a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
928ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
929ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
930ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
931ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
932a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
933d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
934d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
935d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
936d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
937d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
9387974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
9397974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
9407974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
941d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
942550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
943550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
944a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
945550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
9462a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
947895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
948d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
949d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
9501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
951d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
952a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
953a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
954a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
955a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
956a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
957a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
958a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
959a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
960a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
961a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
962a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
963d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
964d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
96546460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
96646460a68f6508775e98c19b4bb8454bb471aac24John McCall
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
968d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
969a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
970a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
97137d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
972a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
973a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
974e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
975b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
976b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
977e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
978e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
979e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
980b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
981a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
982a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
983b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
984b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
985a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
986b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
987d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
988d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
989d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
990d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
991d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
992d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
993d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
994ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
9957c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
996ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
9976c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9986c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9996c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
10006c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
1001d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
1002d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1004ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
1005ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
1006b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1007b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1008b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
1009b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1010b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1011d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
1012eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
1013eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
1014eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
1015eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
1016eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
1017d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
1018f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1019d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
102002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
102102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
102202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
102302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
102402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
10259901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
10269901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
10279901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
10287a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
10299901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
10309901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
1031d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
103217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
1033d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1034d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1035d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
103602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
103702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
103802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
103902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
104002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
10417557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1042a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1043127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1044127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1045127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1046127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
1047b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1048a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
104924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
10501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10512c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
10522c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
10532c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1055127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
10562c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
10572c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1058127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1060b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1061b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1062b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1063b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1064b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1065b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
106679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1067b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1068a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
106979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
10702a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
107364b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
107421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
10752dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
107621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
1077fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1078c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1079c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1080c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1081c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1082c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1083c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1084d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1085d325daa506338ab86f9dd468b48fd010673f49a6John McCall
108668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
108768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
108868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
108968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
109068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1091c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  else if (isFriend && QualifierLoc) {
1092d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1093c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1094d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1095d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1096d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1097a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10987c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1099d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
110068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
110102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1102ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1103ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                           D->getLocation(), D->getDeclName(), T, TInfo,
110416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
1105af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
11069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                           /*isConstexpr*/ false);
1107b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1108c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1109c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1110b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1111b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1112b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1113b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1114b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1115b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1116b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1117b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1119e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
11205cbe101b502e06d16bc77df45a27ce8bc13f33c8Douglas Gregor  if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
11211d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Adopt the already-instantiated parameters into our own context.
11221d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (unsigned P = 0; P < Params.size(); ++P)
11231d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      if (Params[P])
11241d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        Params[P]->setOwningFunction(Function);
11251d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  } else {
11261d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Since we were instantiated via a typedef of a function type, create
11271d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // new parameters.
11281d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    const FunctionProtoType *Proto
11291d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      = Function->getType()->getAs<FunctionProtoType>();
11301d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    assert(Proto && "No function prototype in template instantiation?");
11311d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
11321d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor         AE = Proto->arg_type_end(); AI != AE; ++AI) {
11331d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      ParmVarDecl *Param
11341d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
11351d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor                                             *AI);
11361d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Param->setScopeInfo(0, Params.size());
11371d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Params.push_back(Param);
11381d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    }
11391d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  }
11404278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
114102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1142ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1143a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1144a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1145a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1146a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1147a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1148a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1149a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1150a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1151a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1152a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1153a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1154a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1155a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1156a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1157a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1158d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1159a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1160a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1161a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1162a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1163b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1164b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1165d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1166d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1167d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1168d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1169d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1170d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1171d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
117266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
117366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1174a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
117524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1176838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1177910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
117824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
117924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
118066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
118180f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
118280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
118380f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
118480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
118580f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
118680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
118780f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1188d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
118902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1190a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1191e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1192e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1194af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1195a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
11966826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
11976826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
11986826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1199af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1200af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1201af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1202af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1203af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1204af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1205af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1206af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1207af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1208af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1209e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1210e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1211e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1212af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1213af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1214af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1215af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1216af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1217af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1218af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1219af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1220af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1221af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1222af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1223af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1224af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1225af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1226af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1227a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1228af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1229af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1230af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1231a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1232a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1233a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
12346826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1235a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1236a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1237a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1238a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1239a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
12406826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
12416826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1242a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1243a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
12449f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
12452c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1246e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
124776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
124876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
124976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
125076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1251a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1252a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1253d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
12546826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
125576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1256ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = FunctionTemplate->getPreviousDecl();
125776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1258ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = Function->getPreviousDecl();
125976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
126076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
126176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1262ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
126377535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1264ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
126553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++98 [temp.friend]p5: When a function is defined in a friend function
126653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is defined at each
126753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   instantiation of the class template. The function is defined even if it
126853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   is never used.
126953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++11 [temp.friend]p4: When a function is defined in a friend function
127053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is instantiated when the
127153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   function is odr-used.
127253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //
127353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // If -Wc++98-compat is enabled, we go through the motions of checking for a
127453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // redefinition, but don't instantiate the function.
127553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    if ((!SemaRef.getLangOptions().CPlusPlus0x ||
127653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith         SemaRef.Diags.getDiagnosticLevel(
127753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             diag::warn_cxx98_compat_friend_redefinition,
127853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             Function->getLocation())
127953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith           != DiagnosticsEngine::Ignored) &&
1280238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1281238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1282238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
128310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1284238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
128553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        SemaRef.Diag(Function->getLocation(),
128653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                     SemaRef.getLangOptions().CPlusPlus0x ?
128753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::warn_cxx98_compat_friend_redefinition :
128853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::err_redefinition) << Function->getDeclName();
1289238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
129053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        if (!SemaRef.getLangOptions().CPlusPlus0x)
129153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          Function->setInvalidDecl();
1292a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1293238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1294238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1295238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1296238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1297238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
129813a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
129913a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1300ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1301ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
130253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          if (!SemaRef.getLangOptions().CPlusPlus0x &&
130353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              !queuedInstantiation && R->isUsed(false)) {
1304ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1305ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1306ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1307ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1308ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1309ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1310ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1311ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1312ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1313ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1314ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1315ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1316ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1317238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
13186a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
131910620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
132053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              SemaRef.Diag(Function->getLocation(),
132153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                           SemaRef.getLangOptions().CPlusPlus0x ?
132253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::warn_cxx98_compat_friend_redefinition :
132353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::err_redefinition)
1324238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
13256a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
132653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              if (!SemaRef.getLangOptions().CPlusPlus0x)
132753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                Function->setInvalidDecl();
1328238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1329238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1330238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1331238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1332238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1333a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1334a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
133576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
133676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
133776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
133876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1339eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1340e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1341e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
13422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1343d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1344d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1345af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1346af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
13476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
13486b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1349d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1352d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1353a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
135424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13562c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
13572c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
13582c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
13591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13606b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
13612c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
13622c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
13636b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
13646b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1365b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1366b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1367b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1368b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1369b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1370b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
137179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1372a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
137379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
13742a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
137548dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
13764eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
13775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
13784eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
13794eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
13804eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
13814eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
13824eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
13834eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
13844eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
13854eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
13864eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
13874eab39f0745fb1949dbb40c4145771b927888242John McCall    }
13884eab39f0745fb1949dbb40c4145771b927888242John McCall  }
13894eab39f0745fb1949dbb40c4145771b927888242John McCall
13905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1391dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
139221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
13932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
139421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
13952dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1396723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1397723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1398723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
13995f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
14005f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
14015f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
14025f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
14035f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
14045f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1405723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
14065f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
14075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 4> ParamTypes;
1408a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1409a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                               D->getNumParams(), TemplateArgs, ParamTypes,
141012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               &Params))
1411a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      return 0;
14125f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
14135f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1414c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1415c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1416c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1417b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1418a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1419c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1420b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1421b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1422b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1423b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1424c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1425b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1426c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1427b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1428c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1429c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1430c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1431b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1432b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1433b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1434b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1435b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1436b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1437b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1438b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
14392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1440b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1441dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1443ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
14442577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
14452577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
144617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1448ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
145016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
14519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                        false, /*isConstexpr*/ false);
145217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
145317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1454ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
14552577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
145616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
145765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
145865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1459ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
14600130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1461f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
14629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       /*isConstexpr*/ false,
14639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1464dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
14652577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1466ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
146716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
146816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
1469f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                   D->isInlineSpecified(),
14709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                   /*isConstexpr*/ false, D->getLocEnd());
1471dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14726b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1473c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1474c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1475b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1476d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1477d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1478d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1480d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1481d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1482d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1483d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1484d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1485d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1486d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1487d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1488d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1489d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1490d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1491d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1493d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1494b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1495b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1496b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1497b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
14981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1499d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
150066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
150166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1502a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
150324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1504838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1505910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1506910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.first,
1507910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.second),
150866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1509b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
151066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
15112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
151266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1513a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
15141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
15157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
15167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1517b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
15184eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
15194eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
15204eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
15214eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
15224eab39f0745fb1949dbb40c4145771b927888242John McCall
1523b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1524b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1525b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
15267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
15295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
15305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
15314278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
15325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
15345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
15352dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
15362577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
15372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1539b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1540b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1542dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1543dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1544dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1545dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
15466826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
15476826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1548dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
15492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1550af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
15512c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
155265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
15534ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
15544ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
15554ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
155646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
155746460a68f6508775e98c19b4bb8454bb471aac24John McCall
15589eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
15599eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
15603bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
15613bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDefaulted())
15623bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman    Method->setDefaulted();
15633bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
15643bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman    Method->setDeletedAsWritten();
15653bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
1566b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1567b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1568b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1569b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1570b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1571b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1572b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1573b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1574b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1575b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1576af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    else if (!IsClassScopeSpecialization)
1577b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1578b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1579eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
1580eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  if (D->isExplicitlyDefaulted()) {
1581eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
1582eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  } else {
1583eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt    assert(!D->isDefaulted() &&
1584eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt           "should not implicitly default uninstantiated function");
1585eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  }
1586eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
15872dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
15882dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
15892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1590615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1591dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1592615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1593615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
159403b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
159517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
159603b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
159703b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1598bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
159965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1600bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1601bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
16026477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1603fb44de956f27875def889482b5393475060392afJohn McCall  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
1604fb44de956f27875def889482b5393475060392afJohn McCall                                  llvm::Optional<unsigned>());
16052dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
16062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1607e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1608e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1609e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
16104fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1612e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1613344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1614344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
16154fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
16164fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1617e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1618e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
16199a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1620a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16210f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
1622a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1623e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1624a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1625550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1626550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1627a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1628e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1629e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1630e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
163133642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
163233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
163333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
16346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
16355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
16365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
16376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1638a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
163933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
164033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
16416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
16426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1643a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
16446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
16456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
16466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
16476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
16486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
16496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1650a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
16516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
16526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
16536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1654a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
16566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
16576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
16586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
16596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
16606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
16616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1662a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
16646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
16656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
16666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else if (isa<PackExpansionTypeLoc>(TL)) {
16676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
16686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
16696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
16706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
16716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
16725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
16736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1674a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
16766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
16776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
16786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
16796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> OrigNumExpansions
16806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
16816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
16826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
16836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1684a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
16856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1686a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
16876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
16886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1689a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
16916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
16926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
16936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1694a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
16956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
16966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
16976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1698a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
17006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
17016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
17026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
17036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
17046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
17056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
17066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1707a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
17096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
17106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
17116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
17126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
17136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
17146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
17156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
17166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
17176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
17186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1719a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
17206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
17216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
17226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1723a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
17256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
17266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
17276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1728a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
17306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
17316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
17326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1733a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
17346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
17356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
17366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1737a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1739a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
17406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
17416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
17426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
17436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
17446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
174533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1746a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
17486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1749a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1750ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1751ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1752a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1753a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
17546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
17556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
17566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
17576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
17586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
17596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1760a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1761ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
17626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1763a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1764a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1765a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
17666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1767a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17689a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
176933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
177033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1771a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1772d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1773a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1774a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1775550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1776550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
177733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
177833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
177933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
17800dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
17819106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
17829106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
17839106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
17849106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
17859106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
17869106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
17879106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
17889106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
17892a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
17909106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
17919106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
17929106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
1793a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
1794a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17959106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
17969106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
17979106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1798a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
1799a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                       D->getPosition(), D->isParameterPack(),
180061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getIdentifier(), InstParams);
1801d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
18029a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
1803a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1804a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
18059106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
18069106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1807a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18089106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
18099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
18109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
181148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1812db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
1813db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
1814a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
181548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
181648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1817a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
1818db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
1819a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
1820a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
182148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
182248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
182348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
182448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
182548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1826ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
18271b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
18281b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
18291b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
18301b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
18311b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
18321b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
18331b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
18341b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
18351b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
18365149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
18375149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
18385149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
18395149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
1840dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
18411b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
18421b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
18431b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1844ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1845ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18469f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
18479f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
18489f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
18499f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
18509f54ad4381370c6b771424b53d219e661d6d6706John McCall
1851ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1852ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
18539f54ad4381370c6b771424b53d219e661d6d6706John McCall
1854ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1855ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
18565149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
1857ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1858ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1859ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18605149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
18615149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
18629f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
18639f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
18649f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
18659f54ad4381370c6b771424b53d219e661d6d6706John McCall
18669f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
18679f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
18689f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
18699f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
18709f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
18719f54ad4381370c6b771424b53d219e661d6d6706John McCall
18729f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
18739f54ad4381370c6b771424b53d219e661d6d6706John McCall
18749f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
18759f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1876ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1877ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
18789f54ad4381370c6b771424b53d219e661d6d6706John McCall
1879ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1880ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1881ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1882ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18839f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
18849f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
18859f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
18869f54ad4381370c6b771424b53d219e661d6d6706John McCall
1887323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1888323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
18899f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
18909f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
18919f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
18929f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
18939f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
1894b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
1895b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
1896b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
1897b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
1898b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
1899b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
19009f54ad4381370c6b771424b53d219e661d6d6706John McCall
19019f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
19029f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
19039f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
19049f54ad4381370c6b771424b53d219e661d6d6706John McCall
19059f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
19069f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
19079f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1908323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1909323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1910323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
19119f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1912ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1913ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1914ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1915ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1916ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
19179f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
19189f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1919ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1920ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
19227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
19235149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
1924a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
19255149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
19265149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
19277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
19287ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
19297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
19305149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
19317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1932ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1933ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1934ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
19357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
19367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1937ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
19387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
19397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
19404469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1941ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1942ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
19447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
19457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
19467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
19477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
19485149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
19495149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
19505149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
19510dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
1952a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19530dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
19545149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
19551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1956ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1957ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1958ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
19591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
19609488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1961ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
19627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
19637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
19644469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1965ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1966ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19670d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
19680dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
19690dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1970af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1971af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
1972af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
1973af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
1974af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, 0, true));
1975af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1976af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
1977af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
1978af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1979af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
1980af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, 0, Previous)) {
1981af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
1982af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
1983af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
1984af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1985af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
1986af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
1987af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
1988af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
1989af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1990af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
1991af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1992af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1993ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1994d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
19957e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
19962fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
19972fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
19982fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
19998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
20008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
20018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2002e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
2003e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
2004e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2005e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
2006e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2007e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
2008e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
2009ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2010e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2011e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2012e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2013e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
20145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2015e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2016e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2017e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2018e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2019bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2020e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
20219148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2022e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2023e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2024e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2025ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
2026e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
2027e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2028e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2029e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2030e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2031e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2032e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
20331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2034e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2035a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2036ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2037ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2038ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2039ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2040ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2041a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2042ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2043ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2044d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2045d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2046d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2047ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2048ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2049ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2050550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2051550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2052550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
20532a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2054a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2055ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2056ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2057ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2058ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2059ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2060d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2061a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2062ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2063ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2064d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
2065a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2066a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                    PartialSpec->getNumTemplateArgsAsWritten(),
2067e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2068e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2069a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2070ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2071ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
20725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2073a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2074ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2075a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2076ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2077ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2078d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2079ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2080ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2081ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2082ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2083ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2084910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2085910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2086a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2087ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2088ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2089a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2090ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2091910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2092910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2093ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2094ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2095ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2096ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2097ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2098ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2099ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
21013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
21023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
21033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
21043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2105d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2106ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2107a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2108ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2110ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2111ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2112ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2113ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2114ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2115ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2116ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2117ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2118ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2119ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2120ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2121ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2122ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2123ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2124ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2125d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2126ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2127ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2128d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2129ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2130a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2131a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2132ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2133ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2134a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
213513c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2136a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2137ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2138ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2139ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2140a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2141910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2142910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2143d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
21443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
2145dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
2146cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
2147b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2148b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2149b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2150b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2151ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
21524469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2153a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2154ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2155ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
2156cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
2157d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2158ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2159ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
216021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
216121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
21625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
216321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
216421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
216521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
21666cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
21676cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
21686cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
21696cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
217021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
217121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
21725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2173cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
2174cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
2175140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
21766920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
21776920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2178140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
21796920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
21806920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
218112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
218212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
218312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
218412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
218512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        if (!OldParam->isParameterPack() ||
218612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor            (NewIdx < NumNewParams &&
218712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor             NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
2188a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
218912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
219012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
219112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
219212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
219312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                               NewParam);
219412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          continue;
219512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
2196a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
219712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        // Parameter pack: make the instantiation an argument pack.
219812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
219912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      OldParam);
220021371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        unsigned NumArgumentsInExpansion
220121371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor          = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
220221371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor                                               TemplateArgs);
220321371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        while (NumArgumentsInExpansion--) {
220412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
220512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
220612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
220712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      NewParam);
220812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
22096920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2210895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
2211cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
2212cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
2213cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
2214cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
2215140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
22166920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
22176920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
22186920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
22196920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
22206920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
22216920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
22226920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
22236920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2224cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2225cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
222621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
22275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
22285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
22291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2230e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2231e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2232e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
22331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
22341eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2235e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
223610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Tmpl->isDeletedAsWritten())
223710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
22381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2239cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2240cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2241cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
22421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
22431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2244cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2245cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2246cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2247cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2248cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2249cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2250cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
22511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
2252cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
22531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2254cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2255bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2256cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2257cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
2258f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
2259cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
2260cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
22611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
22630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
22640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
226560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
22660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
22670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
22685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 4> Exceptions;
22690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
22700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
2271b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      if (const PackExpansionType *PackExpansion
2272b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2273b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // We have a pack expansion. Instantiate it.
22745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2275b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2276b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                Unexpanded);
2277a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        assert(!Unexpanded.empty() &&
2278b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor               "Pack expansion without parameter packs?");
227960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2280b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Expand = false;
2281d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        bool RetainExpansion = false;
2282cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        llvm::Optional<unsigned> NumExpansions
2283cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                          = PackExpansion->getNumExpansions();
2284a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2285b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    SourceRange(),
2286a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                    Unexpanded,
2287b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    TemplateArgs,
2288a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                    Expand,
2289d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    RetainExpansion,
2290d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    NumExpansions))
2291b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
229260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2293b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (!Expand) {
2294b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          // We can't expand this pack expansion into separate arguments yet;
2295a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // just substitute into the pattern and create a new pack expansion
2296cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // type.
2297b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2298a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2299b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2300b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2301b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull())
2302b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2303a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2304cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2305b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2306b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          continue;
2307b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
230860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2309b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // Substitute into the pack expansion pattern for each template
2310b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Invalid = false;
2311cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2312b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2313a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2314a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2315b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2316b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2317b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull()) {
2318b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            Invalid = true;
2319b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2320b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          }
232160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2322b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2323b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
232460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2325b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (Invalid)
2326b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
232760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2328b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        continue;
2329b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      }
2330a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
23310ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
23320ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
23330ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
2334a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (T.isNull() ||
23350ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
23360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
23370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
23380ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
23390ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
234056fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    Expr *NoexceptExpr = 0;
234156fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2342f6702a3927147655206ae729a84339c4fda4c651Richard Smith      EnterExpressionEvaluationContext Unevaluated(SemaRef,
2343f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
234456fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl      ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
234556fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl      if (E.isUsable())
23465c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2347f6702a3927147655206ae729a84339c4fda4c651Richard Smith
23485c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor      if (E.isUsable()) {
23495c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        SourceLocation ErrLoc;
23505c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        llvm::APSInt NoexceptVal;
235156fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl        NoexceptExpr = E.take();
23525c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        if (!NoexceptExpr->isTypeDependent() &&
23535c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor            !NoexceptExpr->isValueDependent() &&
23545c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor            !NoexceptExpr->isIntegerConstantExpr(NoexceptVal, SemaRef.Context,
23555c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor                                                 &ErrLoc, /*evaluated=*/false)){
23565c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor          SemaRef.Diag(ErrLoc, diag::err_noexcept_needs_constant_expression)
23575c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor            << NoexceptExpr->getSourceRange();
23585c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor          NoexceptExpr = 0;
23595c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        }
23605c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor      }
236156fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    }
23620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2363a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Rebuild the function type
23640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2365e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
236660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2367e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.NumExceptions = Exceptions.size();
2368e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.Exceptions = Exceptions.data();
236956fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    EPI.NoexceptExpr = NoexceptExpr;
2370e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = Proto->getExtInfo();
2371e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
23720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
23730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
23740ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
23750ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
23760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
23770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
2378e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 EPI));
23790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
23800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
23819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // C++0x [dcl.constexpr]p6: If the instantiated template specialization of
23829f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // a constexpr function template satisfies the requirements for a constexpr
23839f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // function, then it is a constexpr function.
23849f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (Tmpl->isConstexpr() &&
23859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      SemaRef.CheckConstexprFunctionDecl(New, Sema::CCK_Instantiation))
23869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    New->setConstexpr(true);
23879f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
238819f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  const FunctionDecl* Definition = Tmpl;
238919f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
239019f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
239119f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
239219f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
239323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
239423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
23957cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2396e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2397e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2398e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
23995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
24005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
24015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
24025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
24035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
24041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
24051eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
24065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2407e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2408e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
24091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2411e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
241285606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
24135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
24145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
24155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
24165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
24175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2418a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2419a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2420a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2421a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2422b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2423b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2424b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2425b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2426a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2427b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2428b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2429b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2430b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2431b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2432e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2433e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2434e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2435e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2436f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2437b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2438e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2439e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
244010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
244154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
244254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2443af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
2444af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
2445af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2446af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
2447251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
24486cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
24491eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
24503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
2451f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
2452f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
2453f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2454f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
2455f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
2456f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
2457f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
2458dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
2459f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
24601eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
24618387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
2462f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
24638a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
24648387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
24658387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
24668387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
24678387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
24688387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
24698387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Call the LateTemplateParser callback if there a need to late parse
2470a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
2471f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
24728387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
24734a47e8d35dc1778ef7e428d9edd7676be67e725fFrancois Pichet    LateTemplateParser(OpaqueParser, PatternDecl);
24748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
24758387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
24768387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
2477f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
2478e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2479e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2480a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2481e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2482e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2483e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2484a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2485e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2486e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2487a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2488e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2489a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
2490e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2491cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
249258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
249358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
249462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
249558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2496e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
249758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
24981eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2499e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
25001eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2501d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2502d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
25031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2504d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
25051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2506d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
25077ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2508d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
25091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2510f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2511f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2512a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
2513a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2514e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
2515e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2516e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
2517b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2518b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2519b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
25205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
252162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
25222a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
25232a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
252462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
25252a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
25261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2527a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
2528f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2529d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2530e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
253154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
253260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
253360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
253460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
253560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
253660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
253760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
253860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
253960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
25401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
25428a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
25438a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
254412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor  unsigned FParamIdx = 0;
25458a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
25468a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
254712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    if (!PatternParam->isParameterPack()) {
254812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      // Simple case: not a parameter pack.
254912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      assert(FParamIdx < Function->getNumParams());
255012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(I);
255112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
255212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocal(PatternParam, FunctionParam);
255312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ++FParamIdx;
255412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      continue;
255512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
2556a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
255712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    // Expand the parameter pack.
255812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2559a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    for (unsigned NumFParams = Function->getNumParams();
2560a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi         FParamIdx < NumFParams;
256112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         ++FParamIdx) {
256212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
256312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
256412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
256512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
25668a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
256754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2568b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2569b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2570eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  Sema::ContextRAII savedContext(*this, Function);
2571b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
25721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2573e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2574090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2575cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (PatternDecl->isDefaulted()) {
2576cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, 0, /*IsInstantiation=*/true);
25771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2578cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SetDeclDefaulted(Function, PatternDecl->getLocation());
2579cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  } else {
2580cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
2581cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
2582cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2583cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2584cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
2585cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
2586cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2587cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
2588cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2589cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2590cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
2591cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
2592a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2593cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
2594cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
2595cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
2596b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
25970c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
25980c01d18094100db92d38daa923c95661512db203John McCall
2599eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  savedContext.pop();
2600aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2601aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2602aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
26031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
260460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
260560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
260662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
260760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
260860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2609b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
26102a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
26112a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
26122a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2613b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
26141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
261562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
26161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26172a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
26188155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
26198155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
26202a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
26212a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2622b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
26238155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
26248155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
262562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2626b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2627a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2628a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2629a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2630a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2631a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
26327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
26337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
26347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
26357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
26367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
26377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
26387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
26397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
26407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2641e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2642e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2643e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2644e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
26457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
26467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
26477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2648e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2649e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
26507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
26517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
26521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
26547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
26557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
2656a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(Def->isStaticDataMember() && "Not a static data member?");
26570d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
26581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26590d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
26607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
26617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
26621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
26631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2664e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
26650d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2666a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      Diag(PointOfInstantiation,
2667e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2668e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2669e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
267058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
267158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
267262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
267358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
267458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
267558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
26767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
26777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
26787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2679251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
26801028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2681251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2682a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2683251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2684251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2685251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2686251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
2687a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (Var->getTemplateSpecializationKind()
2688251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2689251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
26901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2691f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor  // If we already have a definition, we're done.
2692f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor  if (Var->getDefinition())
2693f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor    return;
2694f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
26957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
26967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
26977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
26981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
27007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
27017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
27025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
270362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
27048155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
27058155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
270662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
27078155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
27081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
27107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
2711f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  ContextRAII previousContext(*this, Var->getDeclContext());
27121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27131028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2714ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
27156bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
2716f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
2717f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  previousContext.pop();
27187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
27197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2720583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2721583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2722583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2723583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
27247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
27257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
27267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
27271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
27298155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
27308155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
27318155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
27327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
27331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
273462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
27351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27368155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
27378155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
27388155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded, "
27398155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
27408155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
27418155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
27427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
27438155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
27448155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded, "
27458155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
274662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
27471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2748a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2749815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
275054dec5f9ad13597bbebb6872ab938e2ad4b6aa1bBenjamin Kramerstatic MultiInitializer CreateMultiInitializer(SmallVectorImpl<Expr*> &Args,
275154dec5f9ad13597bbebb6872ab938e2ad4b6aa1bBenjamin Kramer                                               const CXXCtorInitializer *Init) {
27526df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // FIXME: This is a hack that will do slightly the wrong thing for an
27536df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // initializer of the form foo({...}).
27546df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // The right thing to do would be to modify InstantiateInitializer to create
27556df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // the MultiInitializer.
27566df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  if (Args.size() == 1 && isa<InitListExpr>(Args[0]))
27576df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    return MultiInitializer(Args[0]);
2758c046f30721fbb950f28faf9faa9a050f1fcc9becSebastian Redl  return MultiInitializer(Init->getLParenLoc(), Args.data(),
27596df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                          Args.size(), Init->getRParenLoc());
27606df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl}
27616df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
2762090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2763090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2764090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2765090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
27661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276790ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
27689db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
2769a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2770090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2771090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
277272f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
277372f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2774cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
2775090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2776030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2777030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2778030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2779030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2780030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
27816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2782ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> NewArgs(*this);
27831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27843fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
2785a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
27863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
27873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
278876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
27895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
27903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
27913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
2792d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
2793cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      llvm::Optional<unsigned> NumExpansions;
2794a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
27953fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
2796a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
2797a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
2798d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
27993fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
28003fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
28013fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
28023fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
28033fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
28043fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
2805a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2806a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
2807cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
28083fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
28093fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
28103fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
2811a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        if (InstantiateInitializer(Init->getInit(), TemplateArgs,
28123fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                   LParenLoc, NewArgs, RParenLoc)) {
28133fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
28143fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
28153fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
28163fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
28173fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
281876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
2819a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
2820a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
28213fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
28223fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
28233fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
28243fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
28253fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
28263fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
28273fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
28286df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
28296df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
28306df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                                     BaseTInfo, MultiInit,
28313fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
28323fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
28333fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
28343fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
28353fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
28363fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
2837a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
28383fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
28393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewArgs.clear();
28403fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
2841a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
28423fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
28433fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
28443fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
28456b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
2846a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (InstantiateInitializer(Init->getInit(), TemplateArgs,
2847a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                               LParenLoc, NewArgs, RParenLoc)) {
28486b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
28496b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2850090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2851a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2852090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
285376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
285476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
285576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
285676852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
285776852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
285876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
28599db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2860802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2861802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2862802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
28636df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
28646df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
286576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor
286676852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
286776852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, MultiInit,
286876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
286976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
287076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        NewInit = BuildDelegatingInitializer(TInfo, MultiInit,
287176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
2872090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
2873b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
287400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
287500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
287600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
2877b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
2878b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2879b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
2880b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
2881b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
28821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28836df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
28846df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      NewInit = BuildMemberInitializer(Member, MultiInit,
28856df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
288600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
288700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
2888b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
288900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
289000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
289100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
2892b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
2893b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2894b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
28956df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
2896b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
28976df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
28986df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
28996df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, MultiInit,
29006df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
2901090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2902090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
29039db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
29049db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2905090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
29069db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2907090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2908090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
29091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
291090ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
2911090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2912090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
29131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2914090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2915d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2916090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2917090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
29189db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
29199db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2920090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2921090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
292252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
292352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
292452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
292552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
292652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
292752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
292852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
292952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
293052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
293152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
293252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
293352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
293452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
293552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
293652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
29370d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
29380d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
29390d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
2940a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
29410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
29420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
29430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
29440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
29450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
2946a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
29470d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
29480d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
29490d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2950a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
2951ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2952ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2953a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
2954ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2955ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2956ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2957ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2958ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2959ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2960ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2961ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2962a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2963ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2964ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2965ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
296652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
296752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
296852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
296952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
297052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
297152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
297252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
297352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
297452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
297552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
297652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
297752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
297852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
297952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
298052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
298152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
298252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
298352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
298452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
298552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
298652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
298752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
298852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
298952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
299052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
299152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
299252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
299352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
299452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
299552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
299652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
299752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
299852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
299952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
300052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
300152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
300252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
300352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
300452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3005ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
3006ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
3007ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3008ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3009ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3010ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3011ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
3012ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
3013ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3014ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3015ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3016ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
30177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
30187ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
30197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
3020ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
30217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
30227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
30237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
30240d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
30250d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
3026ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
30270d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
30280d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
302952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
303052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
303152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
303252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
303352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
303452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
303552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
303652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
303752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
303852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
303952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
304052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
304152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
304252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
304352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3044ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
3045ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
3046815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
30470d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
30487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
30497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
30507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
30517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
30527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
30537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
30547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
30557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
30567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
30570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
30580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
30590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
30600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
3061815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
30620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
30630d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
30641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
306652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
30671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
306952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
3070815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
307152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
307252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
3073815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
30747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
307552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
307652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
307752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
307852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
307952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
3080a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
30810d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
30820d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
30830d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3084ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
3085ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3086ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3087ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
3088ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
3089d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3090d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
3091d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
30921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
3093d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
3094d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
3095d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
30961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3097ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3098ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3099ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3100ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3101ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3102ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3103815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
3104815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3105815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3106815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3107815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
31081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
3109815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
3110815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
3111815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
3112815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
3113815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
3114815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
3115815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3116815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
3117815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3118815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
311902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
312002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
312102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
312202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
31237c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
3124e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
312502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
31267c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
312702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
312802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
312902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
313002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
3131ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
3132ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
3133815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3134815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
3135815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
3136815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
3137815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
3138815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3139815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
3140815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
3141815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
3142815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
3143815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
3144815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
3145815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3146815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
3147815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
3148815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3149815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
3150815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
3151815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3152815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
3153815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
3154815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
3155ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
3156ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
31577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
3158e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
3159815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
3160550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
31616d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
3162766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
31632bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
31642bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
3165d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3166d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3167d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
3168a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
316957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
317057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
317157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
3172a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
317357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      unsigned PackIdx = ArgumentPackSubstitutionIndex;
317457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
317557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
317657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
317757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
317857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
317957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
3180a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
318157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
318257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
3183a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
318457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
318557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
31862bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
3187815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3188e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3189e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
3190e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
3191a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31922c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
31932c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
3194e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
31952c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
31962c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
31972c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
31982c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
31992c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
32002c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
32012c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
32022c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
32032c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
32042c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
32052c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
32062c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
32072c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
32082c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
32092c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
32102c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
32112c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
32122c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
32132c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
32142c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
32152c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
32162c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
32172c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
32182c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
32192c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
32202c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
32212c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
32222c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
3223e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
32242c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
32252c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
32262c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
32272c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
32282c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
32292c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
32302c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
32312c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
323252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
32332c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
32342c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
323552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
32368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
3237e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
3238e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
3239e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
324052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3241e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
3242e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
3243a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
32447c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
32451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
324644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
32471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3248815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
3249815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
3250815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
3251815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
32527c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
32533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
32543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
3255eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
32563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
32577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
32587c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
32593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
32603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
3261eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
3262eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
32633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
32643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
32653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
3266a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
3267a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
32687c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
32697c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
32707c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
3271815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
3272815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
327317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
3274815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
3275815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
3276815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
3277815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
3278815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
3279815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3280815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
3281815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
3282815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3283815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
32841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
328517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
328617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
3287815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
32881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3289eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
3290eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
3291eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
3292eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
3293eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
3294eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
3295eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
3296eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
3297a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
3298eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
3299eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
3300eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
3301eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
3302eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
3303eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
3304eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3305eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
3306eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
3307eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
3308eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
3309eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
3310eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
3311a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3312815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
3313815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
3314815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3315815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
3316815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3317d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
33181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
3319d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
33208155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
33216e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
33226e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
33236e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    SmallVector<std::pair<ValueDecl *, SourceLocation>, 4> Pending;
33246e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
33256e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
33266e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
33276e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
3328a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
332960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
333062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
333160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
333260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
333360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
333462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
333562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
333660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
333760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
333860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
333960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
33401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
33427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3343f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3344f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
334558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
334658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
334758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
334858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
33497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
33507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
33511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
33537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
33547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
3355c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
3356291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
3357291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
3358ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
3359291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
3360291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3361291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
3362291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
3363ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
3364291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
3365b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
3366291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
3367291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
336858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
336958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
337058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
337158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
3372ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
3373291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
3374291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
3375291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
3376291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3377f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3378f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
3379f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
33801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
338158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
338258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
338358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
338458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
3385d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
3386d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
33870c01d18094100db92d38daa923c95661512db203John McCall
33880c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
33890c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
33900c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
33910c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
33920c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
33930c01d18094100db92d38daa923c95661512db203John McCall
33940c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
33950c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
33960c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
33970c01d18094100db92d38daa923c95661512db203John McCall      break;
33980c01d18094100db92d38daa923c95661512db203John McCall    }
33990c01d18094100db92d38daa923c95661512db203John McCall  }
34000c01d18094100db92d38daa923c95661512db203John McCall}
3401