SemaTemplateInstantiateDecl.cpp revision 65b90055dd30cfb83d8344ff62ed428257431be4
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//===----------------------------------------------------------------------===/
128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "Sema.h"
13aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/Compiler.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  class VISIBILITY_HIDDEN TemplateDeclInstantiator
24b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
277e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor    const TemplateArgumentList &TemplateArgs;
288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
337e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                             const TemplateArgumentList &TemplateArgs)
347e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
36390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
37390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
38390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
394f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
404f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
466477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
47e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
48d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
50615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
5103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
52bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
536477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitDecl(Decl *) {
583d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      assert(false && "Template instantiation of unknown declaration kind!");
598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    QualType InstantiateFunctionType(FunctionDecl *D,
645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
65e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
704f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
714f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
724f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
734f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
744f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
754f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
764f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
774f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
784f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
794f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
804f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
814f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  QualType T = D->getUnderlyingType();
858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (T->isDependentType()) {
861eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    T = SemaRef.InstantiateType(T, TemplateArgs,
871eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor                                D->getLocation(), D->getDeclName());
888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (T.isNull()) {
898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      T = SemaRef.Context.IntTy;
918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                          D->getIdentifier(), T);
988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
10117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
102bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
1038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1063d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
1073d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  // Instantiate the type of the declaration
1083d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
1093d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                       D->getTypeSpecStartLoc(),
1103d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                       D->getDeclName());
1113d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (T.isNull())
1123d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
1133d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
114b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
1153d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1163d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
1173d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 T, D->getStorageClass(),
1183d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getTypeSpecStartLoc());
1193d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
1203d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
1213d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
1223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we are instantiating a static data member defined
1247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
1257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
1277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
1287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
129390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
130390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
1313d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
132eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
1337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
1357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
1367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
1377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
1387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
1397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
1407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1413d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
1423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    OwningExprResult Init
1437e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
1443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    if (Init.isInvalid())
1453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      Var->setInvalidDecl();
1463d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    else
147b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
1483d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                   D->hasCXXDirectInitializer());
14965b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
15065b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
1513d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Link instantiations of static data members back to the template from
1537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // which they were instantiated.
1547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isStaticDataMember())
1557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D);
1567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1573d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
1583d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
1593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
1618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
1628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  QualType T = D->getType();
1638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (T->isDependentType())  {
1641eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    T = SemaRef.InstantiateType(T, TemplateArgs,
1651eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor                                D->getLocation(), D->getDeclName());
1668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (!T.isNull() && T->isFunctionType()) {
1678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
1688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
1698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
1708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
1718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
1728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
1738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
1748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        << T;
1758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      T = QualType();
1768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
1778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
1818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
1838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
184ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
185ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
186ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
1878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
1887e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
1898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
1908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
1918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
1928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
193e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
1948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
1978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            cast<RecordDecl>(Owner),
1988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
1998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
201ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
2038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
2048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Field) {
2058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Invalid)
2068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Field->setInvalidDecl();
2078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
20817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    Owner->addDecl(Field);
2098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
2128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
2158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
2168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
217ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
218ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
219ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
2217e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor    = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult Message = SemaRef.Clone(D->getMessage());
2268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Decl *StaticAssert
227b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
228b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
229b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
2308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
2318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
2358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
236741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
2378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
2388dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
23906c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
24017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
2418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
2428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2430ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
2448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
24617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
24717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
2488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
2498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
2508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
251ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
252ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
253ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      EnterExpressionEvaluationContext Unevaluated(SemaRef,
254ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
255ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
2567e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
257ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
2588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
2608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
2618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
2628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
2638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
2648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
2658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    EnumConstantDecl *EnumConst
2678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
2688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
2698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
2708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
2728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
2738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
2748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
2758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
2768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
27817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
279b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
2808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
2818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
2828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
284c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
285c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
286c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
2878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                        &Enumerators[0], Enumerators.size());
2888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
2908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2926477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
2936477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
2946477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
2956477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
2966477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
297d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
298d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
299d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
300d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
301d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
302d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
303d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
304741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
305741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
306d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
307d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setAccess(D->getAccess());
308d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
309d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Record->setInstantiationOfMemberClass(D);
310d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
31117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
312d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
313d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
314d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
315e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
316127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
317127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
318127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
319127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
320127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
321127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
322127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    FunctionTemplateSpecializationInfo::Profile(ID,
323127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                          TemplateArgs.getFlatArgumentList(),
324127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                TemplateArgs.flat_size());
325127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
326127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    FunctionTemplateSpecializationInfo *Info
327127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
328127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
329127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
330127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
331127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
332127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
333127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
334e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
335e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
336e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
337e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
338e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  QualType T = InstantiateFunctionType(D, Params);
339e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
3402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
341e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
342e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
343e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function
344e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionDecl::Create(SemaRef.Context, Owner, D->getLocation(),
345e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                           D->getDeclName(), T, D->getStorageClass(),
346e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                           D->isInline(), D->hasWrittenPrototype(),
347e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                           D->getTypeSpecStartLoc());
348e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
349e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // FIXME: friend functions
350e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
351e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
352e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
353e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
354e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
355e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
356e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
357e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
358e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
359e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
360e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
361e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  NamedDecl *PrevDecl = 0;
362e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
363e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
364e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
365127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
366127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // Record this function template specialization.
367127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
368127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                FunctionTemplate,
369127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                &TemplateArgs,
370127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                InsertPos);
371127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor   }
372127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
373e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
374e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
3752dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
376e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
377e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // FIXME: Look for existing, explicit specializations.
37848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
37948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
3800ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
3815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  QualType T = InstantiateFunctionType(D, Params);
3822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
3832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
3842dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
3852dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
3862dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
3872dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXMethodDecl *Method
3882dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
3892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                            D->getDeclName(), T, D->isStatic(),
3902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                            D->isInline());
3911eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Method->setInstantiationOfMemberFunction(D);
3922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
3937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we are instantiating a member function defined
3947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
3987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
3995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
4005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
4015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
402beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
4035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
4045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
4055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
4062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
4072dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  NamedDecl *PrevDecl
4082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
4092dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                  Sema::LookupOrdinaryName, true);
4102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // In C++, the previous declaration we find might be a tag type
4112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // (class or enum). In this case, the new declaration will hide the
4122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // tag type. Note that this does does not apply if we're declaring a
4132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // typedef (C++ [dcl.typedef]p4).
4142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
4152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    PrevDecl = 0;
4162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  bool Redeclaration = false;
4172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  bool OverloadableAttrRequired = false;
418eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
419eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                   /*FIXME:*/OverloadableAttrRequired);
4202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
4212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (!Method->isInvalidDecl() || !PrevDecl)
42217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    Owner->addDecl(Method);
4232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
4242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
4252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
426615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
427e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // FIXME: Look for existing, explicit specializations.
42848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
42948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
4300ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
431615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  QualType T = InstantiateFunctionType(D, Params);
432615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  if (T.isNull())
433615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    return 0;
434615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
435615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  // Build the instantiated method declaration.
436615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
437615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
438615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  DeclarationName Name
43949f25ecf7ff358039ce4c9254b867f32110e660eDouglas Gregor    = SemaRef.Context.DeclarationNames.getCXXConstructorName(
44049f25ecf7ff358039ce4c9254b867f32110e660eDouglas Gregor                                 SemaRef.Context.getCanonicalType(ClassTy));
441615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  CXXConstructorDecl *Constructor
442615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
443615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor                                 Name, T, D->isExplicit(), D->isInline(),
444615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor                                 false);
4451eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Constructor->setInstantiationOfMemberFunction(D);
446615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
447615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  // Attach the parameters
448615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
449615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Params[P]->setOwningFunction(Constructor);
450beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Constructor->setParams(SemaRef.Context, Params.data(), Params.size());
451615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
452615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  if (InitMethodInstantiation(Constructor, D))
453615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Constructor->setInvalidDecl();
454615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
455615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  NamedDecl *PrevDecl
456615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
457615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
458615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  // In C++, the previous declaration we find might be a tag type
459615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  // (class or enum). In this case, the new declaration will hide the
460615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  // tag type. Note that this does does not apply if we're declaring a
461615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  // typedef (C++ [dcl.typedef]p4).
462615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
463615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    PrevDecl = 0;
464615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  bool Redeclaration = false;
465615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  bool OverloadableAttrRequired = false;
466eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
467eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                   /*FIXME:*/OverloadableAttrRequired);
468615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
46949f25ecf7ff358039ce4c9254b867f32110e660eDouglas Gregor  Record->addedConstructor(SemaRef.Context, Constructor);
47017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Constructor);
471615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  return Constructor;
472615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
473615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
47403b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
475e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // FIXME: Look for existing, explicit specializations.
47648dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
47748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
4780ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
4795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  QualType T = InstantiateFunctionType(D, Params);
48003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  if (T.isNull())
48103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    return 0;
4825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Params.size() == 0 && "Destructor with parameters?");
4835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
48403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  // Build the instantiated destructor declaration.
48503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
48649f25ecf7ff358039ce4c9254b867f32110e660eDouglas Gregor  QualType ClassTy =
48749f25ecf7ff358039ce4c9254b867f32110e660eDouglas Gregor    SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
48803b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  CXXDestructorDecl *Destructor
48903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    = CXXDestructorDecl::Create(SemaRef.Context, Record,
49003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor                                D->getLocation(),
49103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor             SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
49203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor                                T, D->isInline(), false);
4931eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Destructor->setInstantiationOfMemberFunction(D);
4945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Destructor, D))
4955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Destructor->setInvalidDecl();
49603b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
49703b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  bool Redeclaration = false;
49803b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  bool OverloadableAttrRequired = false;
49903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  NamedDecl *PrevDecl = 0;
500eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
501eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                   /*FIXME:*/OverloadableAttrRequired);
50217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Destructor);
50303b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor  return Destructor;
50403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
50503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
506bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
507e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // FIXME: Look for existing, explicit specializations.
50848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
50948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
5100ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
511bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  QualType T = InstantiateFunctionType(D, Params);
512bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  if (T.isNull())
513bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    return 0;
514bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  assert(Params.size() == 0 && "Destructor with parameters?");
515bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
516bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  // Build the instantiated conversion declaration.
517bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
518bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
519bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  QualType ConvTy
520bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
521bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  CXXConversionDecl *Conversion
522bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    = CXXConversionDecl::Create(SemaRef.Context, Record,
523bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor                                D->getLocation(),
524bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor         SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
525bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor                                T, D->isInline(), D->isExplicit());
5261eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Conversion->setInstantiationOfMemberFunction(D);
527bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  if (InitMethodInstantiation(Conversion, D))
528bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Conversion->setInvalidDecl();
529bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
530bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  bool Redeclaration = false;
531bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  bool OverloadableAttrRequired = false;
532bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  NamedDecl *PrevDecl = 0;
533eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
534eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner                                   /*FIXME:*/OverloadableAttrRequired);
53517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Conversion);
536bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor  return Conversion;
537bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
538bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
5396477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
5402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
5417e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                                           D->getLocation(), D->getDeclName());
5422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (OrigT.isNull())
5432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
5442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  QualType T = SemaRef.adjustParameterType(OrigT);
5462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (D->getDefaultArg()) {
5482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // FIXME: Leave a marker for "uninstantiated" default
5492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // arguments. They only get instantiated on demand at the call
5502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    // site.
5512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
5522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor        "sorry, dropping default argument during template instantiation");
5532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
5542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor      << D->getDefaultArg()->getSourceRange();
5552dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  }
5562dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5572dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
5582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  ParmVarDecl *Param = 0;
5592dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T == OrigT)
5602dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
5612dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                D->getIdentifier(), T, D->getStorageClass(),
5622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                0);
5632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  else
5642dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
5652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                        D->getLocation(), D->getIdentifier(),
5662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                        T, OrigT, D->getStorageClass(), 0);
5672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
5692dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
5702dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
57148dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
5722dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
5732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
5742dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5752dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorDecl *
5762dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorTemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
5772dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Since parameter types can decay either before or after
5782dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // instantiation, we simply treat OriginalParmVarDecls as
5792dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // ParmVarDecls the same way, and create one or the other depending
5802dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // on what happens after template instantiation.
5812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return VisitParmVarDecl(D);
5822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
5832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
5857e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                            const TemplateArgumentList &TemplateArgs) {
5867e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
5888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Instantiates the type of the given function, including
5915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// instantiating all of the function parameters.
5925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
5935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param D The function that we will be instantiated
5945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
5955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
5965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
5975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns the instantiated function's type if successfull, a NULL
5985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
5995545e166a956a20d7a6b58408e251a1119025485Douglas GregorQualType
6005545e166a956a20d7a6b58408e251a1119025485Douglas GregorTemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
6015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
6025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
6035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Instantiate the function parameters
6057e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
6060ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
6075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(),
6085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
6095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
6106477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
6115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
6125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
6135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
6145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      }
6155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
6165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                              PInst->getType(),
6175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                              diag::err_abstract_type_in_decl,
6188211effbd3abc5948a5d6924c87e72323016a376Anders Carlsson                                              Sema::AbstractParamType))
6195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
6205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
6225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
6235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
6255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
6265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    } else
6275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
6285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
6295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
6315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
6325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
6335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
6355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
6365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  QualType ResultType
6371eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
6385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              D->getLocation(), D->getDeclName());
6395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
6405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
6415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
642beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
6435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
6445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
6455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
6465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
647e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Initializes the common fields of an instantiation function
648e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
649e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
650e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
651e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool
652e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
653e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
654e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
655e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
656cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
657cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
658cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
659cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
660cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // to keeping the new function template specialization. We therefore
661cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // convert the active template instantiation for the function template
662cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
663cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
664cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
665cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
666cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
667cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
668cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
669cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    if (FunctionTemplateDecl *FunTmpl
670cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
671cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
672cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
673bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
674cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
675cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
676cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
677cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
678cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
679e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
680e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
681e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
6825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
6835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
6845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
6855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
6865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
6875545e166a956a20d7a6b58408e251a1119025485Douglas Gregorbool
6885545e166a956a20d7a6b58408e251a1119025485Douglas GregorTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
6895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
690e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
691e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
692e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
6935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
6945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
69577b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson  if (Tmpl->isVirtualAsWritten()) {
69677b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson    New->setVirtualAsWritten(true);
6975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAggregate(false);
6985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPOD(false);
6995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPolymorphic(true);
7005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
7015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (Tmpl->isPure()) {
7025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    New->setPure();
7035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAbstract(true);
7045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
7055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
7065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
7075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
7085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
7095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
710a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
711a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
712a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
713a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
714b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
715b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
716b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
717b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
718a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
719b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
720b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
721b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
722b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
723b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
724f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
725b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
726b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         bool Recursive) {
72754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
72854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
72954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
7306fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
731d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
7321eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
7331637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  const FunctionDecl *PatternDecl = 0;
7341637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate())
7351637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Primary->getTemplatedDecl();
7361637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  else
7371637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Function->getInstantiatedFromMemberFunction();
7381eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
7391eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
7406fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
7411eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
7421eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (!Pattern)
7431eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
7441eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
745f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
746f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
747f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
748b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
749b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
750b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
751b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
752b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
753b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
754b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
755b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
756e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
757e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
75854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
75954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // recorded.
76054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  LocalInstantiationScope Scope(*this);
76154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
76254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
76354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
76454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
76554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
76654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
76754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
768b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
769b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
770b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
771b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
772b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
77354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
77454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  OwningStmtResult Body
77554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
776e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
777e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
778e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
779b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
780b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
781aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
782aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
783aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
784b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
785b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
786b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
787b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // instantiation of this template.
788b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
789b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
790b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
791b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
792b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
793a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
794a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
795a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
796a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
797a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
7987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
7997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
8007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
8017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
8027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
8037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
8047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
8057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
8067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
8077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
8087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
8097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
8107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 bool Recursive) {
8117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
8127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
8137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
8157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // FIXME: Do we have to look for specializations separately?
8167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
8177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  bool FoundOutOfLineDef = false;
8187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
8197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
8207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
8217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                             RDEnd = Def->redecls_end();
8227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor       RD != RDEnd; ++RD) {
8237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext()) {
8247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      Def = *RD;
8257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      FoundOutOfLineDef = true;
8267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
8277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
8287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (!FoundOutOfLineDef) {
8307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
8317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
8327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiate this definition (or provide a specialization for it) in
8337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // another translation unit.
8347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
8357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
8367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
8387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
8397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
8407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
8427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
8437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
8447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
8457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
8467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
8477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
8497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
8507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
8517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
8527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor#if 0
8547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Instantiate the initializer of this static data member.
8557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  OwningExprResult Init
8567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiateExpr(Def->getInit(), getTemplateInstantiationArgs(Var));
8577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Init.isInvalid()) {
8587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // If instantiation of the initializer failed, mark the declaration invalid
8597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // and don't instantiate anything else that was triggered by this
8607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiation.
8617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setInvalidDecl();
8627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
8647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
8657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
8677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
8687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Type-check the initializer.
8707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Init.get())
8717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    AddInitializerToDecl(DeclPtrTy::make(Var), move(Init),
8727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                         Def->hasCXXDirectInitializer());
8737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  else
8747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    ActOnUninitializedDecl(DeclPtrTy::make(Var), false);
8757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor#else
8767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  Var = cast_or_null<VarDecl>(InstantiateDecl(Def, Var->getDeclContext(),
8777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
8787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor#endif
8797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
8817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
8837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
8847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
8857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
8867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
8887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
8897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiation of this template.
8907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
8917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
8937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
8947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
895a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
896815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
897815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
898815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (D->getKind() != Other->getKind())
899815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    return false;
900815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
901815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
90297fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return Record->getInstantiatedFromMemberClass()->getCanonicalDecl()
90397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             == D->getCanonicalDecl();
904815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
905815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
90697fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return Function->getInstantiatedFromMemberFunction()->getCanonicalDecl()
90797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             == D->getCanonicalDecl();
908815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
9098dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
91097fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return Enum->getInstantiatedFromMemberEnum()->getCanonicalDecl()
91197fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             == D->getCanonicalDecl();
912815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
9137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
9147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (Var->isStaticDataMember())
9157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      return Var->getInstantiatedFromStaticDataMember()->getCanonicalDecl()
9167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor               == D->getCanonicalDecl();
9177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
918815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  // FIXME: How can we find instantiations of anonymous unions?
919815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
920815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
921815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
922815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
923815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
924815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
925815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
926815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
927815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
928815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
929815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
930815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
931815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
932815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
933815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
934815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
935815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
936ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
937ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
938815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
939815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
940815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
941815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
942815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
943815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
944815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
945815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
946815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
947815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
948815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
949815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
950815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
951815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
952815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
953815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
954815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
955815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
956815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
957815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
958815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
959815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
960ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
961ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
962ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas GregorNamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
963815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
9642bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
9652bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
9662bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
9672bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
9682bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
969815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
9702bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
971ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor    ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
972815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (!ParentDecl)
973815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return 0;
974815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
975815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    ParentDC = cast<DeclContext>(ParentDecl);
976815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
977815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
978815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
979815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
980815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
981815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
982815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
983815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
98417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
985815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
986815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
987815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
988815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
989815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
990815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
991815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
992815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
993815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
994815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
995815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D,
99617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
99717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
998815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
999815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    assert(Result && "Unable to find instantiation of declaration!");
1000815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
1001815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
1002815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1003815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
1004ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor    if (ClassTemplateDecl *ClassTemplate
1005ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor          = Record->getDescribedClassTemplate()) {
1006ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // When the declaration D was parsed, it referred to the current
1007ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // instantiation. Therefore, look through the current context,
1008ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // which contains actual instantiations, to find the
1009ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // instantiation of the "current instantiation" that D refers
1010ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // to. Alternatively, we could just instantiate the
1011ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // injected-class-name with the current template arguments, but
1012ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // such an instantiation is far more expensive.
1013ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      for (DeclContext *DC = CurContext; !DC->isFileContext();
1014ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor           DC = DC->getParent()) {
1015ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor        if (ClassTemplateSpecializationDecl *Spec
1016ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
101797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis          if (Spec->getSpecializedTemplate()->getCanonicalDecl()
101897fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis              == ClassTemplate->getCanonicalDecl())
1019ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor            return Spec;
1020ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      }
1021ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor
1022ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      assert(false &&
1023ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor             "Unable to find declaration for the current instantiation");
1024815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
1025815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1026815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
1027815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1028d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
1029d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// \brief Performs template instantiation for all implicit template
1030d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
1031d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() {
1032d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  while (!PendingImplicitInstantiations.empty()) {
1033d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor    PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
1034b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.pop_front();
1035d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
10367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
10377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
10386fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
1039b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
10407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
10417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
1042d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
10437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
10447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
10457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
10467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
1047d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
1048d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
1049