SemaTemplateInstantiateDecl.cpp revision 52a575a5ce7b27b6612357cdba5aa4ec1574ad5e
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 24817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Owner->addDecl(Field); 2498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 2508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 2518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Field; 2528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 2538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 25402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { 25502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FriendDecl::FriendUnion FU; 25602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 25702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // Handle friend type expressions by simply substituting template 25802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // parameters into the pattern type. 25902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (Type *Ty = D->getFriendType()) { 26002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs, 26102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall D->getLocation(), DeclarationName()); 26202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (T.isNull()) return 0; 263fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 26402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall assert(getLangOptions().CPlusPlus0x || T->isRecordType()); 26502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FU = T.getTypePtr(); 266fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 26702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // Handle everything else by appropriate substitution. 26802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } else { 26902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall NamedDecl *ND = D->getFriendDecl(); 27002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall assert(ND && "friend decl must be a decl or a type!"); 27102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 27202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Decl *NewND = Visit(ND); 27302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (!NewND) return 0; 274c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman 27502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FU = cast<NamedDecl>(NewND); 27602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 27702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 27802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FriendDecl *FD = 27902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU, 28002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall D->getFriendLoc()); 2815fee110ac106370f75592df024001de73edced2aJohn McCall FD->setAccess(AS_public); 28202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Owner->addDecl(FD); 28302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall return FD; 284fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall} 285fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 2868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { 2878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Expr *AssertExpr = D->getAssertExpr(); 2888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 289ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor // The expression in a static assertion is not potentially evaluated. 290ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); 291ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor 2928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult InstantiatedAssertExpr 293ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstExpr(AssertExpr, TemplateArgs); 2948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (InstantiatedAssertExpr.isInvalid()) 2958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 2968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 29743d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor OwningExprResult Message(SemaRef, D->getMessage()); 29843d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor D->getMessage()->Retain(); 2998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *StaticAssert 300b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), 301b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner move(InstantiatedAssertExpr), 302b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner move(Message)).getAs<Decl>(); 3038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return StaticAssert; 3048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 3058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { 3078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, 3088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getLocation(), D->getIdentifier(), 309741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor D->getTagKeywordLoc(), 3108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor /*PrevDecl=*/0); 3118dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor Enum->setInstantiationOfMemberEnum(D); 31206c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor Enum->setAccess(D->getAccess()); 31317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Owner->addDecl(Enum); 3148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enum->startDefinition(); 3158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3160ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; 3178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConstantDecl *LastEnumConst = 0; 31917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), 32017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ECEnd = D->enumerator_end(); 3218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EC != ECEnd; ++EC) { 3228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // The specified value for the enumerator. 3238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult Value = SemaRef.Owned((Expr *)0); 324ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor if (Expr *UninstValue = EC->getInitExpr()) { 325ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor // The enumerator's value expression is not potentially evaluated. 326ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor EnterExpressionEvaluationContext Unevaluated(SemaRef, 327ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor Action::Unevaluated); 328ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor 329ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); 330ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor } 3318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Drop the initial value and continue. 3338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor bool isInvalid = false; 3348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Value.isInvalid()) { 3358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Value = SemaRef.Owned((Expr *)0); 3368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor isInvalid = true; 3378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConstantDecl *EnumConst 3408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = SemaRef.CheckEnumConstant(Enum, LastEnumConst, 3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EC->getLocation(), EC->getIdentifier(), 3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor move(Value)); 3438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (isInvalid) { 3458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (EnumConst) 3468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConst->setInvalidDecl(); 3478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enum->setInvalidDecl(); 3488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (EnumConst) { 35117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Enum->addDecl(EnumConst); 352b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); 3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor LastEnumConst = EnumConst; 3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 357c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump // FIXME: Fixup LBraceLoc and RBraceLoc 358fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan // FIXME: Empty Scope and AttributeList (required to handle attribute packed). 359c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), 360c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump Sema::DeclPtrTy::make(Enum), 361fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan &Enumerators[0], Enumerators.size(), 362fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan 0, 0); 3638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Enum; 3658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 3668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 3676477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { 3686477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor assert(false && "EnumConstantDecls can only occur within EnumDecls."); 3696477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor return 0; 3706477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor} 3716477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor 372e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { 373e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateParameterList *TempParams = D->getTemplateParameters(); 374ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall TemplateParameterList *InstParams = SubstTemplateParams(TempParams); 375d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!InstParams) 376d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return NULL; 377e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 378e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall CXXRecordDecl *Pattern = D->getTemplatedDecl(); 379e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall CXXRecordDecl *RecordInst 380e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner, 381e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Pattern->getLocation(), Pattern->getIdentifier(), 382e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL); 383e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 384e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall ClassTemplateDecl *Inst 385e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), 386e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->getIdentifier(), InstParams, RecordInst, 0); 387e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall RecordInst->setDescribedClassTemplate(Inst); 388e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Inst->setAccess(D->getAccess()); 389e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Inst->setInstantiatedFromMemberTemplate(D); 390e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 391e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Owner->addDecl(Inst); 392e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return Inst; 393e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall} 394e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 395d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl * 396d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 397d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParameterList *TempParams = D->getTemplateParameters(); 398d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParameterList *InstParams = SubstTemplateParams(TempParams); 399d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!InstParams) 400d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return NULL; 401d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor 402d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // FIXME: Handle instantiation of nested function templates that aren't 403d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // member function templates. This could happen inside a FriendDecl. 404d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor assert(isa<CXXMethodDecl>(D->getTemplatedDecl())); 405d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor CXXMethodDecl *InstMethod 406d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor = cast_or_null<CXXMethodDecl>( 407d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()), 408d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor InstParams)); 409d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!InstMethod) 410d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return 0; 411d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor 412d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // Link the instantiated function template declaration to the function 413d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template from which it was instantiated. 414d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate(); 415d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!"); 416d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor InstTemplate->setInstantiatedFromMemberTemplate(D); 417d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Owner->addDecl(InstTemplate); 418d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor return InstTemplate; 419d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor} 420d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor 421d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { 422d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor CXXRecordDecl *PrevDecl = 0; 423d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor if (D->isInjectedClassName()) 424d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor PrevDecl = cast<CXXRecordDecl>(Owner); 425d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor 426d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor CXXRecordDecl *Record 427d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, 428741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor D->getLocation(), D->getIdentifier(), 429741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor D->getTagKeywordLoc(), PrevDecl); 430d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor Record->setImplicit(D->isImplicit()); 431eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman // FIXME: Check against AS_none is an ugly hack to work around the issue that 432eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman // the tag decls introduced by friend class declarations don't have an access 433eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman // specifier. Remove once this area of the code gets sorted out. 434eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman if (D->getAccess() != AS_none) 435eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman Record->setAccess(D->getAccess()); 436d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor if (!D->isInjectedClassName()) 437d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor Record->setInstantiationOfMemberClass(D); 438d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor 43902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // If the original function was part of a friend declaration, 44002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // inherit its namespace state. 44102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) 44202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); 44302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 44417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Owner->addDecl(Record); 445d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor return Record; 446d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor} 447d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor 44802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore 44902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here. This function serves two purposes: 45002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// 1) instantiating function templates 45102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// 2) substituting friend declarations 45202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2 453e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { 454127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // Check whether there is already a function template specialization for 455127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // this declaration. 456127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); 457127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor void *InsertPos = 0; 458127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (FunctionTemplate) { 459127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor llvm::FoldingSetNodeID ID; 460127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplateSpecializationInfo::Profile(ID, 461d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().getFlatArgumentList(), 462d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().flat_size(), 463828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor SemaRef.Context); 464127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 465127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplateSpecializationInfo *Info 466127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, 467127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor InsertPos); 468127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 469127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // If we already have a function template specialization, return it. 470127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (Info) 471127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor return Info->Function; 472127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 473e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 474e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Sema::LocalInstantiationScope Scope(SemaRef); 475e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 476e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor llvm::SmallVector<ParmVarDecl *, 4> Params; 477ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType T = SubstFunctionType(D, Params); 478e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (T.isNull()) 4792dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return 0; 480fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 481e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Build the instantiated method declaration. 48202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext()); 48302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FunctionDecl *Function = 48402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(), 485a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getDeclName(), T, D->getDeclaratorInfo(), 486a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getStorageClass(), 487a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis D->isInline(), D->hasWrittenPrototype()); 48802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Function->setLexicalDeclContext(Owner); 48902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 490e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Attach the parameters 491e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor for (unsigned P = 0; P < Params.size(); ++P) 492e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Params[P]->setOwningFunction(Function); 493e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Function->setParams(SemaRef.Context, Params.data(), Params.size()); 49402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 49502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // If the original function was part of a friend declaration, 49602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall // inherit its namespace state and add it to the owner. 49702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) { 49802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall bool WasDeclared = (FOK == Decl::FOK_Declared); 49902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Function->setObjectOfFriendDecl(WasDeclared); 50002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (!Owner->isDependentContext()) 50102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall DC->makeDeclVisibleInContext(Function); 502f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall 503f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall Function->setInstantiationOfMemberFunction(D); 50402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } 505e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 506e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (InitFunctionInstantiation(Function, D)) 507e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Function->setInvalidDecl(); 508e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 509e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor bool Redeclaration = false; 510e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor bool OverloadableAttrRequired = false; 511e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor NamedDecl *PrevDecl = 0; 512e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration, 513e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor /*FIXME:*/OverloadableAttrRequired); 514e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 515127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (FunctionTemplate) { 516127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // Record this function template specialization. 517127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor Function->setFunctionTemplateSpecialization(SemaRef.Context, 518127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor FunctionTemplate, 519d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor &TemplateArgs.getInnermost(), 520127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor InsertPos); 521fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall } 522fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall 523e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return Function; 524e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 5252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 526d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl * 527d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, 528d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParameterList *TemplateParams) { 5296b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); 5306b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor void *InsertPos = 0; 531d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (FunctionTemplate && !TemplateParams) { 532d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // We are creating a function template specialization from a function 533d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template. Check whether there is already a function template 534d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // specialization for this particular set of template arguments. 5356b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor llvm::FoldingSetNodeID ID; 5366b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplateSpecializationInfo::Profile(ID, 537d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().getFlatArgumentList(), 538d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor TemplateArgs.getInnermost().flat_size(), 5396b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor SemaRef.Context); 5406b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 5416b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplateSpecializationInfo *Info 5426b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, 5436b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor InsertPos); 5446b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 5456b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor // If we already have a function template specialization, return it. 5466b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor if (Info) 5476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor return Info->Function; 5486b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor } 5496b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 55048dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor Sema::LocalInstantiationScope Scope(SemaRef); 55148dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor 5520ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor llvm::SmallVector<ParmVarDecl *, 4> Params; 553ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType T = SubstFunctionType(D, Params); 5542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor if (T.isNull()) 5552dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return 0; 5562dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 5572dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Build the instantiated method declaration. 5582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 559dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor CXXMethodDecl *Method = 0; 560dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 561dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor DeclarationName Name = D->getDeclName(); 56217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 563dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 564dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( 565dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor SemaRef.Context.getCanonicalType(ClassTy)); 566dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Method = CXXConstructorDecl::Create(SemaRef.Context, Record, 56717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->getLocation(), 568dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Name, T, 56917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->getDeclaratorInfo(), 57017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->isExplicit(), 57117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Constructor->isInline(), false); 57217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { 57317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); 57417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( 57517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor SemaRef.Context.getCanonicalType(ClassTy)); 57617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Method = CXXDestructorDecl::Create(SemaRef.Context, Record, 57717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor Destructor->getLocation(), Name, 57817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor T, Destructor->isInline(), false); 57965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { 58065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor CanQualType ConvTy 58165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = SemaRef.Context.getCanonicalType( 58265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor T->getAsFunctionType()->getResultType()); 58365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( 58465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor ConvTy); 58565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Method = CXXConversionDecl::Create(SemaRef.Context, Record, 58665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Conversion->getLocation(), Name, 58765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor T, Conversion->getDeclaratorInfo(), 58865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Conversion->isInline(), 58965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Conversion->isExplicit()); 590dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor } else { 591dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), 592dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor D->getDeclName(), T, D->getDeclaratorInfo(), 593dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor D->isStatic(), D->isInline()); 594dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor } 5956b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor 596d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (TemplateParams) { 597d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // Our resulting instantiation is actually a function template, since we 598d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // are substituting only the outer template parameters. For example, given 599d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // 600d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template<typename T> 601d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // struct X { 602d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // template<typename U> void f(T, U); 603d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // }; 604d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // 605d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // X<int> x; 606d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // 607d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // We are instantiating the member template "f" within X<int>, which means 608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // substituting int for T, but leaving "f" as a member function template. 609d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor // Build the function template itself. 610d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, 611d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Method->getLocation(), 612d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Method->getDeclName(), 613d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor TemplateParams, Method); 614d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (D->isOutOfLine()) 615d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); 616d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor Method->setDescribedFunctionTemplate(FunctionTemplate); 617d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor } else if (!FunctionTemplate) 6186b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor Method->setInstantiationOfMemberFunction(D); 6192dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 6207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // If we are instantiating a member function defined 6217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // out-of-line, the instantiation will have the same lexical 6227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // context (which will be a namespace scope) as the template. 6237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (D->isOutOfLine()) 6247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Method->setLexicalDeclContext(D->getLexicalDeclContext()); 6257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 6265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // Attach the parameters 6275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor for (unsigned P = 0; P < Params.size(); ++P) 6285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Params[P]->setOwningFunction(Method); 629beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad Method->setParams(SemaRef.Context, Params.data(), Params.size()); 6305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 6315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (InitMethodInstantiation(Method, D)) 6325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Method->setInvalidDecl(); 6332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 634dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor NamedDecl *PrevDecl = 0; 635dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 636d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (!FunctionTemplate || TemplateParams) { 637dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor PrevDecl = SemaRef.LookupQualifiedName(Owner, Name, 638dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Sema::LookupOrdinaryName, true); 639dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 640dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // In C++, the previous declaration we find might be a tag type 641dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // (class or enum). In this case, the new declaration will hide the 642dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // tag type. Note that this does does not apply if we're declaring a 643dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor // typedef (C++ [dcl.typedef]p4). 644dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) 645dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor PrevDecl = 0; 646dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor } 6472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 648d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor if (FunctionTemplate && !TemplateParams) 6496b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor // Record this function template specialization. 6506b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor Method->setFunctionTemplateSpecialization(SemaRef.Context, 6516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor FunctionTemplate, 652d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor &TemplateArgs.getInnermost(), 6536b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor InsertPos); 65465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 65565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor bool Redeclaration = false; 65665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor bool OverloadableAttrRequired = false; 65765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, 65865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor /*FIXME:*/OverloadableAttrRequired); 65965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 66065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl)) 661dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor Owner->addDecl(Method); 662dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor 6632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return Method; 6642dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor} 6652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 666615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { 667dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor return VisitCXXMethodDecl(D); 668615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor} 669615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor 67003b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { 67117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor return VisitCXXMethodDecl(D); 67203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor} 67303b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor 674bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { 67565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return VisitCXXMethodDecl(D); 676bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor} 677bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor 6786477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { 679ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs, 6807e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor D->getLocation(), D->getDeclName()); 6812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor if (OrigT.isNull()) 6822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return 0; 6832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 6842dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor QualType T = SemaRef.adjustParameterType(OrigT); 6852dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 6862dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Allocate the parameter 6872dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor ParmVarDecl *Param = 0; 6882dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor if (T == OrigT) 6892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), 690a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getIdentifier(), T, D->getDeclaratorInfo(), 691a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getStorageClass(), 0); 6922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor else 6932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, 6942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor D->getLocation(), D->getIdentifier(), 695a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis T, D->getDeclaratorInfo(), OrigT, 696a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis D->getStorageClass(), 0); 6972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 6989351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson // Mark the default argument as being uninstantiated. 6999351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson if (Expr *Arg = D->getDefaultArg()) 7009351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson Param->setUninstantiatedDefaultArg(Arg); 7019351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson 7022dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Note: we don't try to instantiate function parameters until after 7032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // we've instantiated the function's type. Therefore, we don't have 7042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // to check for 'void' parameter types here. 70548dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); 7062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return Param; 7072dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor} 7082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 7092dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorDecl * 7102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorTemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { 7112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // Since parameter types can decay either before or after 7122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // instantiation, we simply treat OriginalParmVarDecls as 7132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // ParmVarDecls the same way, and create one or the other depending 7142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor // on what happens after template instantiation. 7152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor return VisitParmVarDecl(D); 7162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor} 7172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor 718e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( 719e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateTypeParmDecl *D) { 720e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall // TODO: don't always clone when decls are refcounted. 721e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall const Type* T = D->getTypeForDecl(); 722e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall assert(T->isTemplateTypeParmType()); 723e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); 724e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 725e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateTypeParmDecl *Inst = 726e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), 727e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TTPT->getDepth(), TTPT->getIndex(), 728e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TTPT->getName(), 729e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->wasDeclaredWithTypename(), 730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->isParameterPack()); 731e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall if (D->hasDefaultArgument()) { 733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall QualType DefaultPattern = D->getDefaultArgument(); 734e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall QualType DefaultInst 735ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstType(DefaultPattern, TemplateArgs, 736ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getDefaultArgumentLoc(), 737ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getDeclName()); 738e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 739e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Inst->setDefaultArgument(DefaultInst, 740e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->getDefaultArgumentLoc(), 741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall D->defaultArgumentWasInherited() /* preserve? */); 742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 744e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return Inst; 745e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall} 746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 7470dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl * 7480dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonTemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) { 7490dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson NestedNameSpecifier *NNS = 7500dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), 7510dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson D->getTargetNestedNameRange(), 7520dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson TemplateArgs); 7530dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson if (!NNS) 7540dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson return 0; 7550dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson 7560dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson CXXScopeSpec SS; 7570dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson SS.setRange(D->getTargetNestedNameRange()); 7580dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson SS.setScopeRep(NNS); 7590dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson 7600dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson return SemaRef.BuildUsingDeclaration(D->getLocation(), SS, 7610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson D->getTargetNameLocation(), 7620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson D->getTargetName(), 0, D->isTypeName()); 7630dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson} 7640dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson 765ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner, 766d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor const MultiLevelTemplateArgumentList &TemplateArgs) { 7677e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); 7688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Instantiator.Visit(D); 7698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 7708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 771e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current 772e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context. 773e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// 774e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate 775e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// 776e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error 777e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList * 778ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { 779e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall // Get errors for all the parameters before bailing out. 780e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall bool Invalid = false; 781e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 782e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall unsigned N = L->size(); 783e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall typedef llvm::SmallVector<Decl*,8> ParamVector; 784e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall ParamVector Params; 785e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Params.reserve(N); 786e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); 787e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall PI != PE; ++PI) { 788e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Decl *D = Visit(*PI); 789e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Params.push_back(D); 790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall Invalid = Invalid || !D; 791e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 792e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 793e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall // Clean up if we had an error. 794e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall if (Invalid) { 795e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); 796e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall PI != PE; ++PI) 797e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall if (*PI) 798e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall (*PI)->Destroy(SemaRef.Context); 799e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return NULL; 800e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall } 801e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 802e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall TemplateParameterList *InstL 803e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), 804e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall L->getLAngleLoc(), &Params.front(), N, 805e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall L->getRAngleLoc()); 806e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall return InstL; 807e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall} 808e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall 809ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including 810ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters. 8115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// 812ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution 8135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// 8145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations 8155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 816ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL 8175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error. 8185545e166a956a20d7a6b58408e251a1119025485Douglas GregorQualType 819ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, 8205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor llvm::SmallVectorImpl<ParmVarDecl *> &Params) { 8215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor bool InvalidDecl = false; 8225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 823ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall // Substitute all of the function's formal parameter types. 8247e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); 8250ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor llvm::SmallVector<QualType, 4> ParamTys; 8265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor for (FunctionDecl::param_iterator P = D->param_begin(), 8275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor PEnd = D->param_end(); 8285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor P != PEnd; ++P) { 8296477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { 8305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (PInst->getType()->isVoidType()) { 8315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); 8325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor PInst->setInvalidDecl(); 833ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), 834ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump PInst->getType(), 835ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump diag::err_abstract_type_in_decl, 836ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump Sema::AbstractParamType)) 8375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor PInst->setInvalidDecl(); 8385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Params.push_back(PInst); 8405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor ParamTys.push_back(PInst->getType()); 8415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (PInst->isInvalidDecl()) 8435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor InvalidDecl = true; 8445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } else 8455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor InvalidDecl = true; 8465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } 8475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // FIXME: Deallocate dead declarations. 8495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (InvalidDecl) 8505545e166a956a20d7a6b58408e251a1119025485Douglas Gregor return QualType(); 8515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 8525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); 8535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor assert(Proto && "Missing prototype?"); 8545545e166a956a20d7a6b58408e251a1119025485Douglas Gregor QualType ResultType 855ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall = SemaRef.SubstType(Proto->getResultType(), TemplateArgs, 856ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall D->getLocation(), D->getDeclName()); 8575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (ResultType.isNull()) 8585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor return QualType(); 8595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 860beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), 8615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Proto->isVariadic(), Proto->getTypeQuals(), 8625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor D->getLocation(), D->getDeclName()); 8635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor} 8645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 865e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Initializes the common fields of an instantiation function 866e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl). 867e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 868e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error 869e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool 870e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, 871e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *Tmpl) { 872e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (Tmpl->isDeleted()) 873e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor New->setDeleted(); 874cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor 875cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // If we are performing substituting explicitly-specified template arguments 876cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // or deduced template arguments into a function template and we reach this 877cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // point, we are now past the point where SFINAE applies and have committed 878cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // to keeping the new function template specialization. We therefore 879cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // convert the active template instantiation for the function template 880cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // into a template instantiation for this specific function template 881cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // specialization, which is not a SFINAE context, so that we diagnose any 882cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor // further errors in the declaration itself. 883cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor typedef Sema::ActiveTemplateInstantiation ActiveInstType; 884cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); 885cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || 886cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { 887cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor if (FunctionTemplateDecl *FunTmpl 888cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { 889cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor assert(FunTmpl->getTemplatedDecl() == Tmpl && 890cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor "Deduction from the wrong function template?"); 891bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar (void) FunTmpl; 892cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInst.Kind = ActiveInstType::TemplateInstantiation; 893cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); 894cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor } 895cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor } 896cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor 897e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return false; 898e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 899e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 9005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method 9015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template 9025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl). 9035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// 9045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error 9055545e166a956a20d7a6b58408e251a1119025485Douglas Gregorbool 9065545e166a956a20d7a6b58408e251a1119025485Douglas GregorTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, 9075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor CXXMethodDecl *Tmpl) { 908e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (InitFunctionInstantiation(New, Tmpl)) 909e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return true; 910e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 9115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); 9125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor New->setAccess(Tmpl->getAccess()); 91377b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson if (Tmpl->isVirtualAsWritten()) { 91477b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson New->setVirtualAsWritten(true); 9155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setAggregate(false); 9165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setPOD(false); 9171d954f6a0057cb55a3a5d483904a3c57d03c996fEli Friedman Record->setEmpty(false); 9185545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setPolymorphic(true); 9195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } 9205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor if (Tmpl->isPure()) { 9215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor New->setPure(); 9225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor Record->setAbstract(true); 9235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor } 9245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor 9255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // FIXME: attributes 9265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor // FIXME: New needs a pointer to Tmpl 9275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor return false; 9285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor} 929a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor 930a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its 931a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template. 932a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// 933b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was 934b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation" 935b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close. 936b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// 937a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a 938b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template 939b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization. 940b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// 941b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that 942b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation. 943f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, 944b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor FunctionDecl *Function, 945b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor bool Recursive) { 94654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor if (Function->isInvalidDecl()) 94754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor return; 94854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor 9496fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis assert(!Function->getBody() && "Already instantiated!"); 950d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 9511eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor // Find the function body that we'll be substituting. 9521637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor const FunctionDecl *PatternDecl = 0; 9535ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) { 9545ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor while (Primary->getInstantiatedFromMemberTemplate()) 9555ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor Primary = Primary->getInstantiatedFromMemberTemplate(); 9565ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor 9571637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor PatternDecl = Primary->getTemplatedDecl(); 9585ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor } else 9591637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor PatternDecl = Function->getInstantiatedFromMemberFunction(); 9601eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor Stmt *Pattern = 0; 9611eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor if (PatternDecl) 9626fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis Pattern = PatternDecl->getBody(PatternDecl); 9631eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor 9641eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor if (!Pattern) 9651eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor return; 9661eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor 967f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); 968f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor if (Inst) 969f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor return; 970b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor 971b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // If we're performing recursive template instantiation, create our own 972b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // queue of pending implicit instantiations that we will instantiate later, 973b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // while we're still within our own instantiation context. 974b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; 975b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor if (Recursive) 976b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 977b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor 978e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); 979e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor 98054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // Introduce a new scope where local variable instantiations will be 98154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // recorded. 98254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor LocalInstantiationScope Scope(*this); 98354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor 98454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // Introduce the instantiated function parameters into the local 98554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // instantiation scope. 98654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) 98754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), 98854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor Function->getParamDecl(I)); 98954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor 990b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor // Enter the scope of this instantiation. We don't use 991b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor // PushDeclContext because we don't have a scope. 992b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor DeclContext *PreviousContext = CurContext; 993b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor CurContext = Function; 994b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor 995090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson MultiLevelTemplateArgumentList TemplateArgs = 996090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson getTemplateInstantiationArgs(Function); 997090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 998090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // If this is a constructor, instantiate the member initializers. 999090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (const CXXConstructorDecl *Ctor = 1000090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson dyn_cast<CXXConstructorDecl>(PatternDecl)) { 1001090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, 1002090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson TemplateArgs); 1003090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1004090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 100554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor // Instantiate the function body. 1006090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs); 1007e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor 1008e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), 1009e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor /*IsInstantiation=*/true); 1010b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor 1011b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor CurContext = PreviousContext; 1012aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor 1013aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor DeclGroupRef DG(Function); 1014aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor Consumer.HandleTopLevelDecl(DG); 1015b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor 1016b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor if (Recursive) { 1017b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // Instantiate any pending implicit instantiations found during the 1018b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // instantiation of this template. 1019b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PerformPendingImplicitInstantiations(); 1020b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor 1021b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor // Restore the set of pending implicit instantiations. 1022b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 1023b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor } 1024a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor} 1025a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor 1026a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its 1027a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template. 1028a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// 10297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was 10307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation" 10317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close. 10327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// 10337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member 10347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization. 10357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// 10367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that 10377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation. 10387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition( 10397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor SourceLocation PointOfInstantiation, 10407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor VarDecl *Var, 10417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor bool Recursive) { 10427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Var->isInvalidDecl()) 10437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return; 10447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Find the out-of-line definition of this static data member. 10467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // FIXME: Do we have to look for specializations separately? 10477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); 10487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor bool FoundOutOfLineDef = false; 10497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Def && "This data member was not instantiated from a template?"); 10507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Def->isStaticDataMember() && "Not a static data member?"); 10517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor for (VarDecl::redecl_iterator RD = Def->redecls_begin(), 10527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor RDEnd = Def->redecls_end(); 10537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor RD != RDEnd; ++RD) { 10547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (RD->getLexicalDeclContext()->isFileContext()) { 10557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Def = *RD; 10567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor FoundOutOfLineDef = true; 10577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 10587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 10597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (!FoundOutOfLineDef) { 10617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // We did not find an out-of-line definition of this static data member, 10627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // so we won't perform any instantiation. Rather, we rely on the user to 10637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // instantiate this definition (or provide a specialization for it) in 10647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // another translation unit. 10657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return; 10667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 10677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); 10697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Inst) 10707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return; 10717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // If we're performing recursive template instantiation, create our own 10737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // queue of pending implicit instantiations that we will instantiate later, 10747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // while we're still within our own instantiation context. 10757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; 10767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Recursive) 10777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 10787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Enter the scope of this instantiation. We don't use 10807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // PushDeclContext because we don't have a scope. 10817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor DeclContext *PreviousContext = CurContext; 10827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor CurContext = Var->getDeclContext(); 10837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 1084ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), 10857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor getTemplateInstantiationArgs(Var))); 10867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor CurContext = PreviousContext; 10887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Var) { 10907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor DeclGroupRef DG(Var); 10917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor Consumer.HandleTopLevelDecl(DG); 10927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 10937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Recursive) { 10957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Instantiate any pending implicit instantiations found during the 10967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // instantiation of this template. 10977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor PerformPendingImplicitInstantiations(); 10987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 10997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Restore the set of pending implicit instantiations. 11007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); 11017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 1102a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor} 1103815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1104090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid 1105090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New, 1106090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson const CXXConstructorDecl *Tmpl, 1107090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson const MultiLevelTemplateArgumentList &TemplateArgs) { 1108090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1109090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson llvm::SmallVector<MemInitTy*, 4> NewInits; 1110090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1111090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // Instantiate all the initializers. 1112090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), 1113090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson InitsEnd = Tmpl->init_end(); Inits != InitsEnd; ++Inits) { 1114090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson CXXBaseOrMemberInitializer *Init = *Inits; 1115090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1116090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this); 1117090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1118090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // Instantiate all the arguments. 1119090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end(); 1120090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Args != ArgsEnd; ++Args) { 1121090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs); 1122090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1123090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (NewArg.isInvalid()) 1124090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson New->setInvalidDecl(); 1125090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson else 1126090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.push_back(NewArg.takeAs<Expr>()); 1127090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1128090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1129090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson MemInitResult NewInit; 1130090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1131090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (Init->isBaseInitializer()) { 1132090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // FIXME: Type needs to be instantiated. 1133090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson QualType BaseType = 1134090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Context.getCanonicalType(QualType(Init->getBaseClass(), 0)); 1135090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1136090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInit = BuildBaseInitializer(BaseType, 1137090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson (Expr **)NewArgs.data(), 1138090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.size(), 1139090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getSourceLocation(), 1140090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getRParenLoc(), 1141090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson New->getParent()); 1142090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } else if (Init->isMemberInitializer()) { 1143090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson FieldDecl *Member = 1144090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson cast<FieldDecl>(FindInstantiatedDecl(Init->getMember())); 1145090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1146090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), 1147090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.size(), 1148090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getSourceLocation(), 1149090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson Init->getRParenLoc()); 1150090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1151090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1152090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson if (NewInit.isInvalid()) 1153090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson New->setInvalidDecl(); 1154090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson else { 1155090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // FIXME: It would be nice if ASTOwningVector had a release function. 1156090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewArgs.take(); 1157090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1158090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInits.push_back((MemInitTy *)NewInit.get()); 1159090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1160090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson } 1161090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 1162090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson // Assign all the initializers to the new constructor. 1163090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson ActOnMemInitializers(DeclPtrTy::make(New), 1164090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson /*FIXME: ColonLoc */ 1165090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson SourceLocation(), 1166090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson NewInits.data(), NewInits.size()); 1167090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson} 1168090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson 116952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the 117052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name. 117152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern, 117252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall ClassTemplateDecl *Instance) { 117352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Pattern = Pattern->getCanonicalDecl(); 117452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 117552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall do { 117652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getCanonicalDecl(); 117752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Pattern == Instance) return true; 117852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getInstantiatedFromMemberTemplate(); 117952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 118052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 118152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 118252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 118352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 118452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern, 118552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall CXXRecordDecl *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->getInstantiatedFromMemberClass(); 119252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 119352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 119452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 119552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 119652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 119752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern, 119852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall FunctionDecl *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->getInstantiatedFromMemberFunction(); 120552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 120652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 120752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 120852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 120952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 121052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern, 121152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall EnumDecl *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->getInstantiatedFromMemberEnum(); 121852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 121952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 122052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 122152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 122252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 122352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern, 122452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall VarDecl *Instance) { 122552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall assert(Instance->isStaticDataMember()); 122652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 122752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Pattern = Pattern->getCanonicalDecl(); 122852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 122952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall do { 123052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getCanonicalDecl(); 123152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Pattern == Instance) return true; 123252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall Instance = Instance->getInstantiatedFromStaticDataMember(); 123352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } while (Instance); 123452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 123552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return false; 123652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall} 123752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 1238815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { 1239815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor if (D->getKind() != Other->getKind()) 1240815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return false; 1241815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 124252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) 124352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<CXXRecordDecl>(D), Record); 1244a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor 124552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) 124652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<FunctionDecl>(D), Function); 1247815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 124852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) 124952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<EnumDecl>(D), Enum); 1250815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 12517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (VarDecl *Var = dyn_cast<VarDecl>(Other)) 125252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (Var->isStaticDataMember()) 125352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); 125452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 125552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) 125652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); 1257a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor 1258815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // FIXME: How can we find instantiations of anonymous unions? 1259815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1260815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return D->getDeclName() && isa<NamedDecl>(Other) && 1261815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); 1262815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor} 1263815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1264815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator> 1265815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic NamedDecl *findInstantiationOf(ASTContext &Ctx, 1266815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor NamedDecl *D, 1267815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor ForwardIterator first, 1268815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor ForwardIterator last) { 1269815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor for (; first != last; ++first) 1270815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor if (isInstantiationOf(Ctx, D, *first)) 1271815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return cast<NamedDecl>(*first); 1272815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1273815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return 0; 1274815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor} 1275815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 127602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context 127702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation. 127802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// 127902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error 128002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDeclContext *Sema::FindInstantiatedContext(DeclContext* DC) { 128102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { 128202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall Decl* ID = FindInstantiatedDecl(D); 128302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall return cast_or_null<DeclContext>(ID); 128402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall } else return DC; 128502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall} 128602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall 1287ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the 1288ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation. 1289815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1290815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration 1291815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the 1292815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example, 1293815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given: 1294815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1295815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code 1296815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T> 1297815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X { 1298815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// enum Kind { 1299815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// KnownValue = sizeof(T) 1300815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// }; 1301815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1302815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// bool getKind() const { return KnownValue; } 1303815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// }; 1304815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1305815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>; 1306815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode 1307815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// 1308815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the 1309815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to 1310815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation 1311ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs 1312ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>. 1313ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallNamedDecl * Sema::FindInstantiatedDecl(NamedDecl *D) { 1314815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor DeclContext *ParentDC = D->getDeclContext(); 13152bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) { 13162bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor // D is a local of some kind. Look into the map of local 13172bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor // declarations to their instantiations. 13182bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); 13192bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor } 1320815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 132152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) 132252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (ClassTemplateDecl *ClassTemplate 132352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall = Record->getDescribedClassTemplate()) { 132452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // When the declaration D was parsed, it referred to the current 132552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // instantiation. Therefore, look through the current context, 132652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // which contains actual instantiations, to find the 132752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // instantiation of the "current instantiation" that D refers 132852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // to. Alternatively, we could just instantiate the 132952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // injected-class-name with the current template arguments, but 133052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall // such an instantiation is far more expensive. 133152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall for (DeclContext *DC = CurContext; !DC->isFileContext(); 133252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall DC = DC->getParent()) { 133352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (ClassTemplateSpecializationDecl *Spec 133452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall = dyn_cast<ClassTemplateSpecializationDecl>(DC)) 133552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall if (isInstantiationOf(ClassTemplate, Spec->getSpecializedTemplate())) 133652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall return Spec; 133752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } 133852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 133952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall assert(false && 134052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall "Unable to find declaration for the current instantiation"); 134152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall } 134252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 134302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall ParentDC = FindInstantiatedContext(ParentDC); 134402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall if (!ParentDC) return 0; 1345815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1346815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor if (ParentDC != D->getDeclContext()) { 1347815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // We performed some kind of instantiation in the parent context, 1348815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // so now we need to look into the instantiated parent context to 1349815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // find the instantiation of the declaration D. 1350815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor NamedDecl *Result = 0; 1351815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor if (D->getDeclName()) { 135217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); 1353815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor Result = findInstantiationOf(Context, D, Found.first, Found.second); 1354815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor } else { 1355815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // Since we don't have a name for the entity we're looking for, 1356815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // our only option is to walk through all of the declarations to 1357815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // find that name. This will occur in a few cases: 1358815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // 1359815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // - anonymous struct/union within a template 1360815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // - unnamed class/struct/union/enum within a template 1361815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // 1362815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor // FIXME: Find a better way to find these instantiations! 1363815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor Result = findInstantiationOf(Context, D, 136417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ParentDC->decls_begin(), 136517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ParentDC->decls_end()); 1366815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor } 136752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall 1368815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor assert(Result && "Unable to find instantiation of declaration!"); 1369815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor D = Result; 1370815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor } 1371815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor 1372815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor return D; 1373815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor} 1374d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 1375d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// \brief Performs template instantiation for all implicit template 1376d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point. 1377d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() { 1378d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor while (!PendingImplicitInstantiations.empty()) { 1379d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front(); 1380b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor PendingImplicitInstantiations.pop_front(); 1381d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 13827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Instantiate function definitions 13837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { 13846fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (!Function->getBody()) 1385b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); 13867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor continue; 13877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor } 1388d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor 13897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Instantiate static data member definitions. 13907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor VarDecl *Var = cast<VarDecl>(Inst.first); 13917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Var->isStaticDataMember() && "Not a static data member?"); 13927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); 1393d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor } 1394d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor} 1395