blob: 759e2c16f1dbf471b942e5de0f8974795d372046 [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation for declarations.
10//
11//===----------------------------------------------------------------------===/
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCallf312b1e2010-08-26 23:41:50 +000014#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000020#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000021#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000022#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000023#include "clang/AST/TypeLoc.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000024#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025
26using namespace clang;
27
John McCallb6217662010-03-15 10:12:16 +000028bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29 DeclaratorDecl *NewDecl) {
30 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
31 if (!OldQual) return false;
32
33 SourceRange QualRange = OldDecl->getQualifierRange();
34
35 NestedNameSpecifier *NewQual
36 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
37 if (!NewQual)
38 return true;
39
40 NewDecl->setQualifierInfo(NewQual, QualRange);
41 return false;
42}
43
44bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45 TagDecl *NewDecl) {
46 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
47 if (!OldQual) return false;
48
49 SourceRange QualRange = OldDecl->getQualifierRange();
50
51 NestedNameSpecifier *NewQual
52 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
53 if (!NewQual)
54 return true;
55
56 NewDecl->setQualifierInfo(NewQual, QualRange);
57 return false;
58}
59
Chandler Carruth4ced79f2010-06-25 03:22:07 +000060// FIXME: Is this still too simple?
John McCall1d8d1cc2010-08-01 02:01:53 +000061void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
62 Decl *Tmpl, Decl *New) {
Sean Huntcf807c42010-08-18 23:23:40 +000063 for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
64 i != e; ++i) {
65 const Attr *TmplAttr = *i;
Chandler Carruth4ced79f2010-06-25 03:22:07 +000066 // FIXME: This should be generalized to more than just the AlignedAttr.
67 if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
Sean Huntcf807c42010-08-18 23:23:40 +000068 if (Aligned->isAlignmentDependent()) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +000069 // The alignment expression is not potentially evaluated.
John McCall1d8d1cc2010-08-01 02:01:53 +000070 EnterExpressionEvaluationContext Unevaluated(*this,
John McCallf312b1e2010-08-26 23:41:50 +000071 Sema::Unevaluated);
Chandler Carruth4ced79f2010-06-25 03:22:07 +000072
Sean Huntcf807c42010-08-18 23:23:40 +000073 if (Aligned->isAlignmentExpr()) {
John McCall60d7b3a2010-08-24 06:29:42 +000074 ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
Nick Lewycky7663f392010-11-20 01:29:55 +000075 TemplateArgs);
Sean Huntcf807c42010-08-18 23:23:40 +000076 if (!Result.isInvalid())
77 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
78 }
79 else {
80 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
Nick Lewycky7663f392010-11-20 01:29:55 +000081 TemplateArgs,
82 Aligned->getLocation(),
83 DeclarationName());
Sean Huntcf807c42010-08-18 23:23:40 +000084 if (Result)
85 AddAlignedAttr(Aligned->getLocation(), New, Result);
86 }
Chandler Carruth4ced79f2010-06-25 03:22:07 +000087 continue;
88 }
89 }
90
Anders Carlssond8fe2d52009-11-07 06:07:58 +000091 // FIXME: Is cloning correct for all attributes?
John McCall1d8d1cc2010-08-01 02:01:53 +000092 Attr *NewAttr = TmplAttr->clone(Context);
Anders Carlssond8fe2d52009-11-07 06:07:58 +000093 New->addAttr(NewAttr);
94 }
95}
96
Douglas Gregor4f722be2009-03-25 15:45:12 +000097Decl *
98TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
99 assert(false && "Translation units cannot be instantiated");
100 return D;
101}
102
103Decl *
104TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
105 assert(false && "Namespaces cannot be instantiated");
106 return D;
107}
108
John McCall3dbd3d52010-02-16 06:53:13 +0000109Decl *
110TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
111 NamespaceAliasDecl *Inst
112 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
113 D->getNamespaceLoc(),
114 D->getAliasLoc(),
115 D->getNamespace()->getIdentifier(),
116 D->getQualifierRange(),
117 D->getQualifier(),
118 D->getTargetNameLoc(),
119 D->getNamespace());
120 Owner->addDecl(Inst);
121 return Inst;
122}
123
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000124Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
125 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000126 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000127 if (DI->getType()->isDependentType() ||
128 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000129 DI = SemaRef.SubstType(DI, TemplateArgs,
130 D->getLocation(), D->getDeclName());
131 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000132 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000133 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000134 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000135 } else {
136 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000139 // Create the new typedef
140 TypedefDecl *Typedef
141 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000142 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000143 if (Invalid)
144 Typedef->setInvalidDecl();
145
Douglas Gregord57a38e2010-04-23 16:25:07 +0000146 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
147 TagDecl *TD = TT->getDecl();
148
149 // If the TagDecl that the TypedefDecl points to is an anonymous decl
150 // keep track of the TypedefDecl.
151 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
152 TD->setTypedefForAnonDecl(Typedef);
153 }
154
John McCall5126fd02009-12-30 00:31:22 +0000155 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000156 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
157 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000158 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
159 }
160
John McCall1d8d1cc2010-08-01 02:01:53 +0000161 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000162
John McCall46460a62010-01-20 21:53:11 +0000163 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000164 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000166 return Typedef;
167}
168
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000169/// \brief Instantiate an initializer, breaking it into separate
170/// initialization arguments.
171///
172/// \param S The semantic analysis object.
173///
174/// \param Init The initializer to instantiate.
175///
176/// \param TemplateArgs Template arguments to be substituted into the
177/// initializer.
178///
179/// \param NewArgs Will be filled in with the instantiation arguments.
180///
181/// \returns true if an error occurred, false otherwise
182static bool InstantiateInitializer(Sema &S, Expr *Init,
183 const MultiLevelTemplateArgumentList &TemplateArgs,
184 SourceLocation &LParenLoc,
Nick Lewycky7663f392010-11-20 01:29:55 +0000185 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000186 SourceLocation &RParenLoc) {
187 NewArgs.clear();
188 LParenLoc = SourceLocation();
189 RParenLoc = SourceLocation();
190
191 if (!Init)
192 return false;
193
John McCall4765fa02010-12-06 08:20:24 +0000194 if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000195 Init = ExprTemp->getSubExpr();
196
197 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
198 Init = Binder->getSubExpr();
199
200 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
201 Init = ICE->getSubExprAsWritten();
202
203 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
204 LParenLoc = ParenList->getLParenLoc();
205 RParenLoc = ParenList->getRParenLoc();
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000206 return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
207 true, TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000208 }
209
210 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000211 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000212 if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
213 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000214 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000215
Douglas Gregor28329e52010-03-24 21:22:47 +0000216 // FIXME: Fake locations!
217 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000218 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000219 return false;
220 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000221 }
222
John McCall60d7b3a2010-08-24 06:29:42 +0000223 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000224 if (Result.isInvalid())
225 return true;
226
227 NewArgs.push_back(Result.takeAs<Expr>());
228 return false;
229}
230
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000231Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000232 // If this is the variable for an anonymous struct or union,
233 // instantiate the anonymous struct/union type first.
234 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
235 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
236 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
237 return 0;
238
John McCallce3ff2b2009-08-25 22:02:44 +0000239 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000240 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000241 TemplateArgs,
242 D->getTypeSpecStartLoc(),
243 D->getDeclName());
244 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000245 return 0;
246
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000247 if (DI->getType()->isFunctionType()) {
248 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
249 << D->isStaticDataMember() << DI->getType();
250 return 0;
251 }
252
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000253 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000254 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
255 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000256 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000257 D->getStorageClass(),
258 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000259 Var->setThreadSpecified(D->isThreadSpecified());
260 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000261
John McCallb6217662010-03-15 10:12:16 +0000262 // Substitute the nested name specifier, if any.
263 if (SubstQualifier(D, Var))
264 return 0;
265
Mike Stump1eb44332009-09-09 15:08:12 +0000266 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000267 // out-of-line, the instantiation will have the same lexical
268 // context (which will be a namespace scope) as the template.
269 if (D->isOutOfLine())
270 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000271
John McCall46460a62010-01-20 21:53:11 +0000272 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000273
274 if (!D->isStaticDataMember())
275 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000276
Mike Stump390b4cc2009-05-16 07:39:55 +0000277 // FIXME: In theory, we could have a previous declaration for variables that
278 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000279 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000280 // FIXME: having to fake up a LookupResult is dumb.
281 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000282 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000283 if (D->isStaticDataMember())
284 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000285 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Douglas Gregor7caa6822009-07-24 20:34:43 +0000287 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000288 if (!D->isStaticDataMember())
289 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000290 Owner->makeDeclVisibleInContext(Var);
291 } else {
292 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000293 if (Owner->isFunctionOrMethod())
294 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000295 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000296 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000297
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000298 // Link instantiations of static data members back to the template from
299 // which they were instantiated.
300 if (Var->isStaticDataMember())
301 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000302 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000303
Douglas Gregor60c93c92010-02-09 07:26:29 +0000304 if (Var->getAnyInitializer()) {
305 // We already have an initializer in the class.
306 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000307 if (Var->isStaticDataMember() && !D->isOutOfLine())
308 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
309 else
310 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
311
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000312 // Instantiate the initializer.
313 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000314 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000315 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000316 InitArgs, RParenLoc)) {
Douglas Gregor07a77b42011-01-14 17:12:22 +0000317 // Attach the initializer to the declaration, if we have one.
318 if (InitArgs.size() == 0)
319 SemaRef.ActOnUninitializedDecl(Var, false);
320 else if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000321 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000322 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000323 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000324 move_arg(InitArgs),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000325 RParenLoc);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000326 } else {
327 assert(InitArgs.size() == 1);
John McCall9ae2f072010-08-23 23:25:46 +0000328 Expr *Init = InitArgs.take()[0];
329 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000330 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000331 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000332 // FIXME: Not too happy about invalidating the declaration
333 // because of a bogus initializer.
334 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000335 }
336
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000337 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000338 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000339 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000340
Douglas Gregor5764f612010-05-08 23:05:03 +0000341 // Diagnose unused local variables.
342 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
343 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000344
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000345 return Var;
346}
347
Abramo Bagnara6206d532010-06-05 05:09:32 +0000348Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
349 AccessSpecDecl* AD
350 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
351 D->getAccessSpecifierLoc(), D->getColonLoc());
352 Owner->addHiddenDecl(AD);
353 return AD;
354}
355
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000356Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
357 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000358 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000359 if (DI->getType()->isDependentType() ||
360 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000361 DI = SemaRef.SubstType(DI, TemplateArgs,
362 D->getLocation(), D->getDeclName());
363 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000364 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000365 Invalid = true;
366 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000367 // C++ [temp.arg.type]p3:
368 // If a declaration acquires a function type through a type
369 // dependent on a template-parameter and this causes a
370 // declaration that does not use the syntactic form of a
371 // function declarator to have function type, the program is
372 // ill-formed.
373 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000374 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000375 Invalid = true;
376 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000377 } else {
378 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000379 }
380
381 Expr *BitWidth = D->getBitWidth();
382 if (Invalid)
383 BitWidth = 0;
384 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000385 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000386 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
John McCall60d7b3a2010-08-24 06:29:42 +0000388 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000389 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000390 if (InstantiatedBitWidth.isInvalid()) {
391 Invalid = true;
392 BitWidth = 0;
393 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000394 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000395 }
396
John McCall07fb6be2009-10-22 23:33:21 +0000397 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
398 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000399 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000400 D->getLocation(),
401 D->isMutable(),
402 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000403 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000404 D->getAccess(),
405 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000406 if (!Field) {
407 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000408 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
John McCall1d8d1cc2010-08-01 02:01:53 +0000411 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000412
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000413 if (Invalid)
414 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000416 if (!Field->getDeclName()) {
417 // Keep track of where this decl came from.
418 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000419 }
420 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
421 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000422 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000423 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000426 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000427 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000428 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000429
430 return Field;
431}
432
Francois Pichet87c2e122010-11-21 06:08:52 +0000433Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
434 NamedDecl **NamedChain =
435 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
436
437 int i = 0;
438 for (IndirectFieldDecl::chain_iterator PI =
439 D->chain_begin(), PE = D->chain_end();
440 PI != PE; ++PI)
441 NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
442 *PI, TemplateArgs));
443
Francois Pichet40e17752010-12-09 10:07:54 +0000444 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000445 IndirectFieldDecl* IndirectField
446 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000447 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000448 NamedChain, D->getChainingSize());
449
450
451 IndirectField->setImplicit(D->isImplicit());
452 IndirectField->setAccess(D->getAccess());
453 Owner->addDecl(IndirectField);
454 return IndirectField;
455}
456
John McCall02cace72009-08-28 07:59:38 +0000457Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000458 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000459 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000460 if (TypeSourceInfo *Ty = D->getFriendType()) {
461 TypeSourceInfo *InstTy =
462 SemaRef.SubstType(Ty, TemplateArgs,
463 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000464 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000465 return 0;
John McCall02cace72009-08-28 07:59:38 +0000466
Douglas Gregor06245bf2010-04-07 17:57:12 +0000467 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
468 if (!FD)
469 return 0;
470
471 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000472 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000473 Owner->addDecl(FD);
474 return FD;
475 }
476
477 NamedDecl *ND = D->getFriendDecl();
478 assert(ND && "friend decl must be a decl or a type!");
479
John McCallaf2094e2010-04-08 09:05:18 +0000480 // All of the Visit implementations for the various potential friend
481 // declarations have to be carefully written to work for friend
482 // objects, with the most important detail being that the target
483 // decl should almost certainly not be placed in Owner.
484 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000485 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
John McCall02cace72009-08-28 07:59:38 +0000487 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000488 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
489 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000490 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000491 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000492 Owner->addDecl(FD);
493 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000494}
495
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000496Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
497 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregorac7610d2009-06-22 20:57:11 +0000499 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000500 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
John McCall60d7b3a2010-08-24 06:29:42 +0000502 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000503 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000504 if (InstantiatedAssertExpr.isInvalid())
505 return 0;
506
John McCall60d7b3a2010-08-24 06:29:42 +0000507 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000508 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000509 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000510 InstantiatedAssertExpr.get(),
511 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000512}
513
514Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000515 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000516 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000517 D->getTagKeywordLoc(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000518 /*PrevDecl=*/0, D->isScoped(),
519 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000520 if (D->isFixed()) {
521 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
522 // If we have type source information for the underlying type, it means it
523 // has been explicitly set by the user. Perform substitution on it before
524 // moving on.
525 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
526 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
527 TemplateArgs,
528 UnderlyingLoc,
529 DeclarationName()));
530
531 if (!Enum->getIntegerTypeSourceInfo())
532 Enum->setIntegerType(SemaRef.Context.IntTy);
533 }
534 else {
535 assert(!D->getIntegerType()->isDependentType()
536 && "Dependent type without type source info");
537 Enum->setIntegerType(D->getIntegerType());
538 }
539 }
540
John McCall5b629aa2010-10-22 23:36:17 +0000541 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
542
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000543 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000544 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000545 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000547 Enum->startDefinition();
548
Douglas Gregor96084f12010-03-01 19:00:07 +0000549 if (D->getDeclContext()->isFunctionOrMethod())
550 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
551
John McCalld226f652010-08-21 09:40:31 +0000552 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000553
554 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000555 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
556 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000557 EC != ECEnd; ++EC) {
558 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000559 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000560 if (Expr *UninstValue = EC->getInitExpr()) {
561 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000562 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000563 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
John McCallce3ff2b2009-08-25 22:02:44 +0000565 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000566 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000567
568 // Drop the initial value and continue.
569 bool isInvalid = false;
570 if (Value.isInvalid()) {
571 Value = SemaRef.Owned((Expr *)0);
572 isInvalid = true;
573 }
574
Mike Stump1eb44332009-09-09 15:08:12 +0000575 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000576 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
577 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000578 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000579
580 if (isInvalid) {
581 if (EnumConst)
582 EnumConst->setInvalidDecl();
583 Enum->setInvalidDecl();
584 }
585
586 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000587 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
588
John McCall3b85ecf2010-01-23 22:37:59 +0000589 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000590 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000591 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000592 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000593
594 if (D->getDeclContext()->isFunctionOrMethod()) {
595 // If the enumeration is within a function or method, record the enum
596 // constant as a local.
597 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
598 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000599 }
600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000602 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000603 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000604 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000605 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000606 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000607 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000608
609 return Enum;
610}
611
Douglas Gregor6477b692009-03-25 15:04:13 +0000612Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
613 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
614 return 0;
615}
616
John McCalle29ba202009-08-20 01:44:21 +0000617Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000618 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
619
Douglas Gregor550d9b22009-10-31 17:21:17 +0000620 // Create a local instantiation scope for this class template, which
621 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000622 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000623 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000624 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000625 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000626 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000627
628 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000629
630 // Instantiate the qualifier. We have to do this first in case
631 // we're a friend declaration, because if we are then we need to put
632 // the new declaration in the appropriate context.
633 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
634 if (Qualifier) {
635 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
636 Pattern->getQualifierRange(),
637 TemplateArgs);
638 if (!Qualifier) return 0;
639 }
640
641 CXXRecordDecl *PrevDecl = 0;
642 ClassTemplateDecl *PrevClassTemplate = 0;
643
Nick Lewycky37574f52010-11-08 23:29:42 +0000644 if (!isFriend && Pattern->getPreviousDeclaration()) {
645 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
646 if (Found.first != Found.second) {
647 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
648 if (PrevClassTemplate)
649 PrevDecl = PrevClassTemplate->getTemplatedDecl();
650 }
651 }
652
John McCall93ba8572010-03-25 06:39:04 +0000653 // If this isn't a friend, then it's a member template, in which
654 // case we just want to build the instantiation in the
655 // specialization. If it is a friend, we want to build it in
656 // the appropriate context.
657 DeclContext *DC = Owner;
658 if (isFriend) {
659 if (Qualifier) {
660 CXXScopeSpec SS;
661 SS.setScopeRep(Qualifier);
662 SS.setRange(Pattern->getQualifierRange());
663 DC = SemaRef.computeDeclContext(SS);
664 if (!DC) return 0;
665 } else {
666 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
667 Pattern->getDeclContext(),
668 TemplateArgs);
669 }
670
671 // Look for a previous declaration of the template in the owning
672 // context.
673 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
674 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
675 SemaRef.LookupQualifiedName(R, DC);
676
677 if (R.isSingleResult()) {
678 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
679 if (PrevClassTemplate)
680 PrevDecl = PrevClassTemplate->getTemplatedDecl();
681 }
682
683 if (!PrevClassTemplate && Qualifier) {
684 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000685 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
686 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000687 return 0;
688 }
689
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000690 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000691 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000692 bool Complain = true;
693
694 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
695 // template for struct std::tr1::__detail::_Map_base, where the
696 // template parameters of the friend declaration don't match the
697 // template parameters of the original declaration. In this one
698 // case, we don't complain about the ill-formed friend
699 // declaration.
700 if (isFriend && Pattern->getIdentifier() &&
701 Pattern->getIdentifier()->isStr("_Map_base") &&
702 DC->isNamespace() &&
703 cast<NamespaceDecl>(DC)->getIdentifier() &&
704 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
705 DeclContext *DCParent = DC->getParent();
706 if (DCParent->isNamespace() &&
707 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
708 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
709 DeclContext *DCParent2 = DCParent->getParent();
710 if (DCParent2->isNamespace() &&
711 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
712 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
713 DCParent2->getParent()->isTranslationUnit())
714 Complain = false;
715 }
716 }
717
John McCall93ba8572010-03-25 06:39:04 +0000718 TemplateParameterList *PrevParams
719 = PrevClassTemplate->getTemplateParameters();
720
721 // Make sure the parameter lists match.
722 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000723 Complain,
724 Sema::TPL_TemplateMatch)) {
725 if (Complain)
726 return 0;
727
728 AdoptedPreviousTemplateParams = true;
729 InstParams = PrevParams;
730 }
John McCall93ba8572010-03-25 06:39:04 +0000731
732 // Do some additional validation, then merge default arguments
733 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000734 if (!AdoptedPreviousTemplateParams &&
735 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000736 Sema::TPC_ClassTemplate))
737 return 0;
738 }
739 }
740
John McCalle29ba202009-08-20 01:44:21 +0000741 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000742 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000743 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000744 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000745 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000746
John McCall93ba8572010-03-25 06:39:04 +0000747 if (Qualifier)
748 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000749
John McCalle29ba202009-08-20 01:44:21 +0000750 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000751 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
752 D->getIdentifier(), InstParams, RecordInst,
753 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000754 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000755
John McCall93ba8572010-03-25 06:39:04 +0000756 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000757 if (PrevClassTemplate)
758 Inst->setAccess(PrevClassTemplate->getAccess());
759 else
760 Inst->setAccess(D->getAccess());
761
John McCall93ba8572010-03-25 06:39:04 +0000762 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
763 // TODO: do we want to track the instantiation progeny of this
764 // friend target decl?
765 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000766 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000767 if (!PrevClassTemplate)
768 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000769 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000770
771 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000772 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000773 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000774
Douglas Gregor259571e2009-10-30 22:42:42 +0000775 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000776 if (isFriend) {
777 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000778 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000779 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000780
John McCalle29ba202009-08-20 01:44:21 +0000781 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000782
783 if (!PrevClassTemplate) {
784 // Queue up any out-of-line partial specializations of this member
785 // class template; the client will force their instantiation once
786 // the enclosing class has been instantiated.
787 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
788 D->getPartialSpecializations(PartialSpecs);
789 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
790 if (PartialSpecs[I]->isOutOfLine())
791 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
792 }
793
John McCalle29ba202009-08-20 01:44:21 +0000794 return Inst;
795}
796
Douglas Gregord60e1052009-08-27 16:57:43 +0000797Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000798TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
799 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000800 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
801
802 // Lookup the already-instantiated declaration in the instantiation
803 // of the class template and return that.
804 DeclContext::lookup_result Found
805 = Owner->lookup(ClassTemplate->getDeclName());
806 if (Found.first == Found.second)
807 return 0;
808
809 ClassTemplateDecl *InstClassTemplate
810 = dyn_cast<ClassTemplateDecl>(*Found.first);
811 if (!InstClassTemplate)
812 return 0;
813
Douglas Gregord65587f2010-11-10 19:44:59 +0000814 if (ClassTemplatePartialSpecializationDecl *Result
815 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
816 return Result;
817
818 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000819}
820
821Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000822TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000823 // Create a local instantiation scope for this function template, which
824 // will contain the instantiations of the template parameters and then get
825 // merged with the local instantiation scope for the function template
826 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000827 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000828
Douglas Gregord60e1052009-08-27 16:57:43 +0000829 TemplateParameterList *TempParams = D->getTemplateParameters();
830 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000831 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000832 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000833
Douglas Gregora735b202009-10-13 14:39:41 +0000834 FunctionDecl *Instantiated = 0;
835 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
836 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
837 InstParams));
838 else
839 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
840 D->getTemplatedDecl(),
841 InstParams));
842
843 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000844 return 0;
845
John McCall46460a62010-01-20 21:53:11 +0000846 Instantiated->setAccess(D->getAccess());
847
Mike Stump1eb44332009-09-09 15:08:12 +0000848 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000849 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000850 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000851 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000852 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000853 assert(InstTemplate &&
854 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000855
John McCallb1a56e72010-03-26 23:10:15 +0000856 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
857
John McCalle976ffe2009-12-14 23:19:40 +0000858 // Link the instantiation back to the pattern *unless* this is a
859 // non-definition friend declaration.
860 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000861 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000862 InstTemplate->setInstantiatedFromMemberTemplate(D);
863
John McCallb1a56e72010-03-26 23:10:15 +0000864 // Make declarations visible in the appropriate context.
865 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000866 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000867
Douglas Gregord60e1052009-08-27 16:57:43 +0000868 return InstTemplate;
869}
870
Douglas Gregord475b8d2009-03-25 21:17:03 +0000871Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
872 CXXRecordDecl *PrevDecl = 0;
873 if (D->isInjectedClassName())
874 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000875 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000876 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
877 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000878 TemplateArgs);
879 if (!Prev) return 0;
880 PrevDecl = cast<CXXRecordDecl>(Prev);
881 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000882
883 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000884 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000885 D->getLocation(), D->getIdentifier(),
886 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000887
888 // Substitute the nested name specifier, if any.
889 if (SubstQualifier(D, Record))
890 return 0;
891
Douglas Gregord475b8d2009-03-25 21:17:03 +0000892 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000893 // FIXME: Check against AS_none is an ugly hack to work around the issue that
894 // the tag decls introduced by friend class declarations don't have an access
895 // specifier. Remove once this area of the code gets sorted out.
896 if (D->getAccess() != AS_none)
897 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000898 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000899 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000900
John McCall02cace72009-08-28 07:59:38 +0000901 // If the original function was part of a friend declaration,
902 // inherit its namespace state.
903 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
904 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
905
Douglas Gregor9901c572010-05-21 00:31:19 +0000906 // Make sure that anonymous structs and unions are recorded.
907 if (D->isAnonymousStructOrUnion()) {
908 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000909 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000910 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
911 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000912
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000913 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000914 return Record;
915}
916
John McCall02cace72009-08-28 07:59:38 +0000917/// Normal class members are of more specific types and therefore
918/// don't make it here. This function serves two purposes:
919/// 1) instantiating function templates
920/// 2) substituting friend declarations
921/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000922Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000923 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000924 // Check whether there is already a function template specialization for
925 // this declaration.
926 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
927 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000928 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000929 std::pair<const TemplateArgument *, unsigned> Innermost
930 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000932 FunctionDecl *SpecFunc
933 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
934 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor127102b2009-06-29 20:59:39 +0000936 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000937 if (SpecFunc)
938 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
John McCallb0cb0222010-03-27 05:57:59 +0000941 bool isFriend;
942 if (FunctionTemplate)
943 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
944 else
945 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
946
Douglas Gregor79c22782010-01-16 20:21:20 +0000947 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000948 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000949 !(isa<Decl>(Owner) &&
950 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000951 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Douglas Gregore53060f2009-06-25 22:08:12 +0000953 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000954 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
955 TInfo = SubstFunctionType(D, Params);
956 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000957 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000958 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000959
John McCalld325daa2010-03-26 04:53:08 +0000960 NestedNameSpecifier *Qualifier = D->getQualifier();
961 if (Qualifier) {
962 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
963 D->getQualifierRange(),
964 TemplateArgs);
965 if (!Qualifier) return 0;
966 }
967
John McCall68b6b872010-02-06 01:50:47 +0000968 // If we're instantiating a local function declaration, put the result
969 // in the owner; otherwise we need to find the instantiated context.
970 DeclContext *DC;
971 if (D->getDeclContext()->isFunctionOrMethod())
972 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000973 else if (isFriend && Qualifier) {
974 CXXScopeSpec SS;
975 SS.setScopeRep(Qualifier);
976 SS.setRange(D->getQualifierRange());
977 DC = SemaRef.computeDeclContext(SS);
978 if (!DC) return 0;
979 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000980 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
981 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000982 }
John McCall68b6b872010-02-06 01:50:47 +0000983
John McCall02cace72009-08-28 07:59:38 +0000984 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000985 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000986 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000987 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000988 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000989
John McCalld325daa2010-03-26 04:53:08 +0000990 if (Qualifier)
991 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000992
John McCallb1a56e72010-03-26 23:10:15 +0000993 DeclContext *LexicalDC = Owner;
994 if (!isFriend && D->isOutOfLine()) {
995 assert(D->getDeclContext()->isFileContext());
996 LexicalDC = D->getDeclContext();
997 }
998
999 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregore53060f2009-06-25 22:08:12 +00001001 // Attach the parameters
1002 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001003 if (Params[P])
1004 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001005 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001006
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001007 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001008 if (TemplateParams) {
1009 // Our resulting instantiation is actually a function template, since we
1010 // are substituting only the outer template parameters. For example, given
1011 //
1012 // template<typename T>
1013 // struct X {
1014 // template<typename U> friend void f(T, U);
1015 // };
1016 //
1017 // X<int> x;
1018 //
1019 // We are instantiating the friend function template "f" within X<int>,
1020 // which means substituting int for T, but leaving "f" as a friend function
1021 // template.
1022 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001023 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001024 Function->getLocation(),
1025 Function->getDeclName(),
1026 TemplateParams, Function);
1027 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001028
1029 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001030
1031 if (isFriend && D->isThisDeclarationADefinition()) {
1032 // TODO: should we remember this connection regardless of whether
1033 // the friend declaration provided a body?
1034 FunctionTemplate->setInstantiatedFromMemberTemplate(
1035 D->getDescribedFunctionTemplate());
1036 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001037 } else if (FunctionTemplate) {
1038 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001039 std::pair<const TemplateArgument *, unsigned> Innermost
1040 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001041 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001042 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001043 Innermost.first,
1044 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001045 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001046 } else if (isFriend && D->isThisDeclarationADefinition()) {
1047 // TODO: should we remember this connection regardless of whether
1048 // the friend declaration provided a body?
1049 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001050 }
Douglas Gregora735b202009-10-13 14:39:41 +00001051
Douglas Gregore53060f2009-06-25 22:08:12 +00001052 if (InitFunctionInstantiation(Function, D))
1053 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Douglas Gregore53060f2009-06-25 22:08:12 +00001055 bool Redeclaration = false;
John McCallaf2094e2010-04-08 09:05:18 +00001056 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001057
John McCall68263142009-11-18 22:49:29 +00001058 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1059 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1060
John McCallaf2094e2010-04-08 09:05:18 +00001061 if (DependentFunctionTemplateSpecializationInfo *Info
1062 = D->getDependentSpecializationInfo()) {
1063 assert(isFriend && "non-friend has dependent specialization info?");
1064
1065 // This needs to be set now for future sanity.
1066 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1067
1068 // Instantiate the explicit template arguments.
1069 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1070 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001071 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1072 ExplicitArgs, TemplateArgs))
1073 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001074
1075 // Map the candidate templates to their instantiations.
1076 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1077 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1078 Info->getTemplate(I),
1079 TemplateArgs);
1080 if (!Temp) return 0;
1081
1082 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1083 }
1084
1085 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1086 &ExplicitArgs,
1087 Previous))
1088 Function->setInvalidDecl();
1089
1090 isExplicitSpecialization = true;
1091
1092 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001093 // Look only into the namespace where the friend would be declared to
1094 // find a previous declaration. This is the innermost enclosing namespace,
1095 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001096 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001097
Douglas Gregora735b202009-10-13 14:39:41 +00001098 // In C++, the previous declaration we find might be a tag type
1099 // (class or enum). In this case, the new declaration will hide the
1100 // tag type. Note that this does does not apply if we're declaring a
1101 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001102 if (Previous.isSingleTagDecl())
1103 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001104 }
1105
John McCall9f54ad42009-12-10 09:41:52 +00001106 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Peter Collingbournec80e8112011-01-21 02:08:54 +00001107 isExplicitSpecialization, Redeclaration);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001108
John McCall76d32642010-04-24 01:30:58 +00001109 NamedDecl *PrincipalDecl = (TemplateParams
1110 ? cast<NamedDecl>(FunctionTemplate)
1111 : Function);
1112
Douglas Gregora735b202009-10-13 14:39:41 +00001113 // If the original function was part of a friend declaration,
1114 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001115 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001116 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001117 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001118 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001119 else
Douglas Gregora735b202009-10-13 14:39:41 +00001120 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001121
1122 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1123 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001124
Gabor Greif77535df2010-08-30 22:25:56 +00001125 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001126
Douglas Gregor238058c2010-05-18 05:45:02 +00001127 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1128 D->isThisDeclarationADefinition()) {
1129 // Check for a function body.
1130 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001131 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001132 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1133 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1134 << Function->getDeclName();
1135 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1136 Function->setInvalidDecl();
1137 }
1138 // Check for redefinitions due to other instantiations of this or
1139 // a similar friend function.
1140 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1141 REnd = Function->redecls_end();
1142 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001143 if (*R == Function)
1144 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001145 switch (R->getFriendObjectKind()) {
1146 case Decl::FOK_None:
1147 if (!queuedInstantiation && R->isUsed(false)) {
1148 if (MemberSpecializationInfo *MSInfo
1149 = Function->getMemberSpecializationInfo()) {
1150 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1151 SourceLocation Loc = R->getLocation(); // FIXME
1152 MSInfo->setPointOfInstantiation(Loc);
1153 SemaRef.PendingLocalImplicitInstantiations.push_back(
1154 std::make_pair(Function, Loc));
1155 queuedInstantiation = true;
1156 }
1157 }
1158 }
1159 break;
1160 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001161 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001162 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001163 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001164 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1165 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001166 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001167 Function->setInvalidDecl();
1168 break;
1169 }
1170 }
1171 }
1172 }
Douglas Gregora735b202009-10-13 14:39:41 +00001173 }
1174
John McCall76d32642010-04-24 01:30:58 +00001175 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1176 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1177 PrincipalDecl->setNonMemberOperator();
1178
Douglas Gregore53060f2009-06-25 22:08:12 +00001179 return Function;
1180}
1181
Douglas Gregord60e1052009-08-27 16:57:43 +00001182Decl *
1183TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1184 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001185 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1186 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001187 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001188 // We are creating a function template specialization from a function
1189 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001190 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001191 std::pair<const TemplateArgument *, unsigned> Innermost
1192 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001194 FunctionDecl *SpecFunc
1195 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1196 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Douglas Gregor6b906862009-08-21 00:16:32 +00001198 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001199 if (SpecFunc)
1200 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001201 }
1202
John McCallb0cb0222010-03-27 05:57:59 +00001203 bool isFriend;
1204 if (FunctionTemplate)
1205 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1206 else
1207 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1208
Douglas Gregor79c22782010-01-16 20:21:20 +00001209 bool MergeWithParentScope = (TemplateParams != 0) ||
1210 !(isa<Decl>(Owner) &&
1211 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001212 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001213
John McCall4eab39f2010-10-19 02:26:41 +00001214 // Instantiate enclosing template arguments for friends.
1215 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1216 unsigned NumTempParamLists = 0;
1217 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1218 TempParamLists.set_size(NumTempParamLists);
1219 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1220 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1221 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1222 if (!InstParams)
1223 return NULL;
1224 TempParamLists[I] = InstParams;
1225 }
1226 }
1227
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001228 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001229 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1230 TInfo = SubstFunctionType(D, Params);
1231 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001232 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001233 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001234
Abramo Bagnara723df242010-12-14 22:11:44 +00001235 // \brief If the type of this function, after ignoring parentheses,
1236 // is not *directly* a function type, then we're instantiating a function
1237 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001238 //
1239 // typedef int functype(int, int);
1240 // functype func;
1241 //
1242 // In this case, we'll just go instantiate the ParmVarDecls that we
1243 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001244 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001245 assert(!Params.size() && "Instantiating type could not yield parameters");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001246 llvm::SmallVector<QualType, 4> ParamTypes;
1247 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1248 D->getNumParams(), TemplateArgs, ParamTypes,
1249 &Params))
1250 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001251 }
1252
John McCallb0cb0222010-03-27 05:57:59 +00001253 NestedNameSpecifier *Qualifier = D->getQualifier();
1254 if (Qualifier) {
1255 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1256 D->getQualifierRange(),
1257 TemplateArgs);
1258 if (!Qualifier) return 0;
1259 }
1260
1261 DeclContext *DC = Owner;
1262 if (isFriend) {
1263 if (Qualifier) {
1264 CXXScopeSpec SS;
1265 SS.setScopeRep(Qualifier);
1266 SS.setRange(D->getQualifierRange());
1267 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001268
1269 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1270 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001271 } else {
1272 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1273 D->getDeclContext(),
1274 TemplateArgs);
1275 }
1276 if (!DC) return 0;
1277 }
1278
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001279 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001280 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001281 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Abramo Bagnara25777432010-08-11 22:01:17 +00001283 DeclarationNameInfo NameInfo
1284 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001285 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001286 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001287 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001288 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001289 Constructor->isInlineSpecified(),
1290 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001291 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001292 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001293 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001294 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001295 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001296 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001297 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001298 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001299 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001300 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001301 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001302 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1303 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001304 D->isStatic(),
1305 D->getStorageClassAsWritten(),
1306 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001307 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001308
John McCallb0cb0222010-03-27 05:57:59 +00001309 if (Qualifier)
1310 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001311
Douglas Gregord60e1052009-08-27 16:57:43 +00001312 if (TemplateParams) {
1313 // Our resulting instantiation is actually a function template, since we
1314 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001315 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001316 // template<typename T>
1317 // struct X {
1318 // template<typename U> void f(T, U);
1319 // };
1320 //
1321 // X<int> x;
1322 //
1323 // We are instantiating the member template "f" within X<int>, which means
1324 // substituting int for T, but leaving "f" as a member function template.
1325 // Build the function template itself.
1326 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1327 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001328 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001329 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001330 if (isFriend) {
1331 FunctionTemplate->setLexicalDeclContext(Owner);
1332 FunctionTemplate->setObjectOfFriendDecl(true);
1333 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001334 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001335 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001336 } else if (FunctionTemplate) {
1337 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001338 std::pair<const TemplateArgument *, unsigned> Innermost
1339 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001340 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001341 TemplateArgumentList::CreateCopy(SemaRef.Context,
1342 Innermost.first,
1343 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001344 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001345 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001346 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001347 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001348 }
1349
Mike Stump1eb44332009-09-09 15:08:12 +00001350 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001351 // out-of-line, the instantiation will have the same lexical
1352 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001353 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001354 if (NumTempParamLists)
1355 Method->setTemplateParameterListsInfo(SemaRef.Context,
1356 NumTempParamLists,
1357 TempParamLists.data());
1358
John McCallb0cb0222010-03-27 05:57:59 +00001359 Method->setLexicalDeclContext(Owner);
1360 Method->setObjectOfFriendDecl(true);
1361 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001362 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Douglas Gregor5545e162009-03-24 00:38:23 +00001364 // Attach the parameters
1365 for (unsigned P = 0; P < Params.size(); ++P)
1366 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001367 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001368
1369 if (InitMethodInstantiation(Method, D))
1370 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001371
Abramo Bagnara25777432010-08-11 22:01:17 +00001372 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1373 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
John McCallb0cb0222010-03-27 05:57:59 +00001375 if (!FunctionTemplate || TemplateParams || isFriend) {
1376 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Douglas Gregordec06662009-08-21 18:42:58 +00001378 // In C++, the previous declaration we find might be a tag type
1379 // (class or enum). In this case, the new declaration will hide the
1380 // tag type. Note that this does does not apply if we're declaring a
1381 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001382 if (Previous.isSingleTagDecl())
1383 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001384 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001385
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001386 bool Redeclaration = false;
Peter Collingbournec80e8112011-01-21 02:08:54 +00001387 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001388
Douglas Gregor4ba31362009-12-01 17:24:26 +00001389 if (D->isPure())
1390 SemaRef.CheckPureMethod(Method, SourceRange());
1391
John McCall46460a62010-01-20 21:53:11 +00001392 Method->setAccess(D->getAccess());
1393
Anders Carlsson9eefa222011-01-20 06:52:44 +00001394 SemaRef.CheckOverrideControl(Method);
1395
John McCallb0cb0222010-03-27 05:57:59 +00001396 if (FunctionTemplate) {
1397 // If there's a function template, let our caller handle it.
1398 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1399 // Don't hide a (potentially) valid declaration with an invalid one.
1400 } else {
1401 NamedDecl *DeclToAdd = (TemplateParams
1402 ? cast<NamedDecl>(FunctionTemplate)
1403 : Method);
1404 if (isFriend)
1405 Record->makeDeclVisibleInContext(DeclToAdd);
1406 else
1407 Owner->addDecl(DeclToAdd);
1408 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001409
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001410 return Method;
1411}
1412
Douglas Gregor615c5d42009-03-24 16:43:20 +00001413Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001414 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001415}
1416
Douglas Gregor03b2b072009-03-24 00:15:49 +00001417Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001418 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001419}
1420
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001421Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001422 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001423}
1424
Douglas Gregor6477b692009-03-25 15:04:13 +00001425ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001426 return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001427}
1428
John McCalle29ba202009-08-20 01:44:21 +00001429Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1430 TemplateTypeParmDecl *D) {
1431 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001432 const Type* T = D->getTypeForDecl();
1433 assert(T->isTemplateTypeParmType());
1434 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001435
John McCalle29ba202009-08-20 01:44:21 +00001436 TemplateTypeParmDecl *Inst =
1437 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001438 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001439 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001440 D->wasDeclaredWithTypename(),
1441 D->isParameterPack());
1442
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001443 if (D->hasDefaultArgument())
1444 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001445
Douglas Gregor550d9b22009-10-31 17:21:17 +00001446 // Introduce this template parameter's instantiation into the instantiation
1447 // scope.
1448 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1449
John McCalle29ba202009-08-20 01:44:21 +00001450 return Inst;
1451}
1452
Douglas Gregor33642df2009-10-23 23:25:44 +00001453Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1454 NonTypeTemplateParmDecl *D) {
1455 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001456 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1457 llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1458 llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1459 bool IsExpandedParameterPack = false;
1460 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001461 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001462 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001463
1464 if (D->isExpandedParameterPack()) {
1465 // The non-type template parameter pack is an already-expanded pack
1466 // expansion of types. Substitute into each of the expanded types.
1467 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1468 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1469 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1470 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1471 TemplateArgs,
1472 D->getLocation(),
1473 D->getDeclName());
1474 if (!NewDI)
1475 return 0;
1476
1477 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1478 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1479 D->getLocation());
1480 if (NewT.isNull())
1481 return 0;
1482 ExpandedParameterPackTypes.push_back(NewT);
1483 }
1484
1485 IsExpandedParameterPack = true;
1486 DI = D->getTypeSourceInfo();
1487 T = DI->getType();
1488 } else if (isa<PackExpansionTypeLoc>(TL)) {
1489 // The non-type template parameter pack's type is a pack expansion of types.
1490 // Determine whether we need to expand this parameter pack into separate
1491 // types.
1492 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1493 TypeLoc Pattern = Expansion.getPatternLoc();
1494 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1495 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1496
1497 // Determine whether the set of unexpanded parameter packs can and should
1498 // be expanded.
1499 bool Expand = true;
1500 bool RetainExpansion = false;
1501 llvm::Optional<unsigned> OrigNumExpansions
1502 = Expansion.getTypePtr()->getNumExpansions();
1503 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1504 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1505 Pattern.getSourceRange(),
1506 Unexpanded.data(),
1507 Unexpanded.size(),
1508 TemplateArgs,
1509 Expand, RetainExpansion,
1510 NumExpansions))
1511 return 0;
1512
1513 if (Expand) {
1514 for (unsigned I = 0; I != *NumExpansions; ++I) {
1515 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1516 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1517 D->getLocation(),
1518 D->getDeclName());
1519 if (!NewDI)
1520 return 0;
1521
1522 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1523 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1524 NewDI->getType(),
1525 D->getLocation());
1526 if (NewT.isNull())
1527 return 0;
1528 ExpandedParameterPackTypes.push_back(NewT);
1529 }
1530
1531 // Note that we have an expanded parameter pack. The "type" of this
1532 // expanded parameter pack is the original expansion type, but callers
1533 // will end up using the expanded parameter pack types for type-checking.
1534 IsExpandedParameterPack = true;
1535 DI = D->getTypeSourceInfo();
1536 T = DI->getType();
1537 } else {
1538 // We cannot fully expand the pack expansion now, so substitute into the
1539 // pattern and create a new pack expansion type.
1540 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1541 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1542 D->getLocation(),
1543 D->getDeclName());
1544 if (!NewPattern)
1545 return 0;
1546
1547 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1548 NumExpansions);
1549 if (!DI)
1550 return 0;
1551
1552 T = DI->getType();
1553 }
1554 } else {
1555 // Simple case: substitution into a parameter that is not a parameter pack.
1556 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1557 D->getLocation(), D->getDeclName());
1558 if (!DI)
1559 return 0;
1560
1561 // Check that this type is acceptable for a non-type template parameter.
1562 bool Invalid = false;
1563 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1564 D->getLocation());
1565 if (T.isNull()) {
1566 T = SemaRef.Context.IntTy;
1567 Invalid = true;
1568 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001569 }
1570
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001571 NonTypeTemplateParmDecl *Param;
1572 if (IsExpandedParameterPack)
1573 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1574 D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001575 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001576 D->getPosition(),
1577 D->getIdentifier(), T,
1578 DI,
1579 ExpandedParameterPackTypes.data(),
1580 ExpandedParameterPackTypes.size(),
1581 ExpandedParameterPackTypesAsWritten.data());
1582 else
1583 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1584 D->getLocation(),
1585 D->getDepth() - TemplateArgs.getNumLevels(),
1586 D->getPosition(),
1587 D->getIdentifier(), T,
1588 D->isParameterPack(), DI);
1589
Douglas Gregor33642df2009-10-23 23:25:44 +00001590 if (Invalid)
1591 Param->setInvalidDecl();
1592
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001593 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001594
1595 // Introduce this template parameter's instantiation into the instantiation
1596 // scope.
1597 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001598 return Param;
1599}
1600
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001601Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001602TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1603 TemplateTemplateParmDecl *D) {
1604 // Instantiate the template parameter list of the template template parameter.
1605 TemplateParameterList *TempParams = D->getTemplateParameters();
1606 TemplateParameterList *InstParams;
1607 {
1608 // Perform the actual substitution of template parameters within a new,
1609 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001610 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001611 InstParams = SubstTemplateParams(TempParams);
1612 if (!InstParams)
1613 return NULL;
1614 }
1615
1616 // Build the template template parameter.
1617 TemplateTemplateParmDecl *Param
1618 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001619 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001620 D->getPosition(), D->isParameterPack(),
1621 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001622 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001623
Douglas Gregor9106ef72009-11-11 16:58:32 +00001624 // Introduce this template parameter's instantiation into the instantiation
1625 // scope.
1626 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1627
1628 return Param;
1629}
1630
Douglas Gregor48c32a72009-11-17 06:07:40 +00001631Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1632 // Using directives are never dependent, so they require no explicit
1633
1634 UsingDirectiveDecl *Inst
1635 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1636 D->getNamespaceKeyLocation(),
1637 D->getQualifierRange(), D->getQualifier(),
1638 D->getIdentLocation(),
1639 D->getNominatedNamespace(),
1640 D->getCommonAncestor());
1641 Owner->addDecl(Inst);
1642 return Inst;
1643}
1644
John McCalled976492009-12-04 22:46:56 +00001645Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001646
1647 // The nested name specifier may be dependent, for example
1648 // template <typename T> struct t {
1649 // struct s1 { T f1(); };
1650 // struct s2 : s1 { using s1::f1; };
1651 // };
1652 // template struct t<int>;
1653 // Here, in using s1::f1, s1 refers to t<T>::s1;
1654 // we need to substitute for t<int>::s1.
1655 NestedNameSpecifier *NNS =
1656 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
1657 D->getNestedNameRange(),
1658 TemplateArgs);
1659 if (!NNS)
1660 return 0;
1661
1662 // The name info is non-dependent, so no transformation
1663 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001664 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001665
John McCall9f54ad42009-12-10 09:41:52 +00001666 // We only need to do redeclaration lookups if we're in a class
1667 // scope (in fact, it's not really even possible in non-class
1668 // scopes).
1669 bool CheckRedeclaration = Owner->isRecord();
1670
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001671 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1672 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001673
John McCalled976492009-12-04 22:46:56 +00001674 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001675 D->getNestedNameRange(),
1676 D->getUsingLocation(),
Douglas Gregor1b398202010-09-29 17:58:28 +00001677 NNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001678 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001679 D->isTypeName());
1680
1681 CXXScopeSpec SS;
Douglas Gregor1b398202010-09-29 17:58:28 +00001682 SS.setScopeRep(NNS);
John McCalled976492009-12-04 22:46:56 +00001683 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001684
1685 if (CheckRedeclaration) {
1686 Prev.setHideTags(false);
1687 SemaRef.LookupQualifiedName(Prev, Owner);
1688
1689 // Check for invalid redeclarations.
1690 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1691 D->isTypeName(), SS,
1692 D->getLocation(), Prev))
1693 NewUD->setInvalidDecl();
1694
1695 }
1696
1697 if (!NewUD->isInvalidDecl() &&
1698 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001699 D->getLocation()))
1700 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001701
John McCalled976492009-12-04 22:46:56 +00001702 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1703 NewUD->setAccess(D->getAccess());
1704 Owner->addDecl(NewUD);
1705
John McCall9f54ad42009-12-10 09:41:52 +00001706 // Don't process the shadow decls for an invalid decl.
1707 if (NewUD->isInvalidDecl())
1708 return NewUD;
1709
John McCall323c3102009-12-22 22:26:37 +00001710 bool isFunctionScope = Owner->isFunctionOrMethod();
1711
John McCall9f54ad42009-12-10 09:41:52 +00001712 // Process the shadow decls.
1713 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1714 I != E; ++I) {
1715 UsingShadowDecl *Shadow = *I;
1716 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001717 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1718 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001719 TemplateArgs));
1720
1721 if (CheckRedeclaration &&
1722 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1723 continue;
1724
1725 UsingShadowDecl *InstShadow
1726 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1727 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001728
1729 if (isFunctionScope)
1730 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001731 }
John McCalled976492009-12-04 22:46:56 +00001732
1733 return NewUD;
1734}
1735
1736Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001737 // Ignore these; we handle them in bulk when processing the UsingDecl.
1738 return 0;
John McCalled976492009-12-04 22:46:56 +00001739}
1740
John McCall7ba107a2009-11-18 02:36:19 +00001741Decl * TemplateDeclInstantiator
1742 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001743 NestedNameSpecifier *NNS =
1744 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1745 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001746 TemplateArgs);
1747 if (!NNS)
1748 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001750 CXXScopeSpec SS;
1751 SS.setRange(D->getTargetNestedNameRange());
1752 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001754 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1755 // Hence, no tranformation is required for it.
1756 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001757 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001758 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001759 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001760 /*instantiation*/ true,
1761 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001762 if (UD)
John McCalled976492009-12-04 22:46:56 +00001763 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1764
John McCall7ba107a2009-11-18 02:36:19 +00001765 return UD;
1766}
1767
1768Decl * TemplateDeclInstantiator
1769 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1770 NestedNameSpecifier *NNS =
1771 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1772 D->getTargetNestedNameRange(),
1773 TemplateArgs);
1774 if (!NNS)
1775 return 0;
1776
1777 CXXScopeSpec SS;
1778 SS.setRange(D->getTargetNestedNameRange());
1779 SS.setScopeRep(NNS);
1780
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001781 DeclarationNameInfo NameInfo
1782 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1783
John McCall7ba107a2009-11-18 02:36:19 +00001784 NamedDecl *UD =
1785 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001786 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001787 /*instantiation*/ true,
1788 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001789 if (UD)
John McCalled976492009-12-04 22:46:56 +00001790 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1791
Anders Carlsson0d8df782009-08-29 19:37:28 +00001792 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001793}
1794
John McCallce3ff2b2009-08-25 22:02:44 +00001795Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001796 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001797 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001798 if (D->isInvalidDecl())
1799 return 0;
1800
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001801 return Instantiator.Visit(D);
1802}
1803
John McCalle29ba202009-08-20 01:44:21 +00001804/// \brief Instantiates a nested template parameter list in the current
1805/// instantiation context.
1806///
1807/// \param L The parameter list to instantiate
1808///
1809/// \returns NULL if there was an error
1810TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001811TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001812 // Get errors for all the parameters before bailing out.
1813 bool Invalid = false;
1814
1815 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001816 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001817 ParamVector Params;
1818 Params.reserve(N);
1819 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1820 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001821 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001822 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001823 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001824 }
1825
1826 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001827 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001828 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001829
1830 TemplateParameterList *InstL
1831 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1832 L->getLAngleLoc(), &Params.front(), N,
1833 L->getRAngleLoc());
1834 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001835}
John McCalle29ba202009-08-20 01:44:21 +00001836
Douglas Gregored9c0f92009-10-29 00:04:11 +00001837/// \brief Instantiate the declaration of a class template partial
1838/// specialization.
1839///
1840/// \param ClassTemplate the (instantiated) class template that is partially
1841// specialized by the instantiation of \p PartialSpec.
1842///
1843/// \param PartialSpec the (uninstantiated) class template partial
1844/// specialization that we are instantiating.
1845///
Douglas Gregord65587f2010-11-10 19:44:59 +00001846/// \returns The instantiated partial specialization, if successful; otherwise,
1847/// NULL to indicate an error.
1848ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001849TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1850 ClassTemplateDecl *ClassTemplate,
1851 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001852 // Create a local instantiation scope for this class template partial
1853 // specialization, which will contain the instantiations of the template
1854 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001855 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001856
Douglas Gregored9c0f92009-10-29 00:04:11 +00001857 // Substitute into the template parameters of the class template partial
1858 // specialization.
1859 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1860 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1861 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001862 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001863
1864 // Substitute into the template arguments of the class template partial
1865 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00001866 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
Douglas Gregore02e2622010-12-22 21:19:48 +00001867 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1868 PartialSpec->getNumTemplateArgsAsWritten(),
1869 InstTemplateArgs, TemplateArgs))
1870 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001871
Douglas Gregored9c0f92009-10-29 00:04:11 +00001872 // Check that the template argument list is well-formed for this
1873 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001874 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001875 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1876 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001877 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001878 false,
1879 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001880 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001881
1882 // Figure out where to insert this class template partial specialization
1883 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001884 void *InsertPos = 0;
1885 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001886 = ClassTemplate->findPartialSpecialization(Converted.data(),
1887 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001888
1889 // Build the canonical type that describes the converted template
1890 // arguments of the class template partial specialization.
1891 QualType CanonType
1892 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001893 Converted.data(),
1894 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001895
1896 // Build the fully-sugared type for this class template
1897 // specialization as the user wrote in the specialization
1898 // itself. This means that we'll pretty-print the type retrieved
1899 // from the specialization's declaration the way that the user
1900 // actually wrote the specialization, rather than formatting the
1901 // name based on the "canonical" representation used to store the
1902 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001903 TypeSourceInfo *WrittenTy
1904 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1905 TemplateName(ClassTemplate),
1906 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001907 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001908 CanonType);
1909
1910 if (PrevDecl) {
1911 // We've already seen a partial specialization with the same template
1912 // parameters and template arguments. This can happen, for example, when
1913 // substituting the outer template arguments ends up causing two
1914 // class template partial specializations of a member class template
1915 // to have identical forms, e.g.,
1916 //
1917 // template<typename T, typename U>
1918 // struct Outer {
1919 // template<typename X, typename Y> struct Inner;
1920 // template<typename Y> struct Inner<T, Y>;
1921 // template<typename Y> struct Inner<U, Y>;
1922 // };
1923 //
1924 // Outer<int, int> outer; // error: the partial specializations of Inner
1925 // // have the same signature.
1926 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001927 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001928 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1929 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001930 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001931 }
1932
1933
1934 // Create the class template partial specialization declaration.
1935 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001936 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1937 PartialSpec->getTagKind(),
1938 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001939 PartialSpec->getLocation(),
1940 InstParams,
1941 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001942 Converted.data(),
1943 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001944 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001945 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001946 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001947 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001948 // Substitute the nested name specifier, if any.
1949 if (SubstQualifier(PartialSpec, InstPartialSpec))
1950 return 0;
1951
Douglas Gregored9c0f92009-10-29 00:04:11 +00001952 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001953 InstPartialSpec->setTypeAsWritten(WrittenTy);
1954
Douglas Gregored9c0f92009-10-29 00:04:11 +00001955 // Add this partial specialization to the set of class template partial
1956 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001957 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001958 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001959}
1960
John McCall21ef0fa2010-03-11 09:03:00 +00001961TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001962TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001963 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001964 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1965 assert(OldTInfo && "substituting function without type source info");
1966 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001967 TypeSourceInfo *NewTInfo
1968 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1969 D->getTypeSpecStartLoc(),
1970 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001971 if (!NewTInfo)
1972 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001973
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001974 if (NewTInfo != OldTInfo) {
1975 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001976 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001977 if (FunctionProtoTypeLoc *OldProtoLoc
1978 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001979 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001980 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1981 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001982 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
1983 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
1984 OldIdx != NumOldParams; ++OldIdx) {
1985 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
1986 if (!OldParam->isParameterPack() ||
1987 (NewIdx < NumNewParams &&
1988 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
1989 // Simple case: normal parameter, or a parameter pack that's
1990 // instantiated to a (still-dependent) parameter pack.
1991 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
1992 Params.push_back(NewParam);
1993 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
1994 NewParam);
1995 continue;
1996 }
1997
1998 // Parameter pack: make the instantiation an argument pack.
1999 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2000 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002001 unsigned NumArgumentsInExpansion
2002 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2003 TemplateArgs);
2004 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002005 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2006 Params.push_back(NewParam);
2007 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2008 NewParam);
2009 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002010 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002011 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002012 } else {
2013 // The function type itself was not dependent and therefore no
2014 // substitution occurred. However, we still need to instantiate
2015 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002016 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002017 if (FunctionProtoTypeLoc *OldProtoLoc
2018 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2019 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2020 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2021 if (!Parm)
2022 return 0;
2023 Params.push_back(Parm);
2024 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002025 }
2026 }
John McCall21ef0fa2010-03-11 09:03:00 +00002027 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002028}
2029
Mike Stump1eb44332009-09-09 15:08:12 +00002030/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002031/// declaration (New) from the corresponding fields of its template (Tmpl).
2032///
2033/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002034bool
2035TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002036 FunctionDecl *Tmpl) {
2037 if (Tmpl->isDeleted())
2038 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Douglas Gregorcca9e962009-07-01 22:01:06 +00002040 // If we are performing substituting explicitly-specified template arguments
2041 // or deduced template arguments into a function template and we reach this
2042 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002043 // to keeping the new function template specialization. We therefore
2044 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002045 // into a template instantiation for this specific function template
2046 // specialization, which is not a SFINAE context, so that we diagnose any
2047 // further errors in the declaration itself.
2048 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2049 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2050 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2051 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002052 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002053 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002054 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002055 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002056 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002057 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2058 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002059 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002060 }
2061 }
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002063 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2064 assert(Proto && "Function template without prototype?");
2065
2066 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2067 Proto->getNoReturnAttr()) {
2068 // The function has an exception specification or a "noreturn"
2069 // attribute. Substitute into each of the exception types.
2070 llvm::SmallVector<QualType, 4> Exceptions;
2071 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2072 // FIXME: Poor location information!
Douglas Gregorb99268b2010-12-21 00:52:54 +00002073 if (const PackExpansionType *PackExpansion
2074 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2075 // We have a pack expansion. Instantiate it.
2076 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2077 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2078 Unexpanded);
2079 assert(!Unexpanded.empty() &&
2080 "Pack expansion without parameter packs?");
2081
2082 bool Expand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002083 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002084 llvm::Optional<unsigned> NumExpansions
2085 = PackExpansion->getNumExpansions();
Douglas Gregorb99268b2010-12-21 00:52:54 +00002086 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2087 SourceRange(),
2088 Unexpanded.data(),
2089 Unexpanded.size(),
2090 TemplateArgs,
Douglas Gregord3731192011-01-10 07:32:04 +00002091 Expand,
2092 RetainExpansion,
2093 NumExpansions))
Douglas Gregorb99268b2010-12-21 00:52:54 +00002094 break;
2095
2096 if (!Expand) {
2097 // We can't expand this pack expansion into separate arguments yet;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002098 // just substitute into the pattern and create a new pack expansion
2099 // type.
Douglas Gregorb99268b2010-12-21 00:52:54 +00002100 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2101 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2102 TemplateArgs,
2103 New->getLocation(), New->getDeclName());
2104 if (T.isNull())
2105 break;
2106
Douglas Gregorcded4f62011-01-14 17:04:44 +00002107 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
Douglas Gregorb99268b2010-12-21 00:52:54 +00002108 Exceptions.push_back(T);
2109 continue;
2110 }
2111
2112 // Substitute into the pack expansion pattern for each template
2113 bool Invalid = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002114 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
Douglas Gregorb99268b2010-12-21 00:52:54 +00002115 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2116
2117 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2118 TemplateArgs,
2119 New->getLocation(), New->getDeclName());
2120 if (T.isNull()) {
2121 Invalid = true;
2122 break;
2123 }
2124
2125 Exceptions.push_back(T);
2126 }
2127
2128 if (Invalid)
2129 break;
2130
2131 continue;
2132 }
2133
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002134 QualType T
2135 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2136 New->getLocation(), New->getDeclName());
2137 if (T.isNull() ||
2138 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2139 continue;
2140
2141 Exceptions.push_back(T);
2142 }
2143
2144 // Rebuild the function type
2145
John McCalle23cf432010-12-14 08:05:40 +00002146 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2147 EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2148 EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2149 EPI.NumExceptions = Exceptions.size();
2150 EPI.Exceptions = Exceptions.data();
2151 EPI.ExtInfo = Proto->getExtInfo();
2152
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002153 const FunctionProtoType *NewProto
2154 = New->getType()->getAs<FunctionProtoType>();
2155 assert(NewProto && "Template instantiation without function prototype?");
2156 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2157 NewProto->arg_type_begin(),
2158 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +00002159 EPI));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002160 }
2161
John McCall1d8d1cc2010-08-01 02:01:53 +00002162 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002163
Douglas Gregore53060f2009-06-25 22:08:12 +00002164 return false;
2165}
2166
Douglas Gregor5545e162009-03-24 00:38:23 +00002167/// \brief Initializes common fields of an instantiated method
2168/// declaration (New) from the corresponding fields of its template
2169/// (Tmpl).
2170///
2171/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002172bool
2173TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002174 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002175 if (InitFunctionInstantiation(New, Tmpl))
2176 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002177
Douglas Gregor5545e162009-03-24 00:38:23 +00002178 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002179 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002180 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002181
2182 // FIXME: attributes
2183 // FIXME: New needs a pointer to Tmpl
2184 return false;
2185}
Douglas Gregora58861f2009-05-13 20:28:22 +00002186
2187/// \brief Instantiate the definition of the given function from its
2188/// template.
2189///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002190/// \param PointOfInstantiation the point at which the instantiation was
2191/// required. Note that this is not precisely a "point of instantiation"
2192/// for the function, but it's close.
2193///
Douglas Gregora58861f2009-05-13 20:28:22 +00002194/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002195/// function template specialization or member function of a class template
2196/// specialization.
2197///
2198/// \param Recursive if true, recursively instantiates any functions that
2199/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002200///
2201/// \param DefinitionRequired if true, then we are performing an explicit
2202/// instantiation where the body of the function is required. Complain if
2203/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002204void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002205 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002206 bool Recursive,
2207 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002208 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002209 return;
2210
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002211 // Never instantiate an explicit specialization.
2212 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2213 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002214
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002215 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002216 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002217 Stmt *Pattern = 0;
2218 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002219 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002220
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002221 if (!Pattern) {
2222 if (DefinitionRequired) {
2223 if (Function->getPrimaryTemplate())
2224 Diag(PointOfInstantiation,
2225 diag::err_explicit_instantiation_undefined_func_template)
2226 << Function->getPrimaryTemplate();
2227 else
2228 Diag(PointOfInstantiation,
2229 diag::err_explicit_instantiation_undefined_member)
2230 << 1 << Function->getDeclName() << Function->getDeclContext();
2231
2232 if (PatternDecl)
2233 Diag(PatternDecl->getLocation(),
2234 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002235 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002236 } else if (Function->getTemplateSpecializationKind()
2237 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002238 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002239 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002240 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002241
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002242 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002243 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002244
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002245 // C++0x [temp.explicit]p9:
2246 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002247 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002248 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002249 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002250 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002251 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002252 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002254 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2255 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002256 return;
2257
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002258 // If we're performing recursive template instantiation, create our own
2259 // queue of pending implicit instantiations that we will instantiate later,
2260 // while we're still within our own instantiation context.
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002261 llvm::SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002262 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002263 if (Recursive) {
2264 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002265 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002266 }
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Douglas Gregor9679caf2010-05-12 17:27:19 +00002268 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002269 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002270 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002271
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002272 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002273 // recorded, unless we're actually a member function within a local
2274 // class, in which case we need to merge our results with the parent
2275 // scope (of the enclosing function).
2276 bool MergeWithParentScope = false;
2277 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2278 MergeWithParentScope = Rec->isLocalClass();
2279
2280 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002282 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002283 // instantiation scope, and set the parameter names to those used
2284 // in the template.
Douglas Gregor12c9c002011-01-07 16:43:16 +00002285 unsigned FParamIdx = 0;
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002286 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2287 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002288 if (!PatternParam->isParameterPack()) {
2289 // Simple case: not a parameter pack.
2290 assert(FParamIdx < Function->getNumParams());
2291 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2292 FunctionParam->setDeclName(PatternParam->getDeclName());
2293 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2294 ++FParamIdx;
2295 continue;
2296 }
2297
2298 // Expand the parameter pack.
2299 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2300 for (unsigned NumFParams = Function->getNumParams();
2301 FParamIdx < NumFParams;
2302 ++FParamIdx) {
2303 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2304 FunctionParam->setDeclName(PatternParam->getDeclName());
2305 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2306 }
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002307 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002308
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002309 // Enter the scope of this instantiation. We don't use
2310 // PushDeclContext because we don't have a scope.
2311 DeclContext *PreviousContext = CurContext;
2312 CurContext = Function;
2313
Mike Stump1eb44332009-09-09 15:08:12 +00002314 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002315 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002316
2317 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002318 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002319 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2320 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2321 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002322 }
2323
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002324 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002325 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002326
Douglas Gregor52604ab2009-09-11 21:19:12 +00002327 if (Body.isInvalid())
2328 Function->setInvalidDecl();
2329
John McCall9ae2f072010-08-23 23:25:46 +00002330 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002331 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002332
John McCall0c01d182010-03-24 05:22:00 +00002333 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2334
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002335 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002336
2337 DeclGroupRef DG(Function);
2338 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002339
Douglas Gregor60406be2010-01-16 22:29:39 +00002340 // This class may have local implicit instantiations that need to be
2341 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002342 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002343 Scope.Exit();
2344
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002345 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002346 // Define any pending vtables.
2347 DefineUsedVTables();
2348
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002349 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002350 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002351 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002353 // Restore the set of pending vtables.
2354 VTableUses.swap(SavedVTableUses);
2355
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002356 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002357 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002358 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002359}
2360
2361/// \brief Instantiate the definition of the given variable from its
2362/// template.
2363///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002364/// \param PointOfInstantiation the point at which the instantiation was
2365/// required. Note that this is not precisely a "point of instantiation"
2366/// for the function, but it's close.
2367///
2368/// \param Var the already-instantiated declaration of a static member
2369/// variable of a class template specialization.
2370///
2371/// \param Recursive if true, recursively instantiates any functions that
2372/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002373///
2374/// \param DefinitionRequired if true, then we are performing an explicit
2375/// instantiation where an out-of-line definition of the member variable
2376/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002377void Sema::InstantiateStaticDataMemberDefinition(
2378 SourceLocation PointOfInstantiation,
2379 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002380 bool Recursive,
2381 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002382 if (Var->isInvalidDecl())
2383 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002384
Douglas Gregor7caa6822009-07-24 20:34:43 +00002385 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002386 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002387 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002388 assert(Def->isStaticDataMember() && "Not a static data member?");
2389 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Douglas Gregor0d035142009-10-27 18:42:08 +00002391 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002392 // We did not find an out-of-line definition of this static data member,
2393 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002394 // instantiate this definition (or provide a specialization for it) in
2395 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002396 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002397 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002398 Diag(PointOfInstantiation,
2399 diag::err_explicit_instantiation_undefined_member)
2400 << 2 << Var->getDeclName() << Var->getDeclContext();
2401 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002402 } else if (Var->getTemplateSpecializationKind()
2403 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002404 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002405 std::make_pair(Var, PointOfInstantiation));
2406 }
2407
Douglas Gregor7caa6822009-07-24 20:34:43 +00002408 return;
2409 }
2410
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002411 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002412 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002413 return;
2414
2415 // C++0x [temp.explicit]p9:
2416 // Except for inline functions, other explicit instantiation declarations
2417 // have the effect of suppressing the implicit instantiation of the entity
2418 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002419 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002420 == TSK_ExplicitInstantiationDeclaration)
2421 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Douglas Gregor7caa6822009-07-24 20:34:43 +00002423 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2424 if (Inst)
2425 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Douglas Gregor7caa6822009-07-24 20:34:43 +00002427 // If we're performing recursive template instantiation, create our own
2428 // queue of pending implicit instantiations that we will instantiate later,
2429 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002430 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002431 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002432 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Douglas Gregor7caa6822009-07-24 20:34:43 +00002434 // Enter the scope of this instantiation. We don't use
2435 // PushDeclContext because we don't have a scope.
2436 DeclContext *PreviousContext = CurContext;
2437 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002439 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002440 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002441 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002442 CurContext = PreviousContext;
2443
2444 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002445 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2446 assert(MSInfo && "Missing member specialization information?");
2447 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2448 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002449 DeclGroupRef DG(Var);
2450 Consumer.HandleTopLevelDecl(DG);
2451 }
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Douglas Gregor7caa6822009-07-24 20:34:43 +00002453 if (Recursive) {
2454 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002455 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002456 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002457
Douglas Gregor7caa6822009-07-24 20:34:43 +00002458 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002459 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002460 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002461}
Douglas Gregor815215d2009-05-27 05:35:12 +00002462
Anders Carlsson09025312009-08-29 05:16:22 +00002463void
2464Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2465 const CXXConstructorDecl *Tmpl,
2466 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Anders Carlsson09025312009-08-29 05:16:22 +00002468 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002469 bool AnyErrors = false;
2470
Anders Carlsson09025312009-08-29 05:16:22 +00002471 // Instantiate all the initializers.
2472 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002473 InitsEnd = Tmpl->init_end();
2474 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002475 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002476
Chandler Carruth030ef472010-09-03 21:54:20 +00002477 // Only instantiate written initializers, let Sema re-construct implicit
2478 // ones.
2479 if (!Init->isWritten())
2480 continue;
2481
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002482 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002483 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002485 SourceLocation EllipsisLoc;
2486
2487 if (Init->isPackExpansion()) {
2488 // This is a pack expansion. We should expand it now.
2489 TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
2490 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2491 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2492 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002493 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002494 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002495 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
2496 BaseTL.getSourceRange(),
2497 Unexpanded.data(),
2498 Unexpanded.size(),
2499 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002500 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002501 NumExpansions)) {
2502 AnyErrors = true;
2503 New->setInvalidDecl();
2504 continue;
2505 }
2506 assert(ShouldExpand && "Partial instantiation of base initializer?");
2507
2508 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002509 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002510 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2511
2512 // Instantiate the initializer.
2513 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2514 LParenLoc, NewArgs, RParenLoc)) {
2515 AnyErrors = true;
2516 break;
2517 }
2518
2519 // Instantiate the base type.
2520 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2521 TemplateArgs,
2522 Init->getSourceLocation(),
2523 New->getDeclName());
2524 if (!BaseTInfo) {
2525 AnyErrors = true;
2526 break;
2527 }
2528
2529 // Build the initializer.
2530 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2531 BaseTInfo,
2532 (Expr **)NewArgs.data(),
2533 NewArgs.size(),
2534 Init->getLParenLoc(),
2535 Init->getRParenLoc(),
2536 New->getParent(),
2537 SourceLocation());
2538 if (NewInit.isInvalid()) {
2539 AnyErrors = true;
2540 break;
2541 }
2542
2543 NewInits.push_back(NewInit.get());
2544 NewArgs.clear();
2545 }
2546
2547 continue;
2548 }
2549
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002550 // Instantiate the initializer.
2551 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002552 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002553 AnyErrors = true;
2554 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002555 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002556
Anders Carlsson09025312009-08-29 05:16:22 +00002557 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002558 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002559 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002560 TemplateArgs,
2561 Init->getSourceLocation(),
2562 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002563 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002564 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002565 New->setInvalidDecl();
2566 continue;
2567 }
2568
John McCalla93c9342009-12-07 02:54:59 +00002569 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002570 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002571 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002572 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002573 Init->getRParenLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002574 New->getParent(),
2575 EllipsisLoc);
Anders Carlsson09025312009-08-29 05:16:22 +00002576 } else if (Init->isMemberInitializer()) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00002577 FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
2578 Init->getMemberLocation(),
2579 Init->getMember(),
2580 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002581
2582 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002583 NewArgs.size(),
2584 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002585 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002586 Init->getRParenLoc());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002587 } else if (Init->isIndirectMemberInitializer()) {
2588 IndirectFieldDecl *IndirectMember =
2589 cast<IndirectFieldDecl>(FindInstantiatedDecl(
2590 Init->getMemberLocation(),
2591 Init->getIndirectMember(), TemplateArgs));
2592
2593 NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2594 NewArgs.size(),
2595 Init->getSourceLocation(),
2596 Init->getLParenLoc(),
2597 Init->getRParenLoc());
Anders Carlsson09025312009-08-29 05:16:22 +00002598 }
2599
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002600 if (NewInit.isInvalid()) {
2601 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002602 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002603 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002604 // FIXME: It would be nice if ASTOwningVector had a release function.
2605 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Anders Carlsson09025312009-08-29 05:16:22 +00002607 NewInits.push_back((MemInitTy *)NewInit.get());
2608 }
2609 }
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Anders Carlsson09025312009-08-29 05:16:22 +00002611 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002612 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002613 /*FIXME: ColonLoc */
2614 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002615 NewInits.data(), NewInits.size(),
2616 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002617}
2618
John McCall52a575a2009-08-29 08:11:13 +00002619// TODO: this could be templated if the various decl types used the
2620// same method name.
2621static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2622 ClassTemplateDecl *Instance) {
2623 Pattern = Pattern->getCanonicalDecl();
2624
2625 do {
2626 Instance = Instance->getCanonicalDecl();
2627 if (Pattern == Instance) return true;
2628 Instance = Instance->getInstantiatedFromMemberTemplate();
2629 } while (Instance);
2630
2631 return false;
2632}
2633
Douglas Gregor0d696532009-09-28 06:34:35 +00002634static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2635 FunctionTemplateDecl *Instance) {
2636 Pattern = Pattern->getCanonicalDecl();
2637
2638 do {
2639 Instance = Instance->getCanonicalDecl();
2640 if (Pattern == Instance) return true;
2641 Instance = Instance->getInstantiatedFromMemberTemplate();
2642 } while (Instance);
2643
2644 return false;
2645}
2646
Douglas Gregored9c0f92009-10-29 00:04:11 +00002647static bool
2648isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2649 ClassTemplatePartialSpecializationDecl *Instance) {
2650 Pattern
2651 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2652 do {
2653 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2654 Instance->getCanonicalDecl());
2655 if (Pattern == Instance)
2656 return true;
2657 Instance = Instance->getInstantiatedFromMember();
2658 } while (Instance);
2659
2660 return false;
2661}
2662
John McCall52a575a2009-08-29 08:11:13 +00002663static bool isInstantiationOf(CXXRecordDecl *Pattern,
2664 CXXRecordDecl *Instance) {
2665 Pattern = Pattern->getCanonicalDecl();
2666
2667 do {
2668 Instance = Instance->getCanonicalDecl();
2669 if (Pattern == Instance) return true;
2670 Instance = Instance->getInstantiatedFromMemberClass();
2671 } while (Instance);
2672
2673 return false;
2674}
2675
2676static bool isInstantiationOf(FunctionDecl *Pattern,
2677 FunctionDecl *Instance) {
2678 Pattern = Pattern->getCanonicalDecl();
2679
2680 do {
2681 Instance = Instance->getCanonicalDecl();
2682 if (Pattern == Instance) return true;
2683 Instance = Instance->getInstantiatedFromMemberFunction();
2684 } while (Instance);
2685
2686 return false;
2687}
2688
2689static bool isInstantiationOf(EnumDecl *Pattern,
2690 EnumDecl *Instance) {
2691 Pattern = Pattern->getCanonicalDecl();
2692
2693 do {
2694 Instance = Instance->getCanonicalDecl();
2695 if (Pattern == Instance) return true;
2696 Instance = Instance->getInstantiatedFromMemberEnum();
2697 } while (Instance);
2698
2699 return false;
2700}
2701
John McCalled976492009-12-04 22:46:56 +00002702static bool isInstantiationOf(UsingShadowDecl *Pattern,
2703 UsingShadowDecl *Instance,
2704 ASTContext &C) {
2705 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2706}
2707
2708static bool isInstantiationOf(UsingDecl *Pattern,
2709 UsingDecl *Instance,
2710 ASTContext &C) {
2711 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2712}
2713
John McCall7ba107a2009-11-18 02:36:19 +00002714static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2715 UsingDecl *Instance,
2716 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002717 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002718}
2719
2720static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002721 UsingDecl *Instance,
2722 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002723 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002724}
2725
John McCall52a575a2009-08-29 08:11:13 +00002726static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2727 VarDecl *Instance) {
2728 assert(Instance->isStaticDataMember());
2729
2730 Pattern = Pattern->getCanonicalDecl();
2731
2732 do {
2733 Instance = Instance->getCanonicalDecl();
2734 if (Pattern == Instance) return true;
2735 Instance = Instance->getInstantiatedFromStaticDataMember();
2736 } while (Instance);
2737
2738 return false;
2739}
2740
John McCalled976492009-12-04 22:46:56 +00002741// Other is the prospective instantiation
2742// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002743static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002744 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002745 if (UnresolvedUsingTypenameDecl *UUD
2746 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2747 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2748 return isInstantiationOf(UUD, UD, Ctx);
2749 }
2750 }
2751
2752 if (UnresolvedUsingValueDecl *UUD
2753 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002754 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2755 return isInstantiationOf(UUD, UD, Ctx);
2756 }
2757 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002758
Anders Carlsson0d8df782009-08-29 19:37:28 +00002759 return false;
2760 }
Mike Stump1eb44332009-09-09 15:08:12 +00002761
John McCall52a575a2009-08-29 08:11:13 +00002762 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2763 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002764
John McCall52a575a2009-08-29 08:11:13 +00002765 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2766 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002767
John McCall52a575a2009-08-29 08:11:13 +00002768 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2769 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002770
Douglas Gregor7caa6822009-07-24 20:34:43 +00002771 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002772 if (Var->isStaticDataMember())
2773 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2774
2775 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2776 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002777
Douglas Gregor0d696532009-09-28 06:34:35 +00002778 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2779 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2780
Douglas Gregored9c0f92009-10-29 00:04:11 +00002781 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2782 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2783 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2784 PartialSpec);
2785
Anders Carlssond8b285f2009-09-01 04:26:58 +00002786 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2787 if (!Field->getDeclName()) {
2788 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002789 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002790 cast<FieldDecl>(D);
2791 }
2792 }
Mike Stump1eb44332009-09-09 15:08:12 +00002793
John McCalled976492009-12-04 22:46:56 +00002794 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2795 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2796
2797 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2798 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2799
Douglas Gregor815215d2009-05-27 05:35:12 +00002800 return D->getDeclName() && isa<NamedDecl>(Other) &&
2801 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2802}
2803
2804template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002805static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002806 NamedDecl *D,
2807 ForwardIterator first,
2808 ForwardIterator last) {
2809 for (; first != last; ++first)
2810 if (isInstantiationOf(Ctx, D, *first))
2811 return cast<NamedDecl>(*first);
2812
2813 return 0;
2814}
2815
John McCall02cace72009-08-28 07:59:38 +00002816/// \brief Finds the instantiation of the given declaration context
2817/// within the current instantiation.
2818///
2819/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002820DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002821 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002822 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002823 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002824 return cast_or_null<DeclContext>(ID);
2825 } else return DC;
2826}
2827
Douglas Gregored961e72009-05-27 17:54:46 +00002828/// \brief Find the instantiation of the given declaration within the
2829/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002830///
2831/// This routine is intended to be used when \p D is a declaration
2832/// referenced from within a template, that needs to mapped into the
2833/// corresponding declaration within an instantiation. For example,
2834/// given:
2835///
2836/// \code
2837/// template<typename T>
2838/// struct X {
2839/// enum Kind {
2840/// KnownValue = sizeof(T)
2841/// };
2842///
2843/// bool getKind() const { return KnownValue; }
2844/// };
2845///
2846/// template struct X<int>;
2847/// \endcode
2848///
2849/// In the instantiation of X<int>::getKind(), we need to map the
2850/// EnumConstantDecl for KnownValue (which refers to
2851/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002852/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2853/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002854NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002855 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002856 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002857 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002858 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002859 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002860 // D is a local of some kind. Look into the map of local
2861 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002862 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002863 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002864
Douglas Gregore95b4092009-09-16 18:34:49 +00002865 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2866 if (!Record->isDependentContext())
2867 return D;
2868
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002869 // If the RecordDecl is actually the injected-class-name or a
2870 // "templated" declaration for a class template, class template
2871 // partial specialization, or a member class of a class template,
2872 // substitute into the injected-class-name of the class template
2873 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002874 QualType T;
2875 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2876
2877 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002878 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002879 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2880 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002881 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002882
2883 // If we call SubstType with an InjectedClassNameType here we
2884 // can end up in an infinite loop.
2885 T = Context.getTypeDeclType(Record);
2886 assert(isa<InjectedClassNameType>(T) &&
2887 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002888 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002889 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002890
2891 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002892 // Substitute into the injected-class-name to get the type
2893 // corresponding to the instantiation we want, which may also be
2894 // the current instantiation (if we're in a template
2895 // definition). This substitution should never fail, since we
2896 // know we can instantiate the injected-class-name or we
2897 // wouldn't have gotten to the injected-class-name!
2898
2899 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2900 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002901 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2902 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2903
2904 if (!T->isDependentType()) {
2905 assert(T->isRecordType() && "Instantiation must produce a record type");
2906 return T->getAs<RecordType>()->getDecl();
2907 }
2908
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002909 // We are performing "partial" template instantiation to create
2910 // the member declarations for the members of a class template
2911 // specialization. Therefore, D is actually referring to something
2912 // in the current instantiation. Look through the current
2913 // context, which contains actual instantiations, to find the
2914 // instantiation of the "current instantiation" that D refers
2915 // to.
2916 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002917 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002918 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002919 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002920 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002921 if (isInstantiationOf(ClassTemplate,
2922 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002923 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002924
2925 if (!DC->isDependentContext())
2926 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002927 }
2928
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002929 // We're performing "instantiation" of a member of the current
2930 // instantiation while we are type-checking the
2931 // definition. Compute the declaration context and return that.
2932 assert(!SawNonDependentContext &&
2933 "No dependent context while instantiating record");
2934 DeclContext *DC = computeDeclContext(T);
2935 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002936 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002937 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002938 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002939
Douglas Gregore95b4092009-09-16 18:34:49 +00002940 // Fall through to deal with other dependent record types (e.g.,
2941 // anonymous unions in class templates).
2942 }
John McCall52a575a2009-08-29 08:11:13 +00002943
Douglas Gregore95b4092009-09-16 18:34:49 +00002944 if (!ParentDC->isDependentContext())
2945 return D;
2946
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002947 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002948 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002949 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002950
Douglas Gregor815215d2009-05-27 05:35:12 +00002951 if (ParentDC != D->getDeclContext()) {
2952 // We performed some kind of instantiation in the parent context,
2953 // so now we need to look into the instantiated parent context to
2954 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002955
John McCall3cb0ebd2010-03-10 03:28:59 +00002956 // If our context used to be dependent, we may need to instantiate
2957 // it before performing lookup into that context.
2958 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002959 if (!Spec->isDependentContext()) {
2960 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002961 const RecordType *Tag = T->getAs<RecordType>();
2962 assert(Tag && "type of non-dependent record is not a RecordType");
2963 if (!Tag->isBeingDefined() &&
2964 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2965 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002966
2967 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002968 }
2969 }
2970
Douglas Gregor815215d2009-05-27 05:35:12 +00002971 NamedDecl *Result = 0;
2972 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002973 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002974 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2975 } else {
2976 // Since we don't have a name for the entity we're looking for,
2977 // our only option is to walk through all of the declarations to
2978 // find that name. This will occur in a few cases:
2979 //
2980 // - anonymous struct/union within a template
2981 // - unnamed class/struct/union/enum within a template
2982 //
2983 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002984 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002985 ParentDC->decls_begin(),
2986 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002987 }
Mike Stump1eb44332009-09-09 15:08:12 +00002988
John McCall9f54ad42009-12-10 09:41:52 +00002989 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002990 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2991 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002992 && "Unable to find instantiation of declaration!");
2993
Douglas Gregor815215d2009-05-27 05:35:12 +00002994 D = Result;
2995 }
2996
Douglas Gregor815215d2009-05-27 05:35:12 +00002997 return D;
2998}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002999
Mike Stump1eb44332009-09-09 15:08:12 +00003000/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003001/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003002void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003003 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003004 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003005 PendingImplicitInstantiation Inst;
3006
3007 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003008 Inst = PendingInstantiations.front();
3009 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003010 } else {
3011 Inst = PendingLocalImplicitInstantiations.front();
3012 PendingLocalImplicitInstantiations.pop_front();
3013 }
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Douglas Gregor7caa6822009-07-24 20:34:43 +00003015 // Instantiate function definitions
3016 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003017 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3018 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003019 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3020 TSK_ExplicitInstantiationDefinition;
3021 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3022 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003023 continue;
3024 }
Mike Stump1eb44332009-09-09 15:08:12 +00003025
Douglas Gregor7caa6822009-07-24 20:34:43 +00003026 // Instantiate static data member definitions.
3027 VarDecl *Var = cast<VarDecl>(Inst.first);
3028 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003029
Chandler Carruth291b4412010-02-13 10:17:50 +00003030 // Don't try to instantiate declarations if the most recent redeclaration
3031 // is invalid.
3032 if (Var->getMostRecentDeclaration()->isInvalidDecl())
3033 continue;
3034
3035 // Check if the most recent declaration has changed the specialization kind
3036 // and removed the need for implicit instantiation.
3037 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3038 case TSK_Undeclared:
3039 assert(false && "Cannot instantitiate an undeclared specialization.");
3040 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003041 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003042 continue; // No longer need to instantiate this type.
3043 case TSK_ExplicitInstantiationDefinition:
3044 // We only need an instantiation if the pending instantiation *is* the
3045 // explicit instantiation.
3046 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003047 case TSK_ImplicitInstantiation:
3048 break;
3049 }
3050
John McCallf312b1e2010-08-26 23:41:50 +00003051 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3052 "instantiating static data member "
3053 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Chandler Carruth58e390e2010-08-25 08:27:02 +00003055 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3056 TSK_ExplicitInstantiationDefinition;
3057 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3058 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003059 }
3060}
John McCall0c01d182010-03-24 05:22:00 +00003061
3062void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3063 const MultiLevelTemplateArgumentList &TemplateArgs) {
3064 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3065 E = Pattern->ddiag_end(); I != E; ++I) {
3066 DependentDiagnostic *DD = *I;
3067
3068 switch (DD->getKind()) {
3069 case DependentDiagnostic::Access:
3070 HandleDependentAccessCheck(*DD, TemplateArgs);
3071 break;
3072 }
3073 }
3074}