SemaTemplateInstantiateDecl.cpp revision 8dbc2694424b4e842b1d5ea39744a137b58600c3
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" 138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h" 148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h" 158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h" 168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h" 178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/Compiler.h" 188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang; 208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace { 228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor class VISIBILITY_HIDDEN TemplateDeclInstantiator 238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor : public DeclVisitor<TemplateDeclInstantiator, Decl *> 248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor { 258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Sema &SemaRef; 268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor DeclContext *Owner; 278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor const TemplateArgument *TemplateArgs; 288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor unsigned NumTemplateArgs; 298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor public: 318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor typedef Sema::OwningExprResult OwningExprResult; 328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, 348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor const TemplateArgument *TemplateArgs, 358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor unsigned NumTemplateArgs) 368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs), 378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor NumTemplateArgs(NumTemplateArgs) { } 388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // FIXME: Once we get closer to completion, replace these 408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // manually-written declarations with automatically-generated ones 418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // from clang/AST/DeclNodes.def. 428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitTypedefDecl(TypedefDecl *D); 438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitFieldDecl(FieldDecl *D); 448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitStaticAssertDecl(StaticAssertDecl *D); 458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitEnumDecl(EnumDecl *D); 468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Base case. FIXME: Remove once we can instantiate everything. 488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *VisitDecl(Decl *) { 498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor }; 528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { 558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor bool Invalid = false; 568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor QualType T = D->getUnderlyingType(); 578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (T->isDependentType()) { 588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, 598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getLocation(), 608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getDeclName()); 618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (T.isNull()) { 628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Invalid = true; 638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor T = SemaRef.Context.IntTy; 648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Create the new typedef 688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor TypedefDecl *Typedef 698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), 708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getIdentifier(), T); 718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Invalid) 728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Typedef->setInvalidDecl(); 738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Owner->addDecl(Typedef); 758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Typedef; 768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { 798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor bool Invalid = false; 808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor QualType T = D->getType(); 818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (T->isDependentType()) { 828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, 838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getLocation(), 848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getDeclName()); 858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (!T.isNull() && T->isFunctionType()) { 868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // C++ [temp.arg.type]p3: 878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // If a declaration acquires a function type through a type 888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // dependent on a template-parameter and this causes a 898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // declaration that does not use the syntactic form of a 908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // function declarator to have function type, the program is 918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // ill-formed. 928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) 938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor << T; 948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor T = QualType(); 958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Invalid = true; 968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Expr *BitWidth = D->getBitWidth(); 1008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Invalid) 1018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor BitWidth = 0; 1028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor else if (BitWidth) { 1038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult InstantiatedBitWidth 1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = SemaRef.InstantiateExpr(BitWidth, TemplateArgs, NumTemplateArgs); 1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (InstantiatedBitWidth.isInvalid()) { 1068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Invalid = true; 1078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor BitWidth = 0; 1088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } else 1098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor BitWidth = (Expr *)InstantiatedBitWidth.release(); 1108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, 1138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor cast<RecordDecl>(Owner), 1148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getLocation(), 1158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->isMutable(), 1168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor BitWidth, 1178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getAccess(), 1188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 0); 1198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Field) { 1208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Invalid) 1218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Field->setInvalidDecl(); 1228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Owner->addDecl(Field); 1248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Field; 1278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 1288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { 1308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Expr *AssertExpr = D->getAssertExpr(); 1318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult InstantiatedAssertExpr 1338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs, NumTemplateArgs); 1348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (InstantiatedAssertExpr.isInvalid()) 1358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return 0; 1368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult Message = SemaRef.Clone(D->getMessage()); 1388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Decl *StaticAssert 1398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = (Decl *)SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), 1408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor move(InstantiatedAssertExpr), 1418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor move(Message)); 1428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return StaticAssert; 1438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 1448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { 1468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, 1478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor D->getLocation(), D->getIdentifier(), 1488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor /*PrevDecl=*/0); 1498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Owner->addDecl(Enum); 1508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enum->startDefinition(); 1518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor llvm::SmallVector<Sema::DeclTy *, 16> Enumerators; 1538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConstantDecl *LastEnumConst = 0; 1558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), 1568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor ECEnd = D->enumerator_end(); 1578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EC != ECEnd; ++EC) { 1588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // The specified value for the enumerator. 1598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor OwningExprResult Value = SemaRef.Owned((Expr *)0); 1608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Expr *UninstValue = EC->getInitExpr()) 1618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Value = SemaRef.InstantiateExpr(UninstValue, 1628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor TemplateArgs, NumTemplateArgs); 1638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor // Drop the initial value and continue. 1658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor bool isInvalid = false; 1668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (Value.isInvalid()) { 1678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Value = SemaRef.Owned((Expr *)0); 1688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor isInvalid = true; 1698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConstantDecl *EnumConst 1728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor = SemaRef.CheckEnumConstant(Enum, LastEnumConst, 1738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EC->getLocation(), EC->getIdentifier(), 1748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor move(Value)); 1758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (isInvalid) { 1778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (EnumConst) 1788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor EnumConst->setInvalidDecl(); 1798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enum->setInvalidDecl(); 1808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor if (EnumConst) { 1838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enum->addDecl(EnumConst); 1848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor Enumerators.push_back(EnumConst); 1858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor LastEnumConst = EnumConst; 1868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor } 1888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor SemaRef.ActOnEnumBody(Enum->getLocation(), Enum, 1908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor &Enumerators[0], Enumerators.size()); 1918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Enum; 1938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 1948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 1958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner, 1968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor const TemplateArgument *TemplateArgs, 1978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor unsigned NumTemplateArgs) { 1988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs, 1998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor NumTemplateArgs); 2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor return Instantiator.Visit(D); 2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor} 2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor 203