SemaTemplateInstantiateDecl.cpp revision d8b285fee4471f393da8ee30f552ceacdc362afa
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" 1883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h" 198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/Compiler.h" 208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang; 228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace { 248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor class VISIBILITY_HIDDEN TemplateDeclInstantiator 25b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner : public DeclVisitor<TemplateDeclInstantiator, Decl *> { 268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Sema &SemaRef; 278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor DeclContext *Owner; 28d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor const MultiLevelTemplateArgumentList &TemplateArgs; 298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor public: 318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor typedef Sema::OwningExprResult OwningExprResult; 328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, 34d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor const MultiLevelTemplateArgumentList &TemplateArgs) 357e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } 368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 37390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: Once we get closer to completion, replace these manually-written 38390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // declarations with automatically-generated ones from 39390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // clang/AST/DeclNodes.def. 404f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); 414f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor Decl *VisitNamespaceDecl(NamespaceDecl *D); 428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitTypedefDecl(TypedefDecl *D); 433d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor Decl *VisitVarDecl(VarDecl *D); 448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitFieldDecl(FieldDecl *D); 458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitStaticAssertDecl(StaticAssertDecl *D); 468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitEnumDecl(EnumDecl *D); 476477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 4802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Decl *VisitFriendDecl(FriendDecl *D); 49e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Decl *VisitFunctionDecl(FunctionDecl *D); 50d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor Decl *VisitCXXRecordDecl(CXXRecordDecl *D); 51d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Decl *VisitCXXMethodDecl(CXXMethodDecl *D, 52d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParameterList *TemplateParams = 0); 53615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); 5403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); 55bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor Decl *VisitCXXConversionDecl(CXXConversionDecl *D); 566477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); 572dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D); 58e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); 59d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 60e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D); 620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson 638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Base case. FIXME: Remove once we can instantiate everything. 648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitDecl(Decl *) { 653d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor assert(false && "Template instantiation of unknown declaration kind!"); 668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 685545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 69fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall const LangOptions &getLangOptions() { 70fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall return SemaRef.getLangOptions(); 71fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall } 72fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // Helper functions for instantiating methods. 74ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType SubstFunctionType(FunctionDecl *D, 755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor llvm::SmallVectorImpl<ParmVarDecl *> &Params); 76e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); 775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); 78e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 79e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateParameterList * 80ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall SubstTemplateParams(TemplateParameterList *List); 818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor }; 828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 844f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl * 854f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 864f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor assert(false && "Translation units cannot be instantiated"); 874f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor return D; 884f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor} 894f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor 904f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl * 914f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { 924f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor assert(false && "Namespaces cannot be instantiated"); 934f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor return D; 944f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor} 954f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor 968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { 978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor bool Invalid = false; 988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor QualType T = D->getUnderlyingType(); 998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (T->isDependentType()) { 100ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall T = SemaRef.SubstType(T, TemplateArgs, 101ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getLocation(), D->getDeclName()); 1028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (T.isNull()) { 1038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Invalid = true; 1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor T = SemaRef.Context.IntTy; 1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Create the new typedef 1098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor TypedefDecl *Typedef 1108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), 1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getIdentifier(), T); 1128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Invalid) 1138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Typedef->setInvalidDecl(); 1148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 11517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Owner->addDecl(Typedef); 116bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor 1178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Typedef; 1188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 1198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1203d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { 121ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall // Do substitution on the type of the declaration 122ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType T = SemaRef.SubstType(D->getType(), TemplateArgs, 123ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getTypeSpecStartLoc(), 124ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getDeclName()); 1253d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor if (T.isNull()) 1263d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor return 0; 1273d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 128b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor // Build the instantiated declaration 1293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, 1303d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor D->getLocation(), D->getIdentifier(), 131a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis T, D->getDeclaratorInfo(), 132a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis D->getStorageClass()); 1333d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor Var->setThreadSpecified(D->isThreadSpecified()); 1343d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); 1353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor Var->setDeclaredInCondition(D->isDeclaredInCondition()); 1363d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 1377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // If we are instantiating a static data member defined 1387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // out-of-line, the instantiation will have the same lexical 1397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // context (which will be a namespace scope) as the template. 1407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (D->isOutOfLine()) 1417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Var->setLexicalDeclContext(D->getLexicalDeclContext()); 1427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 143390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: In theory, we could have a previous declaration for variables that 144390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // are not static data members. 1453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor bool Redeclaration = false; 146eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration); 1477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 1487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (D->isOutOfLine()) { 1497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor D->getLexicalDeclContext()->addDecl(Var); 1507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Owner->makeDeclVisibleInContext(Var); 1517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } else { 1527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Owner->addDecl(Var); 1537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 1547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 1553d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor if (D->getInit()) { 1563d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor OwningExprResult Init 157ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstExpr(D->getInit(), TemplateArgs); 1583d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor if (Init.isInvalid()) 1593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor Var->setInvalidDecl(); 16083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) { 16183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor // FIXME: We're faking all of the comma locations, which is suboptimal. 16283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor // Do we even need these comma locations? 16383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor llvm::SmallVector<SourceLocation, 4> FakeCommaLocs; 16483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor if (PLE->getNumExprs() > 0) { 16583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor FakeCommaLocs.reserve(PLE->getNumExprs() - 1); 16683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) { 16783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor Expr *E = PLE->getExpr(I)->Retain(); 16883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor FakeCommaLocs.push_back( 16983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor SemaRef.PP.getLocForEndOfToken(E->getLocEnd())); 17083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor } 171e9f8eb64ed2d41c23db4a9774768613d4e07a865Douglas Gregor PLE->getExpr(PLE->getNumExprs() - 1)->Retain(); 17283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor } 17383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor 17483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor // Add the direct initializer to the declaration. 17583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), 17683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor PLE->getLParenLoc(), 17783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor Sema::MultiExprArg(SemaRef, 17883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor (void**)PLE->getExprs(), 17983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor PLE->getNumExprs()), 18083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor FakeCommaLocs.data(), 18183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor PLE->getRParenLoc()); 18283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor 18383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor // When Init is destroyed, it will destroy the instantiated ParenListExpr; 18483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor // we've explicitly retained all of its subexpressions already. 18583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor } else 186b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), 1873d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor D->hasCXXDirectInitializer()); 18865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) 18965b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); 1903d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 1917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Link instantiations of static data members back to the template from 1927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // which they were instantiated. 1937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Var->isStaticDataMember()) 1947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D); 1957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 1963d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor return Var; 1973d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor} 1983d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor 1998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { 2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor bool Invalid = false; 2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor QualType T = D->getType(); 2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (T->isDependentType()) { 203ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall T = SemaRef.SubstType(T, TemplateArgs, 204ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getLocation(), D->getDeclName()); 2058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (!T.isNull() && T->isFunctionType()) { 2068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // C++ [temp.arg.type]p3: 2078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // If a declaration acquires a function type through a type 2088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // dependent on a template-parameter and this causes a 2098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // declaration that does not use the syntactic form of a 2108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // function declarator to have function type, the program is 2118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // ill-formed. 2128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) 2138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor << T; 2148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor T = QualType(); 2158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Invalid = true; 2168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 2178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 2188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Expr *BitWidth = D->getBitWidth(); 2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Invalid) 2218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor BitWidth = 0; 2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor else if (BitWidth) { 223ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor // The bit-width expression is not potentially evaluated. 224ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); 225ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor 2268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult InstantiatedBitWidth 227ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstExpr(BitWidth, TemplateArgs); 2288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (InstantiatedBitWidth.isInvalid()) { 2298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Invalid = true; 2308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor BitWidth = 0; 2318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } else 232e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson BitWidth = InstantiatedBitWidth.takeAs<Expr>(); 2338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 2358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, 236a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getDeclaratorInfo(), 2378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor cast<RecordDecl>(Owner), 2388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getLocation(), 2398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->isMutable(), 2408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor BitWidth, 241ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff D->getTypeSpecStartLoc(), 2428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getAccess(), 2438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 0); 2448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Field) { 2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Invalid) 2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Field->setInvalidDecl(); 2478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 248d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson if (!Field->getDeclName()) { 249d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson // Keep track of where this decl came from. 250d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); 251d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson } 252d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson 25317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Owner->addDecl(Field); 2548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 2558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 2568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Field; 2578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 2588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 25902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { 26002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FriendDecl::FriendUnion FU; 26102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 26202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // Handle friend type expressions by simply substituting template 26302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // parameters into the pattern type. 26402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (Type *Ty = D->getFriendType()) { 26502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs, 26602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall D->getLocation(), DeclarationName()); 26702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (T.isNull()) return 0; 268fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 26902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall assert(getLangOptions().CPlusPlus0x || T->isRecordType()); 27002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FU = T.getTypePtr(); 271fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 27202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // Handle everything else by appropriate substitution. 27302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } else { 27402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall NamedDecl *ND = D->getFriendDecl(); 27502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall assert(ND && "friend decl must be a decl or a type!"); 27602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 27702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Decl *NewND = Visit(ND); 27802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (!NewND) return 0; 279c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman 28002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FU = cast<NamedDecl>(NewND); 28102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 28202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 28302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FriendDecl *FD = 28402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU, 28502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall D->getFriendLoc()); 2865fee110ac106370f75592df024001de73edced2aJohn McCall FD->setAccess(AS_public); 28702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Owner->addDecl(FD); 28802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall return FD; 289fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall} 290fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 2918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { 2928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Expr *AssertExpr = D->getAssertExpr(); 2938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 294ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor // The expression in a static assertion is not potentially evaluated. 295ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); 296ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor 2978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult InstantiatedAssertExpr 298ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstExpr(AssertExpr, TemplateArgs); 2998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (InstantiatedAssertExpr.isInvalid()) 3008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 3018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 30243d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor OwningExprResult Message(SemaRef, D->getMessage()); 30343d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor D->getMessage()->Retain(); 3048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *StaticAssert 305b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), 306b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner move(InstantiatedAssertExpr), 307b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner move(Message)).getAs<Decl>(); 3088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return StaticAssert; 3098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 3108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { 3128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, 3138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getLocation(), D->getIdentifier(), 314741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor D->getTagKeywordLoc(), 3158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor /*PrevDecl=*/0); 3168dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor Enum->setInstantiationOfMemberEnum(D); 31706c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor Enum->setAccess(D->getAccess()); 31817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Owner->addDecl(Enum); 3198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enum->startDefinition(); 3208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3210ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; 3228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConstantDecl *LastEnumConst = 0; 32417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), 32517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ECEnd = D->enumerator_end(); 3268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EC != ECEnd; ++EC) { 3278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // The specified value for the enumerator. 3288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult Value = SemaRef.Owned((Expr *)0); 329ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor if (Expr *UninstValue = EC->getInitExpr()) { 330ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor // The enumerator's value expression is not potentially evaluated. 331ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor EnterExpressionEvaluationContext Unevaluated(SemaRef, 332ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor Action::Unevaluated); 333ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor 334ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); 335ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor } 3368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Drop the initial value and continue. 3388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor bool isInvalid = false; 3398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Value.isInvalid()) { 3408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Value = SemaRef.Owned((Expr *)0); 3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor isInvalid = true; 3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConstantDecl *EnumConst 3458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = SemaRef.CheckEnumConstant(Enum, LastEnumConst, 3468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EC->getLocation(), EC->getIdentifier(), 3478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor move(Value)); 3488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (isInvalid) { 3508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (EnumConst) 3518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConst->setInvalidDecl(); 3528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enum->setInvalidDecl(); 3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (EnumConst) { 35617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Enum->addDecl(EnumConst); 357b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); 3588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor LastEnumConst = EnumConst; 3598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 362c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump // FIXME: Fixup LBraceLoc and RBraceLoc 363fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan // FIXME: Empty Scope and AttributeList (required to handle attribute packed). 364c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), 365c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump Sema::DeclPtrTy::make(Enum), 366fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan &Enumerators[0], Enumerators.size(), 367fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan 0, 0); 3688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Enum; 3708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 3718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3726477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { 3736477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor assert(false && "EnumConstantDecls can only occur within EnumDecls."); 3746477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor return 0; 3756477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor} 3766477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor 377e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { 378e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateParameterList *TempParams = D->getTemplateParameters(); 379ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall TemplateParameterList *InstParams = SubstTemplateParams(TempParams); 380d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!InstParams) 381d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return NULL; 382e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 383e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall CXXRecordDecl *Pattern = D->getTemplatedDecl(); 384e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall CXXRecordDecl *RecordInst 385e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner, 386e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Pattern->getLocation(), Pattern->getIdentifier(), 387e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL); 388e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 389e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall ClassTemplateDecl *Inst 390e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), 391e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->getIdentifier(), InstParams, RecordInst, 0); 392e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall RecordInst->setDescribedClassTemplate(Inst); 393e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Inst->setAccess(D->getAccess()); 394e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Inst->setInstantiatedFromMemberTemplate(D); 395e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 396e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Owner->addDecl(Inst); 397e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return Inst; 398e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall} 399e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 400d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl * 401d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 402d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParameterList *TempParams = D->getTemplateParameters(); 403d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParameterList *InstParams = SubstTemplateParams(TempParams); 404d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!InstParams) 405d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return NULL; 406d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor 407d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // FIXME: Handle instantiation of nested function templates that aren't 408d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // member function templates. This could happen inside a FriendDecl. 409d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor assert(isa<CXXMethodDecl>(D->getTemplatedDecl())); 410d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor CXXMethodDecl *InstMethod 411d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor = cast_or_null<CXXMethodDecl>( 412d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()), 413d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor InstParams)); 414d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!InstMethod) 415d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return 0; 416d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor 417d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // Link the instantiated function template declaration to the function 418d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template from which it was instantiated. 419d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate(); 420d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!"); 421d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor InstTemplate->setInstantiatedFromMemberTemplate(D); 422d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Owner->addDecl(InstTemplate); 423d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return InstTemplate; 424d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor} 425d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor 426d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { 427d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor CXXRecordDecl *PrevDecl = 0; 428d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor if (D->isInjectedClassName()) 429d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor PrevDecl = cast<CXXRecordDecl>(Owner); 430d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor 431d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor CXXRecordDecl *Record 432d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, 433741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor D->getLocation(), D->getIdentifier(), 434741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor D->getTagKeywordLoc(), PrevDecl); 435d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor Record->setImplicit(D->isImplicit()); 436eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman // FIXME: Check against AS_none is an ugly hack to work around the issue that 437eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman // the tag decls introduced by friend class declarations don't have an access 438eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman // specifier. Remove once this area of the code gets sorted out. 439eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman if (D->getAccess() != AS_none) 440eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman Record->setAccess(D->getAccess()); 441d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor if (!D->isInjectedClassName()) 442d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor Record->setInstantiationOfMemberClass(D); 443d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor 44402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // If the original function was part of a friend declaration, 44502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // inherit its namespace state. 44602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) 44702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); 44802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 449d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion()); 450d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson 45117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Owner->addDecl(Record); 452d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor return Record; 453d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor} 454d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor 45502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore 45602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here. This function serves two purposes: 45702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// 1) instantiating function templates 45802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// 2) substituting friend declarations 45902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2 460e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { 461127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // Check whether there is already a function template specialization for 462127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // this declaration. 463127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); 464127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor void *InsertPos = 0; 465127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (FunctionTemplate) { 466127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor llvm::FoldingSetNodeID ID; 467127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplateSpecializationInfo::Profile(ID, 468d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().getFlatArgumentList(), 469d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().flat_size(), 470828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor SemaRef.Context); 471127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 472127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplateSpecializationInfo *Info 473127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, 474127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor InsertPos); 475127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 476127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // If we already have a function template specialization, return it. 477127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (Info) 478127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor return Info->Function; 479127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 480e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 481e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Sema::LocalInstantiationScope Scope(SemaRef); 482e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 483e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor llvm::SmallVector<ParmVarDecl *, 4> Params; 484ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType T = SubstFunctionType(D, Params); 485e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (T.isNull()) 4862dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return 0; 487fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 488e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Build the instantiated method declaration. 48902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext()); 49002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FunctionDecl *Function = 49102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(), 492a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getDeclName(), T, D->getDeclaratorInfo(), 493a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getStorageClass(), 494a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis D->isInline(), D->hasWrittenPrototype()); 49502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Function->setLexicalDeclContext(Owner); 49602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 497e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Attach the parameters 498e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor for (unsigned P = 0; P < Params.size(); ++P) 499e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Params[P]->setOwningFunction(Function); 500e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Function->setParams(SemaRef.Context, Params.data(), Params.size()); 50102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 50202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // If the original function was part of a friend declaration, 50302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // inherit its namespace state and add it to the owner. 50402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) { 50502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall bool WasDeclared = (FOK == Decl::FOK_Declared); 50602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Function->setObjectOfFriendDecl(WasDeclared); 50702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (!Owner->isDependentContext()) 508ab88d97734f1260402a0c6a8f6b77bed7ed4e295John McCall DC->makeDeclVisibleInContext(Function, /* Recoverable = */ false); 509f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall 510f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall Function->setInstantiationOfMemberFunction(D); 51102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 512e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 513e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (InitFunctionInstantiation(Function, D)) 514e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Function->setInvalidDecl(); 515e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 516e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor bool Redeclaration = false; 517e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor bool OverloadableAttrRequired = false; 518e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor NamedDecl *PrevDecl = 0; 519e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration, 520e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor /*FIXME:*/OverloadableAttrRequired); 521e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 522127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (FunctionTemplate) { 523127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // Record this function template specialization. 524127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor Function->setFunctionTemplateSpecialization(SemaRef.Context, 525127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplate, 526d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor &TemplateArgs.getInnermost(), 527127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor InsertPos); 528fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall } 529fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 530e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return Function; 531e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 5322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 533d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl * 534d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, 535d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParameterList *TemplateParams) { 5366b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); 5376b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor void *InsertPos = 0; 538d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (FunctionTemplate && !TemplateParams) { 539d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // We are creating a function template specialization from a function 540d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template. Check whether there is already a function template 541d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // specialization for this particular set of template arguments. 5426b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor llvm::FoldingSetNodeID ID; 5436b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplateSpecializationInfo::Profile(ID, 544d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().getFlatArgumentList(), 545d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().flat_size(), 5466b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor SemaRef.Context); 5476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 5486b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplateSpecializationInfo *Info 5496b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, 5506b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor InsertPos); 5516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 5526b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor // If we already have a function template specialization, return it. 5536b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor if (Info) 5546b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor return Info->Function; 5556b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor } 5566b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 55748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor Sema::LocalInstantiationScope Scope(SemaRef); 55848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor 5590ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor llvm::SmallVector<ParmVarDecl *, 4> Params; 560ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType T = SubstFunctionType(D, Params); 5612dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor if (T.isNull()) 5622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return 0; 5632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 5642dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Build the instantiated method declaration. 5652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 566dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor CXXMethodDecl *Method = 0; 567dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 568dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor DeclarationName Name = D->getDeclName(); 56917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 570dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 571dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( 572dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor SemaRef.Context.getCanonicalType(ClassTy)); 573dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Method = CXXConstructorDecl::Create(SemaRef.Context, Record, 57417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->getLocation(), 575dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Name, T, 57617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->getDeclaratorInfo(), 57717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->isExplicit(), 57817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->isInline(), false); 57917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { 58017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 58117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( 58217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor SemaRef.Context.getCanonicalType(ClassTy)); 58317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Method = CXXDestructorDecl::Create(SemaRef.Context, Record, 58417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Destructor->getLocation(), Name, 58517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor T, Destructor->isInline(), false); 58665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { 58765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor CanQualType ConvTy 58865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = SemaRef.Context.getCanonicalType( 58965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor T->getAsFunctionType()->getResultType()); 59065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( 59165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor ConvTy); 59265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Method = CXXConversionDecl::Create(SemaRef.Context, Record, 59365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Conversion->getLocation(), Name, 59465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor T, Conversion->getDeclaratorInfo(), 59565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Conversion->isInline(), 59665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Conversion->isExplicit()); 597dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor } else { 598dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), 599dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor D->getDeclName(), T, D->getDeclaratorInfo(), 600dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor D->isStatic(), D->isInline()); 601dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor } 6026b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 603d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (TemplateParams) { 604d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // Our resulting instantiation is actually a function template, since we 605d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // are substituting only the outer template parameters. For example, given 606d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // 607d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template<typename T> 608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // struct X { 609d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template<typename U> void f(T, U); 610d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // }; 611d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // 612d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // X<int> x; 613d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // 614d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // We are instantiating the member template "f" within X<int>, which means 615d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // substituting int for T, but leaving "f" as a member function template. 616d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // Build the function template itself. 617d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, 618d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Method->getLocation(), 619d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Method->getDeclName(), 620d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParams, Method); 621d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (D->isOutOfLine()) 622d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); 623d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Method->setDescribedFunctionTemplate(FunctionTemplate); 624d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor } else if (!FunctionTemplate) 6256b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor Method->setInstantiationOfMemberFunction(D); 6262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 6277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // If we are instantiating a member function defined 6287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // out-of-line, the instantiation will have the same lexical 6297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // context (which will be a namespace scope) as the template. 6307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (D->isOutOfLine()) 6317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Method->setLexicalDeclContext(D->getLexicalDeclContext()); 6327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 6335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // Attach the parameters 6345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor for (unsigned P = 0; P < Params.size(); ++P) 6355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Params[P]->setOwningFunction(Method); 636beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad Method->setParams(SemaRef.Context, Params.data(), Params.size()); 6375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 6385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (InitMethodInstantiation(Method, D)) 6395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Method->setInvalidDecl(); 6402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 641dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor NamedDecl *PrevDecl = 0; 642dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 643d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!FunctionTemplate || TemplateParams) { 644dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor PrevDecl = SemaRef.LookupQualifiedName(Owner, Name, 645dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Sema::LookupOrdinaryName, true); 646dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 647dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // In C++, the previous declaration we find might be a tag type 648dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // (class or enum). In this case, the new declaration will hide the 649dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // tag type. Note that this does does not apply if we're declaring a 650dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // typedef (C++ [dcl.typedef]p4). 651dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) 652dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor PrevDecl = 0; 653dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor } 6542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 655d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (FunctionTemplate && !TemplateParams) 6566b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor // Record this function template specialization. 6576b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor Method->setFunctionTemplateSpecialization(SemaRef.Context, 6586b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplate, 659d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor &TemplateArgs.getInnermost(), 6606b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor InsertPos); 66165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 66265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor bool Redeclaration = false; 66365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor bool OverloadableAttrRequired = false; 66465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, 66565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor /*FIXME:*/OverloadableAttrRequired); 66665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 66765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl)) 668dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Owner->addDecl(Method); 669dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 6702dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return Method; 6712dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor} 6722dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 673615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 674dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor return VisitCXXMethodDecl(D); 675615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor} 676615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor 67703b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 67817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor return VisitCXXMethodDecl(D); 67903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor} 68003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor 681bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { 68265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return VisitCXXMethodDecl(D); 683bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor} 684bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor 6856477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { 686ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs, 6877e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor D->getLocation(), D->getDeclName()); 6882dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor if (OrigT.isNull()) 6892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return 0; 6902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 6912dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor QualType T = SemaRef.adjustParameterType(OrigT); 6922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 6932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Allocate the parameter 6942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor ParmVarDecl *Param = 0; 6952dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor if (T == OrigT) 6962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), 697a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getIdentifier(), T, D->getDeclaratorInfo(), 698a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getStorageClass(), 0); 6992dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor else 7002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, 7012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor D->getLocation(), D->getIdentifier(), 702a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis T, D->getDeclaratorInfo(), OrigT, 703a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getStorageClass(), 0); 7042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 7059351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson // Mark the default argument as being uninstantiated. 7069351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson if (Expr *Arg = D->getDefaultArg()) 7079351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson Param->setUninstantiatedDefaultArg(Arg); 7089351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson 7092dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Note: we don't try to instantiate function parameters until after 7102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // we've instantiated the function's type. Therefore, we don't have 7112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // to check for 'void' parameter types here. 71248dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); 7132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return Param; 7142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor} 7152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 7162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorDecl * 7172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorTemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { 7182dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Since parameter types can decay either before or after 7192dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // instantiation, we simply treat OriginalParmVarDecls as 7202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // ParmVarDecls the same way, and create one or the other depending 7212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // on what happens after template instantiation. 7222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return VisitParmVarDecl(D); 7232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor} 7242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 725e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( 726e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateTypeParmDecl *D) { 727e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall // TODO: don't always clone when decls are refcounted. 728e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall const Type* T = D->getTypeForDecl(); 729e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall assert(T->isTemplateTypeParmType()); 730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); 731e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateTypeParmDecl *Inst = 733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), 734e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TTPT->getDepth(), TTPT->getIndex(), 735e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TTPT->getName(), 736e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->wasDeclaredWithTypename(), 737e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->isParameterPack()); 738e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 739e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall if (D->hasDefaultArgument()) { 740e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall QualType DefaultPattern = D->getDefaultArgument(); 741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall QualType DefaultInst 742ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstType(DefaultPattern, TemplateArgs, 743ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getDefaultArgumentLoc(), 744ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getDeclName()); 745e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Inst->setDefaultArgument(DefaultInst, 747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->getDefaultArgumentLoc(), 748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->defaultArgumentWasInherited() /* preserve? */); 749e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 750e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 751e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return Inst; 752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall} 753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 7540dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl * 7550dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonTemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) { 7560dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson NestedNameSpecifier *NNS = 7570dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), 7580dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson D->getTargetNestedNameRange(), 7590dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson TemplateArgs); 7600dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson if (!NNS) 7610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson return 0; 7620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson 7630dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson CXXScopeSpec SS; 7640dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson SS.setRange(D->getTargetNestedNameRange()); 7650dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson SS.setScopeRep(NNS); 7660dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson 7670d8df780aef1acda5962347a32591efc629b6748Anders Carlsson NamedDecl *UD = 7680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson SemaRef.BuildUsingDeclaration(D->getLocation(), SS, 7690d8df780aef1acda5962347a32591efc629b6748Anders Carlsson D->getTargetNameLocation(), 7700d8df780aef1acda5962347a32591efc629b6748Anders Carlsson D->getTargetName(), 0, D->isTypeName()); 7710d8df780aef1acda5962347a32591efc629b6748Anders Carlsson if (UD) 7720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD), 7730d8df780aef1acda5962347a32591efc629b6748Anders Carlsson D); 7740d8df780aef1acda5962347a32591efc629b6748Anders Carlsson return UD; 7750dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson} 7760dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson 777ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner, 778d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor const MultiLevelTemplateArgumentList &TemplateArgs) { 7797e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); 7808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Instantiator.Visit(D); 7818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 7828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 783e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current 784e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context. 785e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// 786e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate 787e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// 788e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error 789e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList * 790ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { 791e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall // Get errors for all the parameters before bailing out. 792e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall bool Invalid = false; 793e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 794e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall unsigned N = L->size(); 795e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall typedef llvm::SmallVector<Decl*,8> ParamVector; 796e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall ParamVector Params; 797e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Params.reserve(N); 798e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); 799e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall PI != PE; ++PI) { 800e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Decl *D = Visit(*PI); 801e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Params.push_back(D); 802e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Invalid = Invalid || !D; 803e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 804e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 805e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall // Clean up if we had an error. 806e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall if (Invalid) { 807e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); 808e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall PI != PE; ++PI) 809e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall if (*PI) 810e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall (*PI)->Destroy(SemaRef.Context); 811e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return NULL; 812e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 813e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 814e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateParameterList *InstL 815e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), 816e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall L->getLAngleLoc(), &Params.front(), N, 817e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall L->getRAngleLoc()); 818e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return InstL; 819e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall} 820e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 821ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including 822ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters. 8235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// 824ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution 8255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// 8265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations 8275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 828ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL 8295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error. 8305545e166a956a20d7a6b58408e251a1119025485Douglas GregorQualType 831ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, 8325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor llvm::SmallVectorImpl<ParmVarDecl *> &Params) { 8335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor bool InvalidDecl = false; 8345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 835ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall // Substitute all of the function's formal parameter types. 8367e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); 8370ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor llvm::SmallVector<QualType, 4> ParamTys; 8385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor for (FunctionDecl::param_iterator P = D->param_begin(), 8395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor PEnd = D->param_end(); 8405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor P != PEnd; ++P) { 8416477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { 8425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (PInst->getType()->isVoidType()) { 8435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); 8445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor PInst->setInvalidDecl(); 845ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), 846ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump PInst->getType(), 847ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump diag::err_abstract_type_in_decl, 848ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump Sema::AbstractParamType)) 8495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor PInst->setInvalidDecl(); 8505545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Params.push_back(PInst); 8525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor ParamTys.push_back(PInst->getType()); 8535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8545545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (PInst->isInvalidDecl()) 8555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor InvalidDecl = true; 8565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } else 8575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor InvalidDecl = true; 8585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } 8595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8605545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // FIXME: Deallocate dead declarations. 8615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (InvalidDecl) 8625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor return QualType(); 8635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); 8655545e166a956a20d7a6b58408e251a1119025485Douglas Gregor assert(Proto && "Missing prototype?"); 8665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor QualType ResultType 867ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstType(Proto->getResultType(), TemplateArgs, 868ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getLocation(), D->getDeclName()); 8695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (ResultType.isNull()) 8705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor return QualType(); 8715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 872beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), 8735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Proto->isVariadic(), Proto->getTypeQuals(), 8745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor D->getLocation(), D->getDeclName()); 8755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor} 8765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 877e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Initializes the common fields of an instantiation function 878e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl). 879e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 880e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error 881e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool 882e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, 883e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *Tmpl) { 884e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (Tmpl->isDeleted()) 885e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor New->setDeleted(); 886cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor 887cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // If we are performing substituting explicitly-specified template arguments 888cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // or deduced template arguments into a function template and we reach this 889cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // point, we are now past the point where SFINAE applies and have committed 890cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // to keeping the new function template specialization. We therefore 891cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // convert the active template instantiation for the function template 892cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // into a template instantiation for this specific function template 893cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // specialization, which is not a SFINAE context, so that we diagnose any 894cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // further errors in the declaration itself. 895cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor typedef Sema::ActiveTemplateInstantiation ActiveInstType; 896cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); 897cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || 898cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { 899cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor if (FunctionTemplateDecl *FunTmpl 900cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { 901cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor assert(FunTmpl->getTemplatedDecl() == Tmpl && 902cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor "Deduction from the wrong function template?"); 903bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar (void) FunTmpl; 904cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInst.Kind = ActiveInstType::TemplateInstantiation; 905cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); 906cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor } 907cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor } 908cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor 909e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return false; 910e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 911e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 9125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method 9135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template 9145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl). 9155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// 9165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error 9175545e166a956a20d7a6b58408e251a1119025485Douglas Gregorbool 9185545e166a956a20d7a6b58408e251a1119025485Douglas GregorTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, 9195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor CXXMethodDecl *Tmpl) { 920e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (InitFunctionInstantiation(New, Tmpl)) 921e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return true; 922e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 9235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 9245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor New->setAccess(Tmpl->getAccess()); 92577b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson if (Tmpl->isVirtualAsWritten()) { 92677b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson New->setVirtualAsWritten(true); 9275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setAggregate(false); 9285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setPOD(false); 9291d954f6a0057cb55a3a5d483904a3c57d03c996fEli Friedman Record->setEmpty(false); 9305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setPolymorphic(true); 9315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } 9325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (Tmpl->isPure()) { 9335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor New->setPure(); 9345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setAbstract(true); 9355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } 9365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 9375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // FIXME: attributes 9385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // FIXME: New needs a pointer to Tmpl 9395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor return false; 9405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor} 941a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor 942a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its 943a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template. 944a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// 945b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was 946b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation" 947b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close. 948b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// 949a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a 950b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template 951b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization. 952b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// 953b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that 954b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation. 955f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, 956b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor FunctionDecl *Function, 957b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor bool Recursive) { 95854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor if (Function->isInvalidDecl()) 95954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor return; 96054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor 9616fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis assert(!Function->getBody() && "Already instantiated!"); 962d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 9631eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor // Find the function body that we'll be substituting. 9641637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor const FunctionDecl *PatternDecl = 0; 9655ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) { 9665ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor while (Primary->getInstantiatedFromMemberTemplate()) 9675ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor Primary = Primary->getInstantiatedFromMemberTemplate(); 9685ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor 9691637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor PatternDecl = Primary->getTemplatedDecl(); 9705ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor } else 9711637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor PatternDecl = Function->getInstantiatedFromMemberFunction(); 9721eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor Stmt *Pattern = 0; 9731eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor if (PatternDecl) 9746fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis Pattern = PatternDecl->getBody(PatternDecl); 9751eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor 9761eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor if (!Pattern) 9771eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor return; 9781eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor 979f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); 980f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor if (Inst) 981f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor return; 982b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor 983b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // If we're performing recursive template instantiation, create our own 984b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // queue of pending implicit instantiations that we will instantiate later, 985b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // while we're still within our own instantiation context. 986b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; 987b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor if (Recursive) 988b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 989b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor 990e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); 991e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor 99254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // Introduce a new scope where local variable instantiations will be 99354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // recorded. 99454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor LocalInstantiationScope Scope(*this); 99554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor 99654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // Introduce the instantiated function parameters into the local 99754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // instantiation scope. 99854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) 99954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), 100054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor Function->getParamDecl(I)); 100154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor 1002b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor // Enter the scope of this instantiation. We don't use 1003b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor // PushDeclContext because we don't have a scope. 1004b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor DeclContext *PreviousContext = CurContext; 1005b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor CurContext = Function; 1006b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor 1007090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson MultiLevelTemplateArgumentList TemplateArgs = 1008090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson getTemplateInstantiationArgs(Function); 1009090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1010090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // If this is a constructor, instantiate the member initializers. 1011090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (const CXXConstructorDecl *Ctor = 1012090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson dyn_cast<CXXConstructorDecl>(PatternDecl)) { 1013090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, 1014090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson TemplateArgs); 1015090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1016090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 101754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // Instantiate the function body. 1018090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs); 1019e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor 1020e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), 1021e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor /*IsInstantiation=*/true); 1022b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor 1023b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor CurContext = PreviousContext; 1024aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor 1025aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor DeclGroupRef DG(Function); 1026aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor Consumer.HandleTopLevelDecl(DG); 1027b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor 1028b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor if (Recursive) { 1029b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // Instantiate any pending implicit instantiations found during the 1030b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // instantiation of this template. 1031b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PerformPendingImplicitInstantiations(); 1032b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor 1033b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // Restore the set of pending implicit instantiations. 1034b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 1035b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor } 1036a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor} 1037a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor 1038a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its 1039a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template. 1040a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// 10417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was 10427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation" 10437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close. 10447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// 10457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member 10467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization. 10477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// 10487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that 10497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation. 10507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition( 10517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor SourceLocation PointOfInstantiation, 10527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor VarDecl *Var, 10537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor bool Recursive) { 10547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Var->isInvalidDecl()) 10557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return; 10567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Find the out-of-line definition of this static data member. 10587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // FIXME: Do we have to look for specializations separately? 10597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); 10607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor bool FoundOutOfLineDef = false; 10617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Def && "This data member was not instantiated from a template?"); 10627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Def->isStaticDataMember() && "Not a static data member?"); 10637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor for (VarDecl::redecl_iterator RD = Def->redecls_begin(), 10647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor RDEnd = Def->redecls_end(); 10657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor RD != RDEnd; ++RD) { 10667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (RD->getLexicalDeclContext()->isFileContext()) { 10677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Def = *RD; 10687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor FoundOutOfLineDef = true; 10697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 10707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 10717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (!FoundOutOfLineDef) { 10737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // We did not find an out-of-line definition of this static data member, 10747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // so we won't perform any instantiation. Rather, we rely on the user to 10757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // instantiate this definition (or provide a specialization for it) in 10767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // another translation unit. 10777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return; 10787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 10797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); 10817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Inst) 10827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return; 10837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // If we're performing recursive template instantiation, create our own 10857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // queue of pending implicit instantiations that we will instantiate later, 10867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // while we're still within our own instantiation context. 10877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; 10887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Recursive) 10897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 10907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Enter the scope of this instantiation. We don't use 10927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // PushDeclContext because we don't have a scope. 10937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor DeclContext *PreviousContext = CurContext; 10947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor CurContext = Var->getDeclContext(); 10957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 1096ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), 10977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor getTemplateInstantiationArgs(Var))); 10987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor CurContext = PreviousContext; 11007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 11017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Var) { 11027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor DeclGroupRef DG(Var); 11037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Consumer.HandleTopLevelDecl(DG); 11047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 11057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 11067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Recursive) { 11077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Instantiate any pending implicit instantiations found during the 11087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // instantiation of this template. 11097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor PerformPendingImplicitInstantiations(); 11107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 11117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Restore the set of pending implicit instantiations. 11127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 11137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 1114a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor} 1115815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1116090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid 1117090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New, 1118090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson const CXXConstructorDecl *Tmpl, 1119090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson const MultiLevelTemplateArgumentList &TemplateArgs) { 1120090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1121090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson llvm::SmallVector<MemInitTy*, 4> NewInits; 1122090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1123090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // Instantiate all the initializers. 1124090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), 1125090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson InitsEnd = Tmpl->init_end(); Inits != InitsEnd; ++Inits) { 1126090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson CXXBaseOrMemberInitializer *Init = *Inits; 1127090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1128090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this); 1129090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1130090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // Instantiate all the arguments. 1131090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end(); 1132090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Args != ArgsEnd; ++Args) { 1133090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs); 1134090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1135090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (NewArg.isInvalid()) 1136090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson New->setInvalidDecl(); 1137090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson else 1138090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.push_back(NewArg.takeAs<Expr>()); 1139090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1140090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1141090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson MemInitResult NewInit; 1142090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1143090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (Init->isBaseInitializer()) { 1144c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman QualType BaseType(Init->getBaseClass(), 0); 1145c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(), 1146c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman New->getDeclName()); 1147af496acbae62ad51bf42b40eea8ea8ebe48c5dbbEli Friedman BaseType = Context.getCanonicalType(BaseType); 1148090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1149090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInit = BuildBaseInitializer(BaseType, 1150090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson (Expr **)NewArgs.data(), 1151090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.size(), 1152090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getSourceLocation(), 1153090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getRParenLoc(), 1154090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson New->getParent()); 1155090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } else if (Init->isMemberInitializer()) { 1156090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson FieldDecl *Member = 1157090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson cast<FieldDecl>(FindInstantiatedDecl(Init->getMember())); 1158090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1159090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), 1160090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.size(), 1161090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getSourceLocation(), 1162090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getRParenLoc()); 1163090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1164090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1165090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (NewInit.isInvalid()) 1166090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson New->setInvalidDecl(); 1167090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson else { 1168090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // FIXME: It would be nice if ASTOwningVector had a release function. 1169090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.take(); 1170090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1171090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInits.push_back((MemInitTy *)NewInit.get()); 1172090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1173090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1174090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1175090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // Assign all the initializers to the new constructor. 1176090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson ActOnMemInitializers(DeclPtrTy::make(New), 1177090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson /*FIXME: ColonLoc */ 1178090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson SourceLocation(), 1179090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInits.data(), NewInits.size()); 1180090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson} 1181090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 118252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the 118352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name. 118452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern, 118552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall ClassTemplateDecl *Instance) { 118652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Pattern = Pattern->getCanonicalDecl(); 118752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 118852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall do { 118952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getCanonicalDecl(); 119052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Pattern == Instance) return true; 119152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getInstantiatedFromMemberTemplate(); 119252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 119352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 119452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 119552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 119652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 119752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern, 119852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall CXXRecordDecl *Instance) { 119952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Pattern = Pattern->getCanonicalDecl(); 120052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 120152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall do { 120252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getCanonicalDecl(); 120352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Pattern == Instance) return true; 120452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getInstantiatedFromMemberClass(); 120552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 120652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 120752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 120852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 120952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 121052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern, 121152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall FunctionDecl *Instance) { 121252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Pattern = Pattern->getCanonicalDecl(); 121352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 121452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall do { 121552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getCanonicalDecl(); 121652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Pattern == Instance) return true; 121752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getInstantiatedFromMemberFunction(); 121852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 121952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 122052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 122152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 122252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 122352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern, 122452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall EnumDecl *Instance) { 122552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Pattern = Pattern->getCanonicalDecl(); 122652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 122752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall do { 122852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getCanonicalDecl(); 122952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Pattern == Instance) return true; 123052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getInstantiatedFromMemberEnum(); 123152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 123252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 123352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 123452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 123552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 12360d8df780aef1acda5962347a32591efc629b6748Anders Carlssonstatic bool isInstantiationOf(UnresolvedUsingDecl *Pattern, 12370d8df780aef1acda5962347a32591efc629b6748Anders Carlsson UsingDecl *Instance, 12380d8df780aef1acda5962347a32591efc629b6748Anders Carlsson ASTContext &C) { 12390d8df780aef1acda5962347a32591efc629b6748Anders Carlsson return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern; 12400d8df780aef1acda5962347a32591efc629b6748Anders Carlsson} 12410d8df780aef1acda5962347a32591efc629b6748Anders Carlsson 124252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern, 124352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall VarDecl *Instance) { 124452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall assert(Instance->isStaticDataMember()); 124552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 124652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Pattern = Pattern->getCanonicalDecl(); 124752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 124852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall do { 124952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getCanonicalDecl(); 125052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Pattern == Instance) return true; 125152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getInstantiatedFromStaticDataMember(); 125252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 125352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 125452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 125552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 125652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 1257815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { 12580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson if (D->getKind() != Other->getKind()) { 12590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) { 12600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { 12610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson return isInstantiationOf(UUD, UD, Ctx); 12620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson } 12630d8df780aef1acda5962347a32591efc629b6748Anders Carlsson } 1264815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 12650d8df780aef1acda5962347a32591efc629b6748Anders Carlsson return false; 12660d8df780aef1acda5962347a32591efc629b6748Anders Carlsson } 12670d8df780aef1acda5962347a32591efc629b6748Anders Carlsson 126852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) 126952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<CXXRecordDecl>(D), Record); 1270a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor 127152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) 127252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<FunctionDecl>(D), Function); 1273815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 127452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) 127552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<EnumDecl>(D), Enum); 1276815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 12777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (VarDecl *Var = dyn_cast<VarDecl>(Other)) 127852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Var->isStaticDataMember()) 127952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); 128052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 128152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) 128252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); 1283a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor 1284d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { 1285d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson if (!Field->getDeclName()) { 1286d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson // This is an unnamed field. 1287d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == 1288d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson cast<FieldDecl>(D); 1289d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson } 1290d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson } 1291d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson 1292815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return D->getDeclName() && isa<NamedDecl>(Other) && 1293815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); 1294815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor} 1295815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1296815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator> 1297815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic NamedDecl *findInstantiationOf(ASTContext &Ctx, 1298815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor NamedDecl *D, 1299815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor ForwardIterator first, 1300815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor ForwardIterator last) { 1301815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor for (; first != last; ++first) 1302815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor if (isInstantiationOf(Ctx, D, *first)) 1303815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return cast<NamedDecl>(*first); 1304815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1305815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return 0; 1306815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor} 1307815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 130802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context 130902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation. 131002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// 131102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error 131202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDeclContext *Sema::FindInstantiatedContext(DeclContext* DC) { 131302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { 131402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Decl* ID = FindInstantiatedDecl(D); 131502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall return cast_or_null<DeclContext>(ID); 131602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } else return DC; 131702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall} 131802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 1319ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the 1320ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation. 1321815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1322815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration 1323815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the 1324815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example, 1325815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given: 1326815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1327815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code 1328815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T> 1329815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X { 1330815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// enum Kind { 1331815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// KnownValue = sizeof(T) 1332815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// }; 1333815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1334815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// bool getKind() const { return KnownValue; } 1335815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// }; 1336815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1337815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>; 1338815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode 1339815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1340815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the 1341815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to 1342815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation 1343ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs 1344ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>. 1345ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallNamedDecl * Sema::FindInstantiatedDecl(NamedDecl *D) { 1346815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor DeclContext *ParentDC = D->getDeclContext(); 13472bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) { 13482bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor // D is a local of some kind. Look into the map of local 13492bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor // declarations to their instantiations. 13502bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); 13512bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor } 1352815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 135352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) 135452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (ClassTemplateDecl *ClassTemplate 135552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall = Record->getDescribedClassTemplate()) { 135652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // When the declaration D was parsed, it referred to the current 135752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // instantiation. Therefore, look through the current context, 135852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // which contains actual instantiations, to find the 135952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // instantiation of the "current instantiation" that D refers 136052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // to. Alternatively, we could just instantiate the 136152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // injected-class-name with the current template arguments, but 136252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // such an instantiation is far more expensive. 136352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall for (DeclContext *DC = CurContext; !DC->isFileContext(); 136452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall DC = DC->getParent()) { 136552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (ClassTemplateSpecializationDecl *Spec 136652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall = dyn_cast<ClassTemplateSpecializationDecl>(DC)) 136752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (isInstantiationOf(ClassTemplate, Spec->getSpecializedTemplate())) 136852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return Spec; 136952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } 137052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 137152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall assert(false && 137252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall "Unable to find declaration for the current instantiation"); 137352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } 137452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 137502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall ParentDC = FindInstantiatedContext(ParentDC); 137602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (!ParentDC) return 0; 1377815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1378815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor if (ParentDC != D->getDeclContext()) { 1379815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // We performed some kind of instantiation in the parent context, 1380815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // so now we need to look into the instantiated parent context to 1381815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // find the instantiation of the declaration D. 1382815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor NamedDecl *Result = 0; 1383815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor if (D->getDeclName()) { 138417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); 1385815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor Result = findInstantiationOf(Context, D, Found.first, Found.second); 1386815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor } else { 1387815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // Since we don't have a name for the entity we're looking for, 1388815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // our only option is to walk through all of the declarations to 1389815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // find that name. This will occur in a few cases: 1390815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // 1391815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // - anonymous struct/union within a template 1392815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // - unnamed class/struct/union/enum within a template 1393815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // 1394815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // FIXME: Find a better way to find these instantiations! 1395815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor Result = findInstantiationOf(Context, D, 139617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ParentDC->decls_begin(), 139717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ParentDC->decls_end()); 1398815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor } 139952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 1400815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor assert(Result && "Unable to find instantiation of declaration!"); 1401815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor D = Result; 1402815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor } 1403815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1404815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return D; 1405815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor} 1406d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 1407d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// \brief Performs template instantiation for all implicit template 1408d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point. 1409d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() { 1410d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor while (!PendingImplicitInstantiations.empty()) { 1411d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front(); 1412b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PendingImplicitInstantiations.pop_front(); 1413d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 14147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Instantiate function definitions 14157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { 14166fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (!Function->getBody()) 1417b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); 14187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor continue; 14197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 1420d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 14217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Instantiate static data member definitions. 14227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor VarDecl *Var = cast<VarDecl>(Inst.first); 14237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Var->isStaticDataMember() && "Not a static data member?"); 14247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); 1425d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor } 1426d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor} 1427