blob: ecb9019136ed0a4f484fa210752340be1e43bbe7 [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 *
Chris Lattner57ad3782011-02-17 20:34:02 +0000104TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
105 LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
106 D->getIdentifier());
107 Owner->addDecl(Inst);
108 return Inst;
109}
110
111Decl *
Douglas Gregor4f722be2009-03-25 15:45:12 +0000112TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
113 assert(false && "Namespaces cannot be instantiated");
114 return D;
115}
116
John McCall3dbd3d52010-02-16 06:53:13 +0000117Decl *
118TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
119 NamespaceAliasDecl *Inst
120 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
121 D->getNamespaceLoc(),
122 D->getAliasLoc(),
123 D->getNamespace()->getIdentifier(),
124 D->getQualifierRange(),
125 D->getQualifier(),
126 D->getTargetNameLoc(),
127 D->getNamespace());
128 Owner->addDecl(Inst);
129 return Inst;
130}
131
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000132Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
133 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000134 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000135 if (DI->getType()->isDependentType() ||
136 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000137 DI = SemaRef.SubstType(DI, TemplateArgs,
138 D->getLocation(), D->getDeclName());
139 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000140 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000141 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000142 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000143 } else {
144 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000145 }
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000147 // Create the new typedef
148 TypedefDecl *Typedef
149 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000150 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000151 if (Invalid)
152 Typedef->setInvalidDecl();
153
John McCallcde5a402011-02-01 08:20:08 +0000154 // If the old typedef was the name for linkage purposes of an anonymous
155 // tag decl, re-establish that relationship for the new typedef.
156 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
157 TagDecl *oldTag = oldTagType->getDecl();
158 if (oldTag->getTypedefForAnonDecl() == D) {
159 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
160 assert(!newTag->getIdentifier() && !newTag->getTypedefForAnonDecl());
161 newTag->setTypedefForAnonDecl(Typedef);
162 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000163 }
164
John McCall5126fd02009-12-30 00:31:22 +0000165 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000166 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
167 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000168 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
169 }
170
John McCall1d8d1cc2010-08-01 02:01:53 +0000171 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000172
John McCall46460a62010-01-20 21:53:11 +0000173 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000174 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000176 return Typedef;
177}
178
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000179/// \brief Instantiate an initializer, breaking it into separate
180/// initialization arguments.
181///
182/// \param S The semantic analysis object.
183///
184/// \param Init The initializer to instantiate.
185///
186/// \param TemplateArgs Template arguments to be substituted into the
187/// initializer.
188///
189/// \param NewArgs Will be filled in with the instantiation arguments.
190///
191/// \returns true if an error occurred, false otherwise
192static bool InstantiateInitializer(Sema &S, Expr *Init,
193 const MultiLevelTemplateArgumentList &TemplateArgs,
194 SourceLocation &LParenLoc,
Nick Lewycky7663f392010-11-20 01:29:55 +0000195 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000196 SourceLocation &RParenLoc) {
197 NewArgs.clear();
198 LParenLoc = SourceLocation();
199 RParenLoc = SourceLocation();
200
201 if (!Init)
202 return false;
203
John McCall4765fa02010-12-06 08:20:24 +0000204 if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000205 Init = ExprTemp->getSubExpr();
206
207 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
208 Init = Binder->getSubExpr();
209
210 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
211 Init = ICE->getSubExprAsWritten();
212
213 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
214 LParenLoc = ParenList->getLParenLoc();
215 RParenLoc = ParenList->getRParenLoc();
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000216 return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
217 true, TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000218 }
219
220 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000221 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000222 if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
223 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000224 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000225
Douglas Gregor28329e52010-03-24 21:22:47 +0000226 // FIXME: Fake locations!
227 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000228 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000229 return false;
230 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000231 }
232
John McCall60d7b3a2010-08-24 06:29:42 +0000233 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000234 if (Result.isInvalid())
235 return true;
236
237 NewArgs.push_back(Result.takeAs<Expr>());
238 return false;
239}
240
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000241Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000242 // If this is the variable for an anonymous struct or union,
243 // instantiate the anonymous struct/union type first.
244 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
245 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
246 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
247 return 0;
248
John McCallce3ff2b2009-08-25 22:02:44 +0000249 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000250 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000251 TemplateArgs,
252 D->getTypeSpecStartLoc(),
253 D->getDeclName());
254 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000255 return 0;
256
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000257 if (DI->getType()->isFunctionType()) {
258 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
259 << D->isStaticDataMember() << DI->getType();
260 return 0;
261 }
262
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000263 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000264 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
265 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000266 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000267 D->getStorageClass(),
268 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000269 Var->setThreadSpecified(D->isThreadSpecified());
270 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000271
John McCallb6217662010-03-15 10:12:16 +0000272 // Substitute the nested name specifier, if any.
273 if (SubstQualifier(D, Var))
274 return 0;
275
Mike Stump1eb44332009-09-09 15:08:12 +0000276 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000277 // out-of-line, the instantiation will have the same lexical
278 // context (which will be a namespace scope) as the template.
279 if (D->isOutOfLine())
280 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000281
John McCall46460a62010-01-20 21:53:11 +0000282 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000283
284 if (!D->isStaticDataMember())
285 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000286
Mike Stump390b4cc2009-05-16 07:39:55 +0000287 // FIXME: In theory, we could have a previous declaration for variables that
288 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000289 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000290 // FIXME: having to fake up a LookupResult is dumb.
291 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000292 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000293 if (D->isStaticDataMember())
294 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000295 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregor7caa6822009-07-24 20:34:43 +0000297 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000298 if (!D->isStaticDataMember())
299 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000300 Owner->makeDeclVisibleInContext(Var);
301 } else {
302 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000303 if (Owner->isFunctionOrMethod())
304 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000305 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000306 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000307
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000308 // Link instantiations of static data members back to the template from
309 // which they were instantiated.
310 if (Var->isStaticDataMember())
311 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000312 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000313
Douglas Gregor60c93c92010-02-09 07:26:29 +0000314 if (Var->getAnyInitializer()) {
315 // We already have an initializer in the class.
316 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000317 if (Var->isStaticDataMember() && !D->isOutOfLine())
318 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
319 else
320 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
321
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000322 // Instantiate the initializer.
323 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000324 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000325 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000326 InitArgs, RParenLoc)) {
Douglas Gregor07a77b42011-01-14 17:12:22 +0000327 // Attach the initializer to the declaration, if we have one.
328 if (InitArgs.size() == 0)
329 SemaRef.ActOnUninitializedDecl(Var, false);
330 else if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000331 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000332 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000333 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000334 move_arg(InitArgs),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000335 RParenLoc);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000336 } else {
337 assert(InitArgs.size() == 1);
John McCall9ae2f072010-08-23 23:25:46 +0000338 Expr *Init = InitArgs.take()[0];
339 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000340 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000341 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000342 // FIXME: Not too happy about invalidating the declaration
343 // because of a bogus initializer.
344 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000345 }
346
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000347 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000348 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000349 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000350
Douglas Gregor5764f612010-05-08 23:05:03 +0000351 // Diagnose unused local variables.
352 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
353 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000354
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000355 return Var;
356}
357
Abramo Bagnara6206d532010-06-05 05:09:32 +0000358Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
359 AccessSpecDecl* AD
360 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
361 D->getAccessSpecifierLoc(), D->getColonLoc());
362 Owner->addHiddenDecl(AD);
363 return AD;
364}
365
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000366Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
367 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000368 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000369 if (DI->getType()->isDependentType() ||
370 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000371 DI = SemaRef.SubstType(DI, TemplateArgs,
372 D->getLocation(), D->getDeclName());
373 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000374 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000375 Invalid = true;
376 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000377 // C++ [temp.arg.type]p3:
378 // If a declaration acquires a function type through a type
379 // dependent on a template-parameter and this causes a
380 // declaration that does not use the syntactic form of a
381 // function declarator to have function type, the program is
382 // ill-formed.
383 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000384 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000385 Invalid = true;
386 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000387 } else {
388 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000389 }
390
391 Expr *BitWidth = D->getBitWidth();
392 if (Invalid)
393 BitWidth = 0;
394 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000395 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000396 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000397
John McCall60d7b3a2010-08-24 06:29:42 +0000398 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000399 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000400 if (InstantiatedBitWidth.isInvalid()) {
401 Invalid = true;
402 BitWidth = 0;
403 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000404 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000405 }
406
John McCall07fb6be2009-10-22 23:33:21 +0000407 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
408 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000409 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000410 D->getLocation(),
411 D->isMutable(),
412 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000413 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000414 D->getAccess(),
415 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000416 if (!Field) {
417 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000418 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
John McCall1d8d1cc2010-08-01 02:01:53 +0000421 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000422
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000423 if (Invalid)
424 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000426 if (!Field->getDeclName()) {
427 // Keep track of where this decl came from.
428 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000429 }
430 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
431 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000432 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000433 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000436 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000437 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000438 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000439
440 return Field;
441}
442
Francois Pichet87c2e122010-11-21 06:08:52 +0000443Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
444 NamedDecl **NamedChain =
445 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
446
447 int i = 0;
448 for (IndirectFieldDecl::chain_iterator PI =
449 D->chain_begin(), PE = D->chain_end();
450 PI != PE; ++PI)
451 NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
452 *PI, TemplateArgs));
453
Francois Pichet40e17752010-12-09 10:07:54 +0000454 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000455 IndirectFieldDecl* IndirectField
456 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000457 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000458 NamedChain, D->getChainingSize());
459
460
461 IndirectField->setImplicit(D->isImplicit());
462 IndirectField->setAccess(D->getAccess());
463 Owner->addDecl(IndirectField);
464 return IndirectField;
465}
466
John McCall02cace72009-08-28 07:59:38 +0000467Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000468 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000469 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000470 if (TypeSourceInfo *Ty = D->getFriendType()) {
471 TypeSourceInfo *InstTy =
472 SemaRef.SubstType(Ty, TemplateArgs,
473 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000474 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000475 return 0;
John McCall02cace72009-08-28 07:59:38 +0000476
Douglas Gregor06245bf2010-04-07 17:57:12 +0000477 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
478 if (!FD)
479 return 0;
480
481 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000482 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000483 Owner->addDecl(FD);
484 return FD;
485 }
486
487 NamedDecl *ND = D->getFriendDecl();
488 assert(ND && "friend decl must be a decl or a type!");
489
John McCallaf2094e2010-04-08 09:05:18 +0000490 // All of the Visit implementations for the various potential friend
491 // declarations have to be carefully written to work for friend
492 // objects, with the most important detail being that the target
493 // decl should almost certainly not be placed in Owner.
494 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000495 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
John McCall02cace72009-08-28 07:59:38 +0000497 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000498 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
499 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000500 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000501 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000502 Owner->addDecl(FD);
503 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000504}
505
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000506Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
507 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Douglas Gregorac7610d2009-06-22 20:57:11 +0000509 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000510 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
John McCall60d7b3a2010-08-24 06:29:42 +0000512 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000513 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000514 if (InstantiatedAssertExpr.isInvalid())
515 return 0;
516
John McCall60d7b3a2010-08-24 06:29:42 +0000517 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000518 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000519 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000520 InstantiatedAssertExpr.get(),
521 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000522}
523
524Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000525 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000526 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000527 D->getTagKeywordLoc(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000528 /*PrevDecl=*/0, D->isScoped(),
529 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000530 if (D->isFixed()) {
531 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
532 // If we have type source information for the underlying type, it means it
533 // has been explicitly set by the user. Perform substitution on it before
534 // moving on.
535 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
536 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
537 TemplateArgs,
538 UnderlyingLoc,
539 DeclarationName()));
540
541 if (!Enum->getIntegerTypeSourceInfo())
542 Enum->setIntegerType(SemaRef.Context.IntTy);
543 }
544 else {
545 assert(!D->getIntegerType()->isDependentType()
546 && "Dependent type without type source info");
547 Enum->setIntegerType(D->getIntegerType());
548 }
549 }
550
John McCall5b629aa2010-10-22 23:36:17 +0000551 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
552
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000553 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000554 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000555 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000556 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000557 Enum->startDefinition();
558
Douglas Gregor96084f12010-03-01 19:00:07 +0000559 if (D->getDeclContext()->isFunctionOrMethod())
560 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
561
John McCalld226f652010-08-21 09:40:31 +0000562 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000563
564 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000565 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
566 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000567 EC != ECEnd; ++EC) {
568 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000569 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000570 if (Expr *UninstValue = EC->getInitExpr()) {
571 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000572 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000573 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
John McCallce3ff2b2009-08-25 22:02:44 +0000575 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000576 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000577
578 // Drop the initial value and continue.
579 bool isInvalid = false;
580 if (Value.isInvalid()) {
581 Value = SemaRef.Owned((Expr *)0);
582 isInvalid = true;
583 }
584
Mike Stump1eb44332009-09-09 15:08:12 +0000585 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000586 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
587 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000588 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000589
590 if (isInvalid) {
591 if (EnumConst)
592 EnumConst->setInvalidDecl();
593 Enum->setInvalidDecl();
594 }
595
596 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000597 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
598
John McCall3b85ecf2010-01-23 22:37:59 +0000599 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000600 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000601 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000602 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000603
604 if (D->getDeclContext()->isFunctionOrMethod()) {
605 // If the enumeration is within a function or method, record the enum
606 // constant as a local.
607 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
608 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000609 }
610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000612 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000613 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000614 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000615 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000616 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000617 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000618
619 return Enum;
620}
621
Douglas Gregor6477b692009-03-25 15:04:13 +0000622Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
623 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
624 return 0;
625}
626
John McCalle29ba202009-08-20 01:44:21 +0000627Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000628 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
629
Douglas Gregor550d9b22009-10-31 17:21:17 +0000630 // Create a local instantiation scope for this class template, which
631 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000632 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000633 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000634 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000635 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000636 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000637
638 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000639
640 // Instantiate the qualifier. We have to do this first in case
641 // we're a friend declaration, because if we are then we need to put
642 // the new declaration in the appropriate context.
643 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
644 if (Qualifier) {
645 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
646 Pattern->getQualifierRange(),
647 TemplateArgs);
648 if (!Qualifier) return 0;
649 }
650
651 CXXRecordDecl *PrevDecl = 0;
652 ClassTemplateDecl *PrevClassTemplate = 0;
653
Nick Lewycky37574f52010-11-08 23:29:42 +0000654 if (!isFriend && Pattern->getPreviousDeclaration()) {
655 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
656 if (Found.first != Found.second) {
657 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
658 if (PrevClassTemplate)
659 PrevDecl = PrevClassTemplate->getTemplatedDecl();
660 }
661 }
662
John McCall93ba8572010-03-25 06:39:04 +0000663 // If this isn't a friend, then it's a member template, in which
664 // case we just want to build the instantiation in the
665 // specialization. If it is a friend, we want to build it in
666 // the appropriate context.
667 DeclContext *DC = Owner;
668 if (isFriend) {
669 if (Qualifier) {
670 CXXScopeSpec SS;
671 SS.setScopeRep(Qualifier);
672 SS.setRange(Pattern->getQualifierRange());
673 DC = SemaRef.computeDeclContext(SS);
674 if (!DC) return 0;
675 } else {
676 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
677 Pattern->getDeclContext(),
678 TemplateArgs);
679 }
680
681 // Look for a previous declaration of the template in the owning
682 // context.
683 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
684 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
685 SemaRef.LookupQualifiedName(R, DC);
686
687 if (R.isSingleResult()) {
688 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
689 if (PrevClassTemplate)
690 PrevDecl = PrevClassTemplate->getTemplatedDecl();
691 }
692
693 if (!PrevClassTemplate && Qualifier) {
694 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000695 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
696 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000697 return 0;
698 }
699
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000700 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000701 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000702 bool Complain = true;
703
704 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
705 // template for struct std::tr1::__detail::_Map_base, where the
706 // template parameters of the friend declaration don't match the
707 // template parameters of the original declaration. In this one
708 // case, we don't complain about the ill-formed friend
709 // declaration.
710 if (isFriend && Pattern->getIdentifier() &&
711 Pattern->getIdentifier()->isStr("_Map_base") &&
712 DC->isNamespace() &&
713 cast<NamespaceDecl>(DC)->getIdentifier() &&
714 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
715 DeclContext *DCParent = DC->getParent();
716 if (DCParent->isNamespace() &&
717 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
718 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
719 DeclContext *DCParent2 = DCParent->getParent();
720 if (DCParent2->isNamespace() &&
721 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
722 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
723 DCParent2->getParent()->isTranslationUnit())
724 Complain = false;
725 }
726 }
727
John McCall93ba8572010-03-25 06:39:04 +0000728 TemplateParameterList *PrevParams
729 = PrevClassTemplate->getTemplateParameters();
730
731 // Make sure the parameter lists match.
732 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000733 Complain,
734 Sema::TPL_TemplateMatch)) {
735 if (Complain)
736 return 0;
737
738 AdoptedPreviousTemplateParams = true;
739 InstParams = PrevParams;
740 }
John McCall93ba8572010-03-25 06:39:04 +0000741
742 // Do some additional validation, then merge default arguments
743 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000744 if (!AdoptedPreviousTemplateParams &&
745 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000746 Sema::TPC_ClassTemplate))
747 return 0;
748 }
749 }
750
John McCalle29ba202009-08-20 01:44:21 +0000751 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000752 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000753 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000754 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000755 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000756
John McCall93ba8572010-03-25 06:39:04 +0000757 if (Qualifier)
758 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000759
John McCalle29ba202009-08-20 01:44:21 +0000760 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000761 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
762 D->getIdentifier(), InstParams, RecordInst,
763 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000764 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000765
John McCall93ba8572010-03-25 06:39:04 +0000766 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000767 if (PrevClassTemplate)
768 Inst->setAccess(PrevClassTemplate->getAccess());
769 else
770 Inst->setAccess(D->getAccess());
771
John McCall93ba8572010-03-25 06:39:04 +0000772 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
773 // TODO: do we want to track the instantiation progeny of this
774 // friend target decl?
775 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000776 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000777 if (!PrevClassTemplate)
778 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000779 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000780
781 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000782 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000783 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000784
Douglas Gregor259571e2009-10-30 22:42:42 +0000785 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000786 if (isFriend) {
787 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000788 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000789 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000790
John McCalle29ba202009-08-20 01:44:21 +0000791 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000792
793 if (!PrevClassTemplate) {
794 // Queue up any out-of-line partial specializations of this member
795 // class template; the client will force their instantiation once
796 // the enclosing class has been instantiated.
797 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
798 D->getPartialSpecializations(PartialSpecs);
799 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
800 if (PartialSpecs[I]->isOutOfLine())
801 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
802 }
803
John McCalle29ba202009-08-20 01:44:21 +0000804 return Inst;
805}
806
Douglas Gregord60e1052009-08-27 16:57:43 +0000807Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000808TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
809 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000810 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
811
812 // Lookup the already-instantiated declaration in the instantiation
813 // of the class template and return that.
814 DeclContext::lookup_result Found
815 = Owner->lookup(ClassTemplate->getDeclName());
816 if (Found.first == Found.second)
817 return 0;
818
819 ClassTemplateDecl *InstClassTemplate
820 = dyn_cast<ClassTemplateDecl>(*Found.first);
821 if (!InstClassTemplate)
822 return 0;
823
Douglas Gregord65587f2010-11-10 19:44:59 +0000824 if (ClassTemplatePartialSpecializationDecl *Result
825 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
826 return Result;
827
828 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000829}
830
831Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000832TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000833 // Create a local instantiation scope for this function template, which
834 // will contain the instantiations of the template parameters and then get
835 // merged with the local instantiation scope for the function template
836 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000837 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000838
Douglas Gregord60e1052009-08-27 16:57:43 +0000839 TemplateParameterList *TempParams = D->getTemplateParameters();
840 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000841 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000842 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000843
Douglas Gregora735b202009-10-13 14:39:41 +0000844 FunctionDecl *Instantiated = 0;
845 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
846 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
847 InstParams));
848 else
849 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
850 D->getTemplatedDecl(),
851 InstParams));
852
853 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000854 return 0;
855
John McCall46460a62010-01-20 21:53:11 +0000856 Instantiated->setAccess(D->getAccess());
857
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000859 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000860 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000861 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000862 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000863 assert(InstTemplate &&
864 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000865
John McCallb1a56e72010-03-26 23:10:15 +0000866 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
867
John McCalle976ffe2009-12-14 23:19:40 +0000868 // Link the instantiation back to the pattern *unless* this is a
869 // non-definition friend declaration.
870 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000871 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000872 InstTemplate->setInstantiatedFromMemberTemplate(D);
873
John McCallb1a56e72010-03-26 23:10:15 +0000874 // Make declarations visible in the appropriate context.
875 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000876 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000877
Douglas Gregord60e1052009-08-27 16:57:43 +0000878 return InstTemplate;
879}
880
Douglas Gregord475b8d2009-03-25 21:17:03 +0000881Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
882 CXXRecordDecl *PrevDecl = 0;
883 if (D->isInjectedClassName())
884 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000885 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000886 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
887 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000888 TemplateArgs);
889 if (!Prev) return 0;
890 PrevDecl = cast<CXXRecordDecl>(Prev);
891 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000892
893 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000894 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000895 D->getLocation(), D->getIdentifier(),
896 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000897
898 // Substitute the nested name specifier, if any.
899 if (SubstQualifier(D, Record))
900 return 0;
901
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000903 // FIXME: Check against AS_none is an ugly hack to work around the issue that
904 // the tag decls introduced by friend class declarations don't have an access
905 // specifier. Remove once this area of the code gets sorted out.
906 if (D->getAccess() != AS_none)
907 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000908 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000909 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000910
John McCall02cace72009-08-28 07:59:38 +0000911 // If the original function was part of a friend declaration,
912 // inherit its namespace state.
913 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
914 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
915
Douglas Gregor9901c572010-05-21 00:31:19 +0000916 // Make sure that anonymous structs and unions are recorded.
917 if (D->isAnonymousStructOrUnion()) {
918 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000919 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000920 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
921 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000922
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000923 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000924 return Record;
925}
926
John McCall02cace72009-08-28 07:59:38 +0000927/// Normal class members are of more specific types and therefore
928/// don't make it here. This function serves two purposes:
929/// 1) instantiating function templates
930/// 2) substituting friend declarations
931/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000932Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000933 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000934 // Check whether there is already a function template specialization for
935 // this declaration.
936 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
937 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000938 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000939 std::pair<const TemplateArgument *, unsigned> Innermost
940 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000942 FunctionDecl *SpecFunc
943 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
944 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Douglas Gregor127102b2009-06-29 20:59:39 +0000946 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000947 if (SpecFunc)
948 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000949 }
Mike Stump1eb44332009-09-09 15:08:12 +0000950
John McCallb0cb0222010-03-27 05:57:59 +0000951 bool isFriend;
952 if (FunctionTemplate)
953 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
954 else
955 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
956
Douglas Gregor79c22782010-01-16 20:21:20 +0000957 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000958 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000959 !(isa<Decl>(Owner) &&
960 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000961 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregore53060f2009-06-25 22:08:12 +0000963 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000964 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
965 TInfo = SubstFunctionType(D, Params);
966 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000967 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000968 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000969
John McCalld325daa2010-03-26 04:53:08 +0000970 NestedNameSpecifier *Qualifier = D->getQualifier();
971 if (Qualifier) {
972 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
973 D->getQualifierRange(),
974 TemplateArgs);
975 if (!Qualifier) return 0;
976 }
977
John McCall68b6b872010-02-06 01:50:47 +0000978 // If we're instantiating a local function declaration, put the result
979 // in the owner; otherwise we need to find the instantiated context.
980 DeclContext *DC;
981 if (D->getDeclContext()->isFunctionOrMethod())
982 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000983 else if (isFriend && Qualifier) {
984 CXXScopeSpec SS;
985 SS.setScopeRep(Qualifier);
986 SS.setRange(D->getQualifierRange());
987 DC = SemaRef.computeDeclContext(SS);
988 if (!DC) return 0;
989 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000990 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
991 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000992 }
John McCall68b6b872010-02-06 01:50:47 +0000993
John McCall02cace72009-08-28 07:59:38 +0000994 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000995 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000996 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000997 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000998 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000999
John McCalld325daa2010-03-26 04:53:08 +00001000 if (Qualifier)
1001 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001002
John McCallb1a56e72010-03-26 23:10:15 +00001003 DeclContext *LexicalDC = Owner;
1004 if (!isFriend && D->isOutOfLine()) {
1005 assert(D->getDeclContext()->isFileContext());
1006 LexicalDC = D->getDeclContext();
1007 }
1008
1009 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Douglas Gregore53060f2009-06-25 22:08:12 +00001011 // Attach the parameters
1012 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001013 if (Params[P])
1014 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001015 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001016
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001017 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001018 if (TemplateParams) {
1019 // Our resulting instantiation is actually a function template, since we
1020 // are substituting only the outer template parameters. For example, given
1021 //
1022 // template<typename T>
1023 // struct X {
1024 // template<typename U> friend void f(T, U);
1025 // };
1026 //
1027 // X<int> x;
1028 //
1029 // We are instantiating the friend function template "f" within X<int>,
1030 // which means substituting int for T, but leaving "f" as a friend function
1031 // template.
1032 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001033 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001034 Function->getLocation(),
1035 Function->getDeclName(),
1036 TemplateParams, Function);
1037 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001038
1039 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001040
1041 if (isFriend && D->isThisDeclarationADefinition()) {
1042 // TODO: should we remember this connection regardless of whether
1043 // the friend declaration provided a body?
1044 FunctionTemplate->setInstantiatedFromMemberTemplate(
1045 D->getDescribedFunctionTemplate());
1046 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001047 } else if (FunctionTemplate) {
1048 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001049 std::pair<const TemplateArgument *, unsigned> Innermost
1050 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001051 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001052 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001053 Innermost.first,
1054 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001055 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001056 } else if (isFriend && D->isThisDeclarationADefinition()) {
1057 // TODO: should we remember this connection regardless of whether
1058 // the friend declaration provided a body?
1059 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001060 }
Douglas Gregora735b202009-10-13 14:39:41 +00001061
Douglas Gregore53060f2009-06-25 22:08:12 +00001062 if (InitFunctionInstantiation(Function, D))
1063 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregore53060f2009-06-25 22:08:12 +00001065 bool Redeclaration = false;
John McCallaf2094e2010-04-08 09:05:18 +00001066 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001067
John McCall68263142009-11-18 22:49:29 +00001068 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1069 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1070
John McCallaf2094e2010-04-08 09:05:18 +00001071 if (DependentFunctionTemplateSpecializationInfo *Info
1072 = D->getDependentSpecializationInfo()) {
1073 assert(isFriend && "non-friend has dependent specialization info?");
1074
1075 // This needs to be set now for future sanity.
1076 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1077
1078 // Instantiate the explicit template arguments.
1079 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1080 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001081 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1082 ExplicitArgs, TemplateArgs))
1083 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001084
1085 // Map the candidate templates to their instantiations.
1086 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1087 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1088 Info->getTemplate(I),
1089 TemplateArgs);
1090 if (!Temp) return 0;
1091
1092 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1093 }
1094
1095 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1096 &ExplicitArgs,
1097 Previous))
1098 Function->setInvalidDecl();
1099
1100 isExplicitSpecialization = true;
1101
1102 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001103 // Look only into the namespace where the friend would be declared to
1104 // find a previous declaration. This is the innermost enclosing namespace,
1105 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001106 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001107
Douglas Gregora735b202009-10-13 14:39:41 +00001108 // In C++, the previous declaration we find might be a tag type
1109 // (class or enum). In this case, the new declaration will hide the
1110 // tag type. Note that this does does not apply if we're declaring a
1111 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001112 if (Previous.isSingleTagDecl())
1113 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001114 }
1115
John McCall9f54ad42009-12-10 09:41:52 +00001116 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Peter Collingbournec80e8112011-01-21 02:08:54 +00001117 isExplicitSpecialization, Redeclaration);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001118
John McCall76d32642010-04-24 01:30:58 +00001119 NamedDecl *PrincipalDecl = (TemplateParams
1120 ? cast<NamedDecl>(FunctionTemplate)
1121 : Function);
1122
Douglas Gregora735b202009-10-13 14:39:41 +00001123 // If the original function was part of a friend declaration,
1124 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001125 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001126 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001127 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001128 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001129 else
Douglas Gregora735b202009-10-13 14:39:41 +00001130 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001131
1132 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1133 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001134
Gabor Greif77535df2010-08-30 22:25:56 +00001135 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001136
Douglas Gregor238058c2010-05-18 05:45:02 +00001137 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1138 D->isThisDeclarationADefinition()) {
1139 // Check for a function body.
1140 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001141 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001142 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1143 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1144 << Function->getDeclName();
1145 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1146 Function->setInvalidDecl();
1147 }
1148 // Check for redefinitions due to other instantiations of this or
1149 // a similar friend function.
1150 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1151 REnd = Function->redecls_end();
1152 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001153 if (*R == Function)
1154 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001155 switch (R->getFriendObjectKind()) {
1156 case Decl::FOK_None:
1157 if (!queuedInstantiation && R->isUsed(false)) {
1158 if (MemberSpecializationInfo *MSInfo
1159 = Function->getMemberSpecializationInfo()) {
1160 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1161 SourceLocation Loc = R->getLocation(); // FIXME
1162 MSInfo->setPointOfInstantiation(Loc);
1163 SemaRef.PendingLocalImplicitInstantiations.push_back(
1164 std::make_pair(Function, Loc));
1165 queuedInstantiation = true;
1166 }
1167 }
1168 }
1169 break;
1170 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001171 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001172 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001173 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001174 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1175 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001176 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001177 Function->setInvalidDecl();
1178 break;
1179 }
1180 }
1181 }
1182 }
Douglas Gregora735b202009-10-13 14:39:41 +00001183 }
1184
John McCall76d32642010-04-24 01:30:58 +00001185 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1186 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1187 PrincipalDecl->setNonMemberOperator();
1188
Douglas Gregore53060f2009-06-25 22:08:12 +00001189 return Function;
1190}
1191
Douglas Gregord60e1052009-08-27 16:57:43 +00001192Decl *
1193TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1194 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001195 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1196 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001197 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001198 // We are creating a function template specialization from a function
1199 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001200 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001201 std::pair<const TemplateArgument *, unsigned> Innermost
1202 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001204 FunctionDecl *SpecFunc
1205 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1206 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Douglas Gregor6b906862009-08-21 00:16:32 +00001208 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001209 if (SpecFunc)
1210 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001211 }
1212
John McCallb0cb0222010-03-27 05:57:59 +00001213 bool isFriend;
1214 if (FunctionTemplate)
1215 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1216 else
1217 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1218
Douglas Gregor79c22782010-01-16 20:21:20 +00001219 bool MergeWithParentScope = (TemplateParams != 0) ||
1220 !(isa<Decl>(Owner) &&
1221 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001222 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001223
John McCall4eab39f2010-10-19 02:26:41 +00001224 // Instantiate enclosing template arguments for friends.
1225 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1226 unsigned NumTempParamLists = 0;
1227 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1228 TempParamLists.set_size(NumTempParamLists);
1229 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1230 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1231 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1232 if (!InstParams)
1233 return NULL;
1234 TempParamLists[I] = InstParams;
1235 }
1236 }
1237
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001238 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001239 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1240 TInfo = SubstFunctionType(D, Params);
1241 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001242 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001243 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001244
Abramo Bagnara723df242010-12-14 22:11:44 +00001245 // \brief If the type of this function, after ignoring parentheses,
1246 // is not *directly* a function type, then we're instantiating a function
1247 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001248 //
1249 // typedef int functype(int, int);
1250 // functype func;
1251 //
1252 // In this case, we'll just go instantiate the ParmVarDecls that we
1253 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001254 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001255 assert(!Params.size() && "Instantiating type could not yield parameters");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001256 llvm::SmallVector<QualType, 4> ParamTypes;
1257 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1258 D->getNumParams(), TemplateArgs, ParamTypes,
1259 &Params))
1260 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001261 }
1262
John McCallb0cb0222010-03-27 05:57:59 +00001263 NestedNameSpecifier *Qualifier = D->getQualifier();
1264 if (Qualifier) {
1265 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1266 D->getQualifierRange(),
1267 TemplateArgs);
1268 if (!Qualifier) return 0;
1269 }
1270
1271 DeclContext *DC = Owner;
1272 if (isFriend) {
1273 if (Qualifier) {
1274 CXXScopeSpec SS;
1275 SS.setScopeRep(Qualifier);
1276 SS.setRange(D->getQualifierRange());
1277 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001278
1279 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1280 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001281 } else {
1282 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1283 D->getDeclContext(),
1284 TemplateArgs);
1285 }
1286 if (!DC) return 0;
1287 }
1288
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001289 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001290 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001291 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Abramo Bagnara25777432010-08-11 22:01:17 +00001293 DeclarationNameInfo NameInfo
1294 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001295 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001296 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001297 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001298 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001299 Constructor->isInlineSpecified(),
1300 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001301 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001302 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001303 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001304 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001305 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001306 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001307 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001308 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001309 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001310 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001311 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001312 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1313 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001314 D->isStatic(),
1315 D->getStorageClassAsWritten(),
1316 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001317 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001318
John McCallb0cb0222010-03-27 05:57:59 +00001319 if (Qualifier)
1320 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001321
Douglas Gregord60e1052009-08-27 16:57:43 +00001322 if (TemplateParams) {
1323 // Our resulting instantiation is actually a function template, since we
1324 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001325 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001326 // template<typename T>
1327 // struct X {
1328 // template<typename U> void f(T, U);
1329 // };
1330 //
1331 // X<int> x;
1332 //
1333 // We are instantiating the member template "f" within X<int>, which means
1334 // substituting int for T, but leaving "f" as a member function template.
1335 // Build the function template itself.
1336 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1337 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001338 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001339 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001340 if (isFriend) {
1341 FunctionTemplate->setLexicalDeclContext(Owner);
1342 FunctionTemplate->setObjectOfFriendDecl(true);
1343 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001344 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001345 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001346 } else if (FunctionTemplate) {
1347 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001348 std::pair<const TemplateArgument *, unsigned> Innermost
1349 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001350 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001351 TemplateArgumentList::CreateCopy(SemaRef.Context,
1352 Innermost.first,
1353 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001354 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001355 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001356 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001357 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001358 }
1359
Mike Stump1eb44332009-09-09 15:08:12 +00001360 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001361 // out-of-line, the instantiation will have the same lexical
1362 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001363 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001364 if (NumTempParamLists)
1365 Method->setTemplateParameterListsInfo(SemaRef.Context,
1366 NumTempParamLists,
1367 TempParamLists.data());
1368
John McCallb0cb0222010-03-27 05:57:59 +00001369 Method->setLexicalDeclContext(Owner);
1370 Method->setObjectOfFriendDecl(true);
1371 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001372 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Douglas Gregor5545e162009-03-24 00:38:23 +00001374 // Attach the parameters
1375 for (unsigned P = 0; P < Params.size(); ++P)
1376 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001377 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001378
1379 if (InitMethodInstantiation(Method, D))
1380 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001381
Abramo Bagnara25777432010-08-11 22:01:17 +00001382 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1383 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
John McCallb0cb0222010-03-27 05:57:59 +00001385 if (!FunctionTemplate || TemplateParams || isFriend) {
1386 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Douglas Gregordec06662009-08-21 18:42:58 +00001388 // In C++, the previous declaration we find might be a tag type
1389 // (class or enum). In this case, the new declaration will hide the
1390 // tag type. Note that this does does not apply if we're declaring a
1391 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001392 if (Previous.isSingleTagDecl())
1393 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001394 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001395
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001396 bool Redeclaration = false;
Peter Collingbournec80e8112011-01-21 02:08:54 +00001397 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001398
Douglas Gregor4ba31362009-12-01 17:24:26 +00001399 if (D->isPure())
1400 SemaRef.CheckPureMethod(Method, SourceRange());
1401
John McCall46460a62010-01-20 21:53:11 +00001402 Method->setAccess(D->getAccess());
1403
Anders Carlsson9eefa222011-01-20 06:52:44 +00001404 SemaRef.CheckOverrideControl(Method);
1405
John McCallb0cb0222010-03-27 05:57:59 +00001406 if (FunctionTemplate) {
1407 // If there's a function template, let our caller handle it.
1408 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1409 // Don't hide a (potentially) valid declaration with an invalid one.
1410 } else {
1411 NamedDecl *DeclToAdd = (TemplateParams
1412 ? cast<NamedDecl>(FunctionTemplate)
1413 : Method);
1414 if (isFriend)
1415 Record->makeDeclVisibleInContext(DeclToAdd);
1416 else
1417 Owner->addDecl(DeclToAdd);
1418 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001419
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001420 return Method;
1421}
1422
Douglas Gregor615c5d42009-03-24 16:43:20 +00001423Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001424 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001425}
1426
Douglas Gregor03b2b072009-03-24 00:15:49 +00001427Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001428 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001429}
1430
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001431Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001432 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001433}
1434
Douglas Gregor6477b692009-03-25 15:04:13 +00001435ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001436 return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001437}
1438
John McCalle29ba202009-08-20 01:44:21 +00001439Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1440 TemplateTypeParmDecl *D) {
1441 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001442 const Type* T = D->getTypeForDecl();
1443 assert(T->isTemplateTypeParmType());
1444 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001445
John McCalle29ba202009-08-20 01:44:21 +00001446 TemplateTypeParmDecl *Inst =
1447 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001448 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001449 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001450 D->wasDeclaredWithTypename(),
1451 D->isParameterPack());
1452
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001453 if (D->hasDefaultArgument())
1454 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001455
Douglas Gregor550d9b22009-10-31 17:21:17 +00001456 // Introduce this template parameter's instantiation into the instantiation
1457 // scope.
1458 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1459
John McCalle29ba202009-08-20 01:44:21 +00001460 return Inst;
1461}
1462
Douglas Gregor33642df2009-10-23 23:25:44 +00001463Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1464 NonTypeTemplateParmDecl *D) {
1465 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001466 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1467 llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1468 llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1469 bool IsExpandedParameterPack = false;
1470 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001471 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001472 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001473
1474 if (D->isExpandedParameterPack()) {
1475 // The non-type template parameter pack is an already-expanded pack
1476 // expansion of types. Substitute into each of the expanded types.
1477 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1478 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1479 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1480 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1481 TemplateArgs,
1482 D->getLocation(),
1483 D->getDeclName());
1484 if (!NewDI)
1485 return 0;
1486
1487 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1488 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1489 D->getLocation());
1490 if (NewT.isNull())
1491 return 0;
1492 ExpandedParameterPackTypes.push_back(NewT);
1493 }
1494
1495 IsExpandedParameterPack = true;
1496 DI = D->getTypeSourceInfo();
1497 T = DI->getType();
1498 } else if (isa<PackExpansionTypeLoc>(TL)) {
1499 // The non-type template parameter pack's type is a pack expansion of types.
1500 // Determine whether we need to expand this parameter pack into separate
1501 // types.
1502 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1503 TypeLoc Pattern = Expansion.getPatternLoc();
1504 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1505 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1506
1507 // Determine whether the set of unexpanded parameter packs can and should
1508 // be expanded.
1509 bool Expand = true;
1510 bool RetainExpansion = false;
1511 llvm::Optional<unsigned> OrigNumExpansions
1512 = Expansion.getTypePtr()->getNumExpansions();
1513 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1514 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1515 Pattern.getSourceRange(),
1516 Unexpanded.data(),
1517 Unexpanded.size(),
1518 TemplateArgs,
1519 Expand, RetainExpansion,
1520 NumExpansions))
1521 return 0;
1522
1523 if (Expand) {
1524 for (unsigned I = 0; I != *NumExpansions; ++I) {
1525 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1526 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1527 D->getLocation(),
1528 D->getDeclName());
1529 if (!NewDI)
1530 return 0;
1531
1532 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1533 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1534 NewDI->getType(),
1535 D->getLocation());
1536 if (NewT.isNull())
1537 return 0;
1538 ExpandedParameterPackTypes.push_back(NewT);
1539 }
1540
1541 // Note that we have an expanded parameter pack. The "type" of this
1542 // expanded parameter pack is the original expansion type, but callers
1543 // will end up using the expanded parameter pack types for type-checking.
1544 IsExpandedParameterPack = true;
1545 DI = D->getTypeSourceInfo();
1546 T = DI->getType();
1547 } else {
1548 // We cannot fully expand the pack expansion now, so substitute into the
1549 // pattern and create a new pack expansion type.
1550 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1551 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1552 D->getLocation(),
1553 D->getDeclName());
1554 if (!NewPattern)
1555 return 0;
1556
1557 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1558 NumExpansions);
1559 if (!DI)
1560 return 0;
1561
1562 T = DI->getType();
1563 }
1564 } else {
1565 // Simple case: substitution into a parameter that is not a parameter pack.
1566 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1567 D->getLocation(), D->getDeclName());
1568 if (!DI)
1569 return 0;
1570
1571 // Check that this type is acceptable for a non-type template parameter.
1572 bool Invalid = false;
1573 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1574 D->getLocation());
1575 if (T.isNull()) {
1576 T = SemaRef.Context.IntTy;
1577 Invalid = true;
1578 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001579 }
1580
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001581 NonTypeTemplateParmDecl *Param;
1582 if (IsExpandedParameterPack)
1583 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1584 D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001585 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001586 D->getPosition(),
1587 D->getIdentifier(), T,
1588 DI,
1589 ExpandedParameterPackTypes.data(),
1590 ExpandedParameterPackTypes.size(),
1591 ExpandedParameterPackTypesAsWritten.data());
1592 else
1593 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1594 D->getLocation(),
1595 D->getDepth() - TemplateArgs.getNumLevels(),
1596 D->getPosition(),
1597 D->getIdentifier(), T,
1598 D->isParameterPack(), DI);
1599
Douglas Gregor33642df2009-10-23 23:25:44 +00001600 if (Invalid)
1601 Param->setInvalidDecl();
1602
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001603 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001604
1605 // Introduce this template parameter's instantiation into the instantiation
1606 // scope.
1607 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001608 return Param;
1609}
1610
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001611Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001612TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1613 TemplateTemplateParmDecl *D) {
1614 // Instantiate the template parameter list of the template template parameter.
1615 TemplateParameterList *TempParams = D->getTemplateParameters();
1616 TemplateParameterList *InstParams;
1617 {
1618 // Perform the actual substitution of template parameters within a new,
1619 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001620 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001621 InstParams = SubstTemplateParams(TempParams);
1622 if (!InstParams)
1623 return NULL;
1624 }
1625
1626 // Build the template template parameter.
1627 TemplateTemplateParmDecl *Param
1628 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001629 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001630 D->getPosition(), D->isParameterPack(),
1631 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001632 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001633
Douglas Gregor9106ef72009-11-11 16:58:32 +00001634 // Introduce this template parameter's instantiation into the instantiation
1635 // scope.
1636 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1637
1638 return Param;
1639}
1640
Douglas Gregor48c32a72009-11-17 06:07:40 +00001641Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1642 // Using directives are never dependent, so they require no explicit
1643
1644 UsingDirectiveDecl *Inst
1645 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1646 D->getNamespaceKeyLocation(),
1647 D->getQualifierRange(), D->getQualifier(),
1648 D->getIdentLocation(),
1649 D->getNominatedNamespace(),
1650 D->getCommonAncestor());
1651 Owner->addDecl(Inst);
1652 return Inst;
1653}
1654
John McCalled976492009-12-04 22:46:56 +00001655Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001656
1657 // The nested name specifier may be dependent, for example
1658 // template <typename T> struct t {
1659 // struct s1 { T f1(); };
1660 // struct s2 : s1 { using s1::f1; };
1661 // };
1662 // template struct t<int>;
1663 // Here, in using s1::f1, s1 refers to t<T>::s1;
1664 // we need to substitute for t<int>::s1.
1665 NestedNameSpecifier *NNS =
1666 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
1667 D->getNestedNameRange(),
1668 TemplateArgs);
1669 if (!NNS)
1670 return 0;
1671
1672 // The name info is non-dependent, so no transformation
1673 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001674 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001675
John McCall9f54ad42009-12-10 09:41:52 +00001676 // We only need to do redeclaration lookups if we're in a class
1677 // scope (in fact, it's not really even possible in non-class
1678 // scopes).
1679 bool CheckRedeclaration = Owner->isRecord();
1680
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001681 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1682 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001683
John McCalled976492009-12-04 22:46:56 +00001684 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001685 D->getNestedNameRange(),
1686 D->getUsingLocation(),
Douglas Gregor1b398202010-09-29 17:58:28 +00001687 NNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001688 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001689 D->isTypeName());
1690
1691 CXXScopeSpec SS;
Douglas Gregor1b398202010-09-29 17:58:28 +00001692 SS.setScopeRep(NNS);
John McCalled976492009-12-04 22:46:56 +00001693 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001694
1695 if (CheckRedeclaration) {
1696 Prev.setHideTags(false);
1697 SemaRef.LookupQualifiedName(Prev, Owner);
1698
1699 // Check for invalid redeclarations.
1700 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1701 D->isTypeName(), SS,
1702 D->getLocation(), Prev))
1703 NewUD->setInvalidDecl();
1704
1705 }
1706
1707 if (!NewUD->isInvalidDecl() &&
1708 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001709 D->getLocation()))
1710 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001711
John McCalled976492009-12-04 22:46:56 +00001712 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1713 NewUD->setAccess(D->getAccess());
1714 Owner->addDecl(NewUD);
1715
John McCall9f54ad42009-12-10 09:41:52 +00001716 // Don't process the shadow decls for an invalid decl.
1717 if (NewUD->isInvalidDecl())
1718 return NewUD;
1719
John McCall323c3102009-12-22 22:26:37 +00001720 bool isFunctionScope = Owner->isFunctionOrMethod();
1721
John McCall9f54ad42009-12-10 09:41:52 +00001722 // Process the shadow decls.
1723 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1724 I != E; ++I) {
1725 UsingShadowDecl *Shadow = *I;
1726 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001727 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1728 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001729 TemplateArgs));
1730
1731 if (CheckRedeclaration &&
1732 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1733 continue;
1734
1735 UsingShadowDecl *InstShadow
1736 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1737 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001738
1739 if (isFunctionScope)
1740 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001741 }
John McCalled976492009-12-04 22:46:56 +00001742
1743 return NewUD;
1744}
1745
1746Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001747 // Ignore these; we handle them in bulk when processing the UsingDecl.
1748 return 0;
John McCalled976492009-12-04 22:46:56 +00001749}
1750
John McCall7ba107a2009-11-18 02:36:19 +00001751Decl * TemplateDeclInstantiator
1752 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001753 NestedNameSpecifier *NNS =
1754 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1755 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001756 TemplateArgs);
1757 if (!NNS)
1758 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001760 CXXScopeSpec SS;
1761 SS.setRange(D->getTargetNestedNameRange());
1762 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001764 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1765 // Hence, no tranformation is required for it.
1766 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001767 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001768 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001769 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001770 /*instantiation*/ true,
1771 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001772 if (UD)
John McCalled976492009-12-04 22:46:56 +00001773 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1774
John McCall7ba107a2009-11-18 02:36:19 +00001775 return UD;
1776}
1777
1778Decl * TemplateDeclInstantiator
1779 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1780 NestedNameSpecifier *NNS =
1781 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1782 D->getTargetNestedNameRange(),
1783 TemplateArgs);
1784 if (!NNS)
1785 return 0;
1786
1787 CXXScopeSpec SS;
1788 SS.setRange(D->getTargetNestedNameRange());
1789 SS.setScopeRep(NNS);
1790
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001791 DeclarationNameInfo NameInfo
1792 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1793
John McCall7ba107a2009-11-18 02:36:19 +00001794 NamedDecl *UD =
1795 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001796 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001797 /*instantiation*/ true,
1798 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001799 if (UD)
John McCalled976492009-12-04 22:46:56 +00001800 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1801
Anders Carlsson0d8df782009-08-29 19:37:28 +00001802 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001803}
1804
John McCallce3ff2b2009-08-25 22:02:44 +00001805Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001806 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001807 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001808 if (D->isInvalidDecl())
1809 return 0;
1810
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001811 return Instantiator.Visit(D);
1812}
1813
John McCalle29ba202009-08-20 01:44:21 +00001814/// \brief Instantiates a nested template parameter list in the current
1815/// instantiation context.
1816///
1817/// \param L The parameter list to instantiate
1818///
1819/// \returns NULL if there was an error
1820TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001821TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001822 // Get errors for all the parameters before bailing out.
1823 bool Invalid = false;
1824
1825 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001826 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001827 ParamVector Params;
1828 Params.reserve(N);
1829 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1830 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001831 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001832 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001833 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001834 }
1835
1836 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001837 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001838 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001839
1840 TemplateParameterList *InstL
1841 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1842 L->getLAngleLoc(), &Params.front(), N,
1843 L->getRAngleLoc());
1844 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001845}
John McCalle29ba202009-08-20 01:44:21 +00001846
Douglas Gregored9c0f92009-10-29 00:04:11 +00001847/// \brief Instantiate the declaration of a class template partial
1848/// specialization.
1849///
1850/// \param ClassTemplate the (instantiated) class template that is partially
1851// specialized by the instantiation of \p PartialSpec.
1852///
1853/// \param PartialSpec the (uninstantiated) class template partial
1854/// specialization that we are instantiating.
1855///
Douglas Gregord65587f2010-11-10 19:44:59 +00001856/// \returns The instantiated partial specialization, if successful; otherwise,
1857/// NULL to indicate an error.
1858ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001859TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1860 ClassTemplateDecl *ClassTemplate,
1861 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001862 // Create a local instantiation scope for this class template partial
1863 // specialization, which will contain the instantiations of the template
1864 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001865 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001866
Douglas Gregored9c0f92009-10-29 00:04:11 +00001867 // Substitute into the template parameters of the class template partial
1868 // specialization.
1869 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1870 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1871 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001872 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001873
1874 // Substitute into the template arguments of the class template partial
1875 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00001876 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
Douglas Gregore02e2622010-12-22 21:19:48 +00001877 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1878 PartialSpec->getNumTemplateArgsAsWritten(),
1879 InstTemplateArgs, TemplateArgs))
1880 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001881
Douglas Gregored9c0f92009-10-29 00:04:11 +00001882 // Check that the template argument list is well-formed for this
1883 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001884 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001885 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1886 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001887 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001888 false,
1889 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001890 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001891
1892 // Figure out where to insert this class template partial specialization
1893 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001894 void *InsertPos = 0;
1895 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001896 = ClassTemplate->findPartialSpecialization(Converted.data(),
1897 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001898
1899 // Build the canonical type that describes the converted template
1900 // arguments of the class template partial specialization.
1901 QualType CanonType
1902 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001903 Converted.data(),
1904 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001905
1906 // Build the fully-sugared type for this class template
1907 // specialization as the user wrote in the specialization
1908 // itself. This means that we'll pretty-print the type retrieved
1909 // from the specialization's declaration the way that the user
1910 // actually wrote the specialization, rather than formatting the
1911 // name based on the "canonical" representation used to store the
1912 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001913 TypeSourceInfo *WrittenTy
1914 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1915 TemplateName(ClassTemplate),
1916 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001917 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001918 CanonType);
1919
1920 if (PrevDecl) {
1921 // We've already seen a partial specialization with the same template
1922 // parameters and template arguments. This can happen, for example, when
1923 // substituting the outer template arguments ends up causing two
1924 // class template partial specializations of a member class template
1925 // to have identical forms, e.g.,
1926 //
1927 // template<typename T, typename U>
1928 // struct Outer {
1929 // template<typename X, typename Y> struct Inner;
1930 // template<typename Y> struct Inner<T, Y>;
1931 // template<typename Y> struct Inner<U, Y>;
1932 // };
1933 //
1934 // Outer<int, int> outer; // error: the partial specializations of Inner
1935 // // have the same signature.
1936 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001937 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001938 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1939 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001940 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001941 }
1942
1943
1944 // Create the class template partial specialization declaration.
1945 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001946 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1947 PartialSpec->getTagKind(),
1948 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001949 PartialSpec->getLocation(),
1950 InstParams,
1951 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001952 Converted.data(),
1953 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001954 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001955 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001956 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001957 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001958 // Substitute the nested name specifier, if any.
1959 if (SubstQualifier(PartialSpec, InstPartialSpec))
1960 return 0;
1961
Douglas Gregored9c0f92009-10-29 00:04:11 +00001962 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001963 InstPartialSpec->setTypeAsWritten(WrittenTy);
1964
Douglas Gregored9c0f92009-10-29 00:04:11 +00001965 // Add this partial specialization to the set of class template partial
1966 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001967 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001968 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001969}
1970
John McCall21ef0fa2010-03-11 09:03:00 +00001971TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001972TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001973 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001974 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1975 assert(OldTInfo && "substituting function without type source info");
1976 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001977 TypeSourceInfo *NewTInfo
1978 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1979 D->getTypeSpecStartLoc(),
1980 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001981 if (!NewTInfo)
1982 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001983
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001984 if (NewTInfo != OldTInfo) {
1985 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001986 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001987 if (FunctionProtoTypeLoc *OldProtoLoc
1988 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001989 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001990 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1991 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001992 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
1993 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
1994 OldIdx != NumOldParams; ++OldIdx) {
1995 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
1996 if (!OldParam->isParameterPack() ||
1997 (NewIdx < NumNewParams &&
1998 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
1999 // Simple case: normal parameter, or a parameter pack that's
2000 // instantiated to a (still-dependent) parameter pack.
2001 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2002 Params.push_back(NewParam);
2003 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
2004 NewParam);
2005 continue;
2006 }
2007
2008 // Parameter pack: make the instantiation an argument pack.
2009 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2010 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002011 unsigned NumArgumentsInExpansion
2012 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2013 TemplateArgs);
2014 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002015 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2016 Params.push_back(NewParam);
2017 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2018 NewParam);
2019 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002020 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002021 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002022 } else {
2023 // The function type itself was not dependent and therefore no
2024 // substitution occurred. However, we still need to instantiate
2025 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002026 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002027 if (FunctionProtoTypeLoc *OldProtoLoc
2028 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2029 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2030 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2031 if (!Parm)
2032 return 0;
2033 Params.push_back(Parm);
2034 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002035 }
2036 }
John McCall21ef0fa2010-03-11 09:03:00 +00002037 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002038}
2039
Mike Stump1eb44332009-09-09 15:08:12 +00002040/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002041/// declaration (New) from the corresponding fields of its template (Tmpl).
2042///
2043/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002044bool
2045TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002046 FunctionDecl *Tmpl) {
2047 if (Tmpl->isDeleted())
2048 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Douglas Gregorcca9e962009-07-01 22:01:06 +00002050 // If we are performing substituting explicitly-specified template arguments
2051 // or deduced template arguments into a function template and we reach this
2052 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002053 // to keeping the new function template specialization. We therefore
2054 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002055 // into a template instantiation for this specific function template
2056 // specialization, which is not a SFINAE context, so that we diagnose any
2057 // further errors in the declaration itself.
2058 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2059 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2060 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2061 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002062 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002063 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002064 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002065 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002066 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002067 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2068 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002069 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002070 }
2071 }
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002073 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2074 assert(Proto && "Function template without prototype?");
2075
2076 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2077 Proto->getNoReturnAttr()) {
2078 // The function has an exception specification or a "noreturn"
2079 // attribute. Substitute into each of the exception types.
2080 llvm::SmallVector<QualType, 4> Exceptions;
2081 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2082 // FIXME: Poor location information!
Douglas Gregorb99268b2010-12-21 00:52:54 +00002083 if (const PackExpansionType *PackExpansion
2084 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2085 // We have a pack expansion. Instantiate it.
2086 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2087 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2088 Unexpanded);
2089 assert(!Unexpanded.empty() &&
2090 "Pack expansion without parameter packs?");
2091
2092 bool Expand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002093 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002094 llvm::Optional<unsigned> NumExpansions
2095 = PackExpansion->getNumExpansions();
Douglas Gregorb99268b2010-12-21 00:52:54 +00002096 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2097 SourceRange(),
2098 Unexpanded.data(),
2099 Unexpanded.size(),
2100 TemplateArgs,
Douglas Gregord3731192011-01-10 07:32:04 +00002101 Expand,
2102 RetainExpansion,
2103 NumExpansions))
Douglas Gregorb99268b2010-12-21 00:52:54 +00002104 break;
2105
2106 if (!Expand) {
2107 // We can't expand this pack expansion into separate arguments yet;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002108 // just substitute into the pattern and create a new pack expansion
2109 // type.
Douglas Gregorb99268b2010-12-21 00:52:54 +00002110 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2111 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2112 TemplateArgs,
2113 New->getLocation(), New->getDeclName());
2114 if (T.isNull())
2115 break;
2116
Douglas Gregorcded4f62011-01-14 17:04:44 +00002117 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
Douglas Gregorb99268b2010-12-21 00:52:54 +00002118 Exceptions.push_back(T);
2119 continue;
2120 }
2121
2122 // Substitute into the pack expansion pattern for each template
2123 bool Invalid = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002124 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
Douglas Gregorb99268b2010-12-21 00:52:54 +00002125 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2126
2127 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2128 TemplateArgs,
2129 New->getLocation(), New->getDeclName());
2130 if (T.isNull()) {
2131 Invalid = true;
2132 break;
2133 }
2134
2135 Exceptions.push_back(T);
2136 }
2137
2138 if (Invalid)
2139 break;
2140
2141 continue;
2142 }
2143
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002144 QualType T
2145 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2146 New->getLocation(), New->getDeclName());
2147 if (T.isNull() ||
2148 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2149 continue;
2150
2151 Exceptions.push_back(T);
2152 }
2153
2154 // Rebuild the function type
2155
John McCalle23cf432010-12-14 08:05:40 +00002156 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2157 EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2158 EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2159 EPI.NumExceptions = Exceptions.size();
2160 EPI.Exceptions = Exceptions.data();
2161 EPI.ExtInfo = Proto->getExtInfo();
2162
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002163 const FunctionProtoType *NewProto
2164 = New->getType()->getAs<FunctionProtoType>();
2165 assert(NewProto && "Template instantiation without function prototype?");
2166 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2167 NewProto->arg_type_begin(),
2168 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +00002169 EPI));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002170 }
2171
John McCall1d8d1cc2010-08-01 02:01:53 +00002172 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002173
Douglas Gregore53060f2009-06-25 22:08:12 +00002174 return false;
2175}
2176
Douglas Gregor5545e162009-03-24 00:38:23 +00002177/// \brief Initializes common fields of an instantiated method
2178/// declaration (New) from the corresponding fields of its template
2179/// (Tmpl).
2180///
2181/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002182bool
2183TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002184 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002185 if (InitFunctionInstantiation(New, Tmpl))
2186 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002187
Douglas Gregor5545e162009-03-24 00:38:23 +00002188 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002189 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002190 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002191
2192 // FIXME: attributes
2193 // FIXME: New needs a pointer to Tmpl
2194 return false;
2195}
Douglas Gregora58861f2009-05-13 20:28:22 +00002196
2197/// \brief Instantiate the definition of the given function from its
2198/// template.
2199///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002200/// \param PointOfInstantiation the point at which the instantiation was
2201/// required. Note that this is not precisely a "point of instantiation"
2202/// for the function, but it's close.
2203///
Douglas Gregora58861f2009-05-13 20:28:22 +00002204/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002205/// function template specialization or member function of a class template
2206/// specialization.
2207///
2208/// \param Recursive if true, recursively instantiates any functions that
2209/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002210///
2211/// \param DefinitionRequired if true, then we are performing an explicit
2212/// instantiation where the body of the function is required. Complain if
2213/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002214void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002215 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002216 bool Recursive,
2217 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002218 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002219 return;
2220
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002221 // Never instantiate an explicit specialization.
2222 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2223 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002224
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002225 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002226 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002227 Stmt *Pattern = 0;
2228 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002229 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002230
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002231 if (!Pattern) {
2232 if (DefinitionRequired) {
2233 if (Function->getPrimaryTemplate())
2234 Diag(PointOfInstantiation,
2235 diag::err_explicit_instantiation_undefined_func_template)
2236 << Function->getPrimaryTemplate();
2237 else
2238 Diag(PointOfInstantiation,
2239 diag::err_explicit_instantiation_undefined_member)
2240 << 1 << Function->getDeclName() << Function->getDeclContext();
2241
2242 if (PatternDecl)
2243 Diag(PatternDecl->getLocation(),
2244 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002245 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002246 } else if (Function->getTemplateSpecializationKind()
2247 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002248 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002249 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002250 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002251
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002252 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002253 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002254
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002255 // C++0x [temp.explicit]p9:
2256 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002257 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002258 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002259 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002260 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002261 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002262 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002263
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002264 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2265 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002266 return;
2267
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002268 // If we're performing recursive template instantiation, create our own
2269 // queue of pending implicit instantiations that we will instantiate later,
2270 // while we're still within our own instantiation context.
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002271 llvm::SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002272 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002273 if (Recursive) {
2274 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002275 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002276 }
Mike Stump1eb44332009-09-09 15:08:12 +00002277
Douglas Gregor9679caf2010-05-12 17:27:19 +00002278 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002279 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002280 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002281
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002282 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002283 // recorded, unless we're actually a member function within a local
2284 // class, in which case we need to merge our results with the parent
2285 // scope (of the enclosing function).
2286 bool MergeWithParentScope = false;
2287 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2288 MergeWithParentScope = Rec->isLocalClass();
2289
2290 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002291
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002292 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002293 // instantiation scope, and set the parameter names to those used
2294 // in the template.
Douglas Gregor12c9c002011-01-07 16:43:16 +00002295 unsigned FParamIdx = 0;
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002296 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2297 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002298 if (!PatternParam->isParameterPack()) {
2299 // Simple case: not a parameter pack.
2300 assert(FParamIdx < Function->getNumParams());
2301 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2302 FunctionParam->setDeclName(PatternParam->getDeclName());
2303 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2304 ++FParamIdx;
2305 continue;
2306 }
2307
2308 // Expand the parameter pack.
2309 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2310 for (unsigned NumFParams = Function->getNumParams();
2311 FParamIdx < NumFParams;
2312 ++FParamIdx) {
2313 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2314 FunctionParam->setDeclName(PatternParam->getDeclName());
2315 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2316 }
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002317 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002318
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002319 // Enter the scope of this instantiation. We don't use
2320 // PushDeclContext because we don't have a scope.
John McCalleee1d542011-02-14 07:13:47 +00002321 Sema::ContextRAII savedContext(*this, Function);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002322
Mike Stump1eb44332009-09-09 15:08:12 +00002323 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002324 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002325
2326 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002327 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002328 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2329 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2330 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002331 }
2332
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002333 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002334 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002335
Douglas Gregor52604ab2009-09-11 21:19:12 +00002336 if (Body.isInvalid())
2337 Function->setInvalidDecl();
2338
John McCall9ae2f072010-08-23 23:25:46 +00002339 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002340 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002341
John McCall0c01d182010-03-24 05:22:00 +00002342 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2343
John McCalleee1d542011-02-14 07:13:47 +00002344 savedContext.pop();
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002345
2346 DeclGroupRef DG(Function);
2347 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Douglas Gregor60406be2010-01-16 22:29:39 +00002349 // This class may have local implicit instantiations that need to be
2350 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002351 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002352 Scope.Exit();
2353
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002354 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002355 // Define any pending vtables.
2356 DefineUsedVTables();
2357
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002358 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002359 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002360 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002362 // Restore the set of pending vtables.
2363 VTableUses.swap(SavedVTableUses);
2364
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002365 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002366 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002367 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002368}
2369
2370/// \brief Instantiate the definition of the given variable from its
2371/// template.
2372///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002373/// \param PointOfInstantiation the point at which the instantiation was
2374/// required. Note that this is not precisely a "point of instantiation"
2375/// for the function, but it's close.
2376///
2377/// \param Var the already-instantiated declaration of a static member
2378/// variable of a class template specialization.
2379///
2380/// \param Recursive if true, recursively instantiates any functions that
2381/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002382///
2383/// \param DefinitionRequired if true, then we are performing an explicit
2384/// instantiation where an out-of-line definition of the member variable
2385/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002386void Sema::InstantiateStaticDataMemberDefinition(
2387 SourceLocation PointOfInstantiation,
2388 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002389 bool Recursive,
2390 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002391 if (Var->isInvalidDecl())
2392 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002393
Douglas Gregor7caa6822009-07-24 20:34:43 +00002394 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002395 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002396 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002397 assert(Def->isStaticDataMember() && "Not a static data member?");
2398 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Douglas Gregor0d035142009-10-27 18:42:08 +00002400 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002401 // We did not find an out-of-line definition of this static data member,
2402 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002403 // instantiate this definition (or provide a specialization for it) in
2404 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002405 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002406 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002407 Diag(PointOfInstantiation,
2408 diag::err_explicit_instantiation_undefined_member)
2409 << 2 << Var->getDeclName() << Var->getDeclContext();
2410 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002411 } else if (Var->getTemplateSpecializationKind()
2412 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002413 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002414 std::make_pair(Var, PointOfInstantiation));
2415 }
2416
Douglas Gregor7caa6822009-07-24 20:34:43 +00002417 return;
2418 }
2419
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002420 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002421 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002422 return;
2423
2424 // C++0x [temp.explicit]p9:
2425 // Except for inline functions, other explicit instantiation declarations
2426 // have the effect of suppressing the implicit instantiation of the entity
2427 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002428 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002429 == TSK_ExplicitInstantiationDeclaration)
2430 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002431
Douglas Gregor7caa6822009-07-24 20:34:43 +00002432 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2433 if (Inst)
2434 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002435
Douglas Gregor7caa6822009-07-24 20:34:43 +00002436 // If we're performing recursive template instantiation, create our own
2437 // queue of pending implicit instantiations that we will instantiate later,
2438 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002439 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002440 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002441 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Douglas Gregor7caa6822009-07-24 20:34:43 +00002443 // Enter the scope of this instantiation. We don't use
2444 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002445 ContextRAII previousContext(*this, Var->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002447 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002448 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002449 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002450
2451 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002452
2453 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002454 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2455 assert(MSInfo && "Missing member specialization information?");
2456 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2457 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002458 DeclGroupRef DG(Var);
2459 Consumer.HandleTopLevelDecl(DG);
2460 }
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Douglas Gregor7caa6822009-07-24 20:34:43 +00002462 if (Recursive) {
2463 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002464 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002465 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Douglas Gregor7caa6822009-07-24 20:34:43 +00002467 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002468 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002469 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002470}
Douglas Gregor815215d2009-05-27 05:35:12 +00002471
Anders Carlsson09025312009-08-29 05:16:22 +00002472void
2473Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2474 const CXXConstructorDecl *Tmpl,
2475 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002476
Anders Carlsson09025312009-08-29 05:16:22 +00002477 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002478 bool AnyErrors = false;
2479
Anders Carlsson09025312009-08-29 05:16:22 +00002480 // Instantiate all the initializers.
2481 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002482 InitsEnd = Tmpl->init_end();
2483 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002484 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002485
Chandler Carruth030ef472010-09-03 21:54:20 +00002486 // Only instantiate written initializers, let Sema re-construct implicit
2487 // ones.
2488 if (!Init->isWritten())
2489 continue;
2490
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002491 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002492 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002493
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002494 SourceLocation EllipsisLoc;
2495
2496 if (Init->isPackExpansion()) {
2497 // This is a pack expansion. We should expand it now.
2498 TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
2499 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2500 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2501 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002502 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002503 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002504 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
2505 BaseTL.getSourceRange(),
2506 Unexpanded.data(),
2507 Unexpanded.size(),
2508 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002509 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002510 NumExpansions)) {
2511 AnyErrors = true;
2512 New->setInvalidDecl();
2513 continue;
2514 }
2515 assert(ShouldExpand && "Partial instantiation of base initializer?");
2516
2517 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002518 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002519 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2520
2521 // Instantiate the initializer.
2522 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2523 LParenLoc, NewArgs, RParenLoc)) {
2524 AnyErrors = true;
2525 break;
2526 }
2527
2528 // Instantiate the base type.
2529 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2530 TemplateArgs,
2531 Init->getSourceLocation(),
2532 New->getDeclName());
2533 if (!BaseTInfo) {
2534 AnyErrors = true;
2535 break;
2536 }
2537
2538 // Build the initializer.
2539 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2540 BaseTInfo,
2541 (Expr **)NewArgs.data(),
2542 NewArgs.size(),
2543 Init->getLParenLoc(),
2544 Init->getRParenLoc(),
2545 New->getParent(),
2546 SourceLocation());
2547 if (NewInit.isInvalid()) {
2548 AnyErrors = true;
2549 break;
2550 }
2551
2552 NewInits.push_back(NewInit.get());
2553 NewArgs.clear();
2554 }
2555
2556 continue;
2557 }
2558
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002559 // Instantiate the initializer.
2560 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002561 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002562 AnyErrors = true;
2563 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002564 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002565
Anders Carlsson09025312009-08-29 05:16:22 +00002566 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002567 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002568 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002569 TemplateArgs,
2570 Init->getSourceLocation(),
2571 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002572 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002573 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002574 New->setInvalidDecl();
2575 continue;
2576 }
2577
John McCalla93c9342009-12-07 02:54:59 +00002578 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002579 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002580 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002581 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002582 Init->getRParenLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002583 New->getParent(),
2584 EllipsisLoc);
Anders Carlsson09025312009-08-29 05:16:22 +00002585 } else if (Init->isMemberInitializer()) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00002586 FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
2587 Init->getMemberLocation(),
2588 Init->getMember(),
2589 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002590
2591 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002592 NewArgs.size(),
2593 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002594 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002595 Init->getRParenLoc());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002596 } else if (Init->isIndirectMemberInitializer()) {
2597 IndirectFieldDecl *IndirectMember =
2598 cast<IndirectFieldDecl>(FindInstantiatedDecl(
2599 Init->getMemberLocation(),
2600 Init->getIndirectMember(), TemplateArgs));
2601
2602 NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2603 NewArgs.size(),
2604 Init->getSourceLocation(),
2605 Init->getLParenLoc(),
2606 Init->getRParenLoc());
Anders Carlsson09025312009-08-29 05:16:22 +00002607 }
2608
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002609 if (NewInit.isInvalid()) {
2610 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002611 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002612 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002613 // FIXME: It would be nice if ASTOwningVector had a release function.
2614 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002615
Anders Carlsson09025312009-08-29 05:16:22 +00002616 NewInits.push_back((MemInitTy *)NewInit.get());
2617 }
2618 }
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Anders Carlsson09025312009-08-29 05:16:22 +00002620 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002621 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002622 /*FIXME: ColonLoc */
2623 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002624 NewInits.data(), NewInits.size(),
2625 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002626}
2627
John McCall52a575a2009-08-29 08:11:13 +00002628// TODO: this could be templated if the various decl types used the
2629// same method name.
2630static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2631 ClassTemplateDecl *Instance) {
2632 Pattern = Pattern->getCanonicalDecl();
2633
2634 do {
2635 Instance = Instance->getCanonicalDecl();
2636 if (Pattern == Instance) return true;
2637 Instance = Instance->getInstantiatedFromMemberTemplate();
2638 } while (Instance);
2639
2640 return false;
2641}
2642
Douglas Gregor0d696532009-09-28 06:34:35 +00002643static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2644 FunctionTemplateDecl *Instance) {
2645 Pattern = Pattern->getCanonicalDecl();
2646
2647 do {
2648 Instance = Instance->getCanonicalDecl();
2649 if (Pattern == Instance) return true;
2650 Instance = Instance->getInstantiatedFromMemberTemplate();
2651 } while (Instance);
2652
2653 return false;
2654}
2655
Douglas Gregored9c0f92009-10-29 00:04:11 +00002656static bool
2657isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2658 ClassTemplatePartialSpecializationDecl *Instance) {
2659 Pattern
2660 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2661 do {
2662 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2663 Instance->getCanonicalDecl());
2664 if (Pattern == Instance)
2665 return true;
2666 Instance = Instance->getInstantiatedFromMember();
2667 } while (Instance);
2668
2669 return false;
2670}
2671
John McCall52a575a2009-08-29 08:11:13 +00002672static bool isInstantiationOf(CXXRecordDecl *Pattern,
2673 CXXRecordDecl *Instance) {
2674 Pattern = Pattern->getCanonicalDecl();
2675
2676 do {
2677 Instance = Instance->getCanonicalDecl();
2678 if (Pattern == Instance) return true;
2679 Instance = Instance->getInstantiatedFromMemberClass();
2680 } while (Instance);
2681
2682 return false;
2683}
2684
2685static bool isInstantiationOf(FunctionDecl *Pattern,
2686 FunctionDecl *Instance) {
2687 Pattern = Pattern->getCanonicalDecl();
2688
2689 do {
2690 Instance = Instance->getCanonicalDecl();
2691 if (Pattern == Instance) return true;
2692 Instance = Instance->getInstantiatedFromMemberFunction();
2693 } while (Instance);
2694
2695 return false;
2696}
2697
2698static bool isInstantiationOf(EnumDecl *Pattern,
2699 EnumDecl *Instance) {
2700 Pattern = Pattern->getCanonicalDecl();
2701
2702 do {
2703 Instance = Instance->getCanonicalDecl();
2704 if (Pattern == Instance) return true;
2705 Instance = Instance->getInstantiatedFromMemberEnum();
2706 } while (Instance);
2707
2708 return false;
2709}
2710
John McCalled976492009-12-04 22:46:56 +00002711static bool isInstantiationOf(UsingShadowDecl *Pattern,
2712 UsingShadowDecl *Instance,
2713 ASTContext &C) {
2714 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2715}
2716
2717static bool isInstantiationOf(UsingDecl *Pattern,
2718 UsingDecl *Instance,
2719 ASTContext &C) {
2720 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2721}
2722
John McCall7ba107a2009-11-18 02:36:19 +00002723static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2724 UsingDecl *Instance,
2725 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002726 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002727}
2728
2729static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002730 UsingDecl *Instance,
2731 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002732 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002733}
2734
John McCall52a575a2009-08-29 08:11:13 +00002735static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2736 VarDecl *Instance) {
2737 assert(Instance->isStaticDataMember());
2738
2739 Pattern = Pattern->getCanonicalDecl();
2740
2741 do {
2742 Instance = Instance->getCanonicalDecl();
2743 if (Pattern == Instance) return true;
2744 Instance = Instance->getInstantiatedFromStaticDataMember();
2745 } while (Instance);
2746
2747 return false;
2748}
2749
John McCalled976492009-12-04 22:46:56 +00002750// Other is the prospective instantiation
2751// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002752static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002753 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002754 if (UnresolvedUsingTypenameDecl *UUD
2755 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2756 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2757 return isInstantiationOf(UUD, UD, Ctx);
2758 }
2759 }
2760
2761 if (UnresolvedUsingValueDecl *UUD
2762 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002763 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2764 return isInstantiationOf(UUD, UD, Ctx);
2765 }
2766 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002767
Anders Carlsson0d8df782009-08-29 19:37:28 +00002768 return false;
2769 }
Mike Stump1eb44332009-09-09 15:08:12 +00002770
John McCall52a575a2009-08-29 08:11:13 +00002771 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2772 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002773
John McCall52a575a2009-08-29 08:11:13 +00002774 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2775 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002776
John McCall52a575a2009-08-29 08:11:13 +00002777 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2778 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002779
Douglas Gregor7caa6822009-07-24 20:34:43 +00002780 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002781 if (Var->isStaticDataMember())
2782 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2783
2784 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2785 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002786
Douglas Gregor0d696532009-09-28 06:34:35 +00002787 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2788 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2789
Douglas Gregored9c0f92009-10-29 00:04:11 +00002790 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2791 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2792 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2793 PartialSpec);
2794
Anders Carlssond8b285f2009-09-01 04:26:58 +00002795 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2796 if (!Field->getDeclName()) {
2797 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002798 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002799 cast<FieldDecl>(D);
2800 }
2801 }
Mike Stump1eb44332009-09-09 15:08:12 +00002802
John McCalled976492009-12-04 22:46:56 +00002803 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2804 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2805
2806 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2807 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2808
Douglas Gregor815215d2009-05-27 05:35:12 +00002809 return D->getDeclName() && isa<NamedDecl>(Other) &&
2810 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2811}
2812
2813template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002814static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002815 NamedDecl *D,
2816 ForwardIterator first,
2817 ForwardIterator last) {
2818 for (; first != last; ++first)
2819 if (isInstantiationOf(Ctx, D, *first))
2820 return cast<NamedDecl>(*first);
2821
2822 return 0;
2823}
2824
John McCall02cace72009-08-28 07:59:38 +00002825/// \brief Finds the instantiation of the given declaration context
2826/// within the current instantiation.
2827///
2828/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002829DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002830 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002831 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002832 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002833 return cast_or_null<DeclContext>(ID);
2834 } else return DC;
2835}
2836
Douglas Gregored961e72009-05-27 17:54:46 +00002837/// \brief Find the instantiation of the given declaration within the
2838/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002839///
2840/// This routine is intended to be used when \p D is a declaration
2841/// referenced from within a template, that needs to mapped into the
2842/// corresponding declaration within an instantiation. For example,
2843/// given:
2844///
2845/// \code
2846/// template<typename T>
2847/// struct X {
2848/// enum Kind {
2849/// KnownValue = sizeof(T)
2850/// };
2851///
2852/// bool getKind() const { return KnownValue; }
2853/// };
2854///
2855/// template struct X<int>;
2856/// \endcode
2857///
2858/// In the instantiation of X<int>::getKind(), we need to map the
2859/// EnumConstantDecl for KnownValue (which refers to
2860/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002861/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2862/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002863NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002864 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002865 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002866 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002867 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002868 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002869 // D is a local of some kind. Look into the map of local
2870 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00002871 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
2872 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
2873 = CurrentInstantiationScope->findInstantiationOf(D);
Chris Lattnerd8e54992011-02-17 19:47:42 +00002874
Chris Lattner57ad3782011-02-17 20:34:02 +00002875 if (Found) {
2876 if (Decl *FD = Found->dyn_cast<Decl *>())
2877 return cast<NamedDecl>(FD);
2878
2879 unsigned PackIdx = ArgumentPackSubstitutionIndex;
2880 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
2881 }
2882
2883 // If we didn't find the decl, then we must have a label decl that hasn't
2884 // been found yet. Lazily instantiate it and return it now.
2885 assert(isa<LabelDecl>(D));
Chris Lattnerd8e54992011-02-17 19:47:42 +00002886
Chris Lattner57ad3782011-02-17 20:34:02 +00002887 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
2888 assert(Inst && "Failed to instantiate label??");
2889
2890 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
2891 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002892 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002893
Douglas Gregore95b4092009-09-16 18:34:49 +00002894 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2895 if (!Record->isDependentContext())
2896 return D;
2897
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002898 // If the RecordDecl is actually the injected-class-name or a
2899 // "templated" declaration for a class template, class template
2900 // partial specialization, or a member class of a class template,
2901 // substitute into the injected-class-name of the class template
2902 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002903 QualType T;
2904 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2905
2906 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002907 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002908 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2909 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002910 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002911
2912 // If we call SubstType with an InjectedClassNameType here we
2913 // can end up in an infinite loop.
2914 T = Context.getTypeDeclType(Record);
2915 assert(isa<InjectedClassNameType>(T) &&
2916 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002917 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002918 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002919
2920 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002921 // Substitute into the injected-class-name to get the type
2922 // corresponding to the instantiation we want, which may also be
2923 // the current instantiation (if we're in a template
2924 // definition). This substitution should never fail, since we
2925 // know we can instantiate the injected-class-name or we
2926 // wouldn't have gotten to the injected-class-name!
2927
2928 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2929 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002930 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2931 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2932
2933 if (!T->isDependentType()) {
2934 assert(T->isRecordType() && "Instantiation must produce a record type");
2935 return T->getAs<RecordType>()->getDecl();
2936 }
2937
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002938 // We are performing "partial" template instantiation to create
2939 // the member declarations for the members of a class template
2940 // specialization. Therefore, D is actually referring to something
2941 // in the current instantiation. Look through the current
2942 // context, which contains actual instantiations, to find the
2943 // instantiation of the "current instantiation" that D refers
2944 // to.
2945 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002946 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002947 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002948 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002949 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002950 if (isInstantiationOf(ClassTemplate,
2951 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002952 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002953
2954 if (!DC->isDependentContext())
2955 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002956 }
2957
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002958 // We're performing "instantiation" of a member of the current
2959 // instantiation while we are type-checking the
2960 // definition. Compute the declaration context and return that.
2961 assert(!SawNonDependentContext &&
2962 "No dependent context while instantiating record");
2963 DeclContext *DC = computeDeclContext(T);
2964 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002965 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002966 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002967 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002968
Douglas Gregore95b4092009-09-16 18:34:49 +00002969 // Fall through to deal with other dependent record types (e.g.,
2970 // anonymous unions in class templates).
2971 }
John McCall52a575a2009-08-29 08:11:13 +00002972
Douglas Gregore95b4092009-09-16 18:34:49 +00002973 if (!ParentDC->isDependentContext())
2974 return D;
2975
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002976 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002977 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002978 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002979
Douglas Gregor815215d2009-05-27 05:35:12 +00002980 if (ParentDC != D->getDeclContext()) {
2981 // We performed some kind of instantiation in the parent context,
2982 // so now we need to look into the instantiated parent context to
2983 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002984
John McCall3cb0ebd2010-03-10 03:28:59 +00002985 // If our context used to be dependent, we may need to instantiate
2986 // it before performing lookup into that context.
2987 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002988 if (!Spec->isDependentContext()) {
2989 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002990 const RecordType *Tag = T->getAs<RecordType>();
2991 assert(Tag && "type of non-dependent record is not a RecordType");
2992 if (!Tag->isBeingDefined() &&
2993 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2994 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002995
2996 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002997 }
2998 }
2999
Douglas Gregor815215d2009-05-27 05:35:12 +00003000 NamedDecl *Result = 0;
3001 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003002 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00003003 Result = findInstantiationOf(Context, D, Found.first, Found.second);
3004 } else {
3005 // Since we don't have a name for the entity we're looking for,
3006 // our only option is to walk through all of the declarations to
3007 // find that name. This will occur in a few cases:
3008 //
3009 // - anonymous struct/union within a template
3010 // - unnamed class/struct/union/enum within a template
3011 //
3012 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003013 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003014 ParentDC->decls_begin(),
3015 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003016 }
Mike Stump1eb44332009-09-09 15:08:12 +00003017
John McCall9f54ad42009-12-10 09:41:52 +00003018 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00003019 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
3020 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00003021 && "Unable to find instantiation of declaration!");
3022
Douglas Gregor815215d2009-05-27 05:35:12 +00003023 D = Result;
3024 }
3025
Douglas Gregor815215d2009-05-27 05:35:12 +00003026 return D;
3027}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003028
Mike Stump1eb44332009-09-09 15:08:12 +00003029/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003030/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003031void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003032 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003033 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003034 PendingImplicitInstantiation Inst;
3035
3036 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003037 Inst = PendingInstantiations.front();
3038 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003039 } else {
3040 Inst = PendingLocalImplicitInstantiations.front();
3041 PendingLocalImplicitInstantiations.pop_front();
3042 }
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Douglas Gregor7caa6822009-07-24 20:34:43 +00003044 // Instantiate function definitions
3045 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003046 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3047 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003048 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3049 TSK_ExplicitInstantiationDefinition;
3050 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3051 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003052 continue;
3053 }
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Douglas Gregor7caa6822009-07-24 20:34:43 +00003055 // Instantiate static data member definitions.
3056 VarDecl *Var = cast<VarDecl>(Inst.first);
3057 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003058
Chandler Carruth291b4412010-02-13 10:17:50 +00003059 // Don't try to instantiate declarations if the most recent redeclaration
3060 // is invalid.
3061 if (Var->getMostRecentDeclaration()->isInvalidDecl())
3062 continue;
3063
3064 // Check if the most recent declaration has changed the specialization kind
3065 // and removed the need for implicit instantiation.
3066 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3067 case TSK_Undeclared:
3068 assert(false && "Cannot instantitiate an undeclared specialization.");
3069 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003070 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003071 continue; // No longer need to instantiate this type.
3072 case TSK_ExplicitInstantiationDefinition:
3073 // We only need an instantiation if the pending instantiation *is* the
3074 // explicit instantiation.
3075 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003076 case TSK_ImplicitInstantiation:
3077 break;
3078 }
3079
John McCallf312b1e2010-08-26 23:41:50 +00003080 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3081 "instantiating static data member "
3082 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003083
Chandler Carruth58e390e2010-08-25 08:27:02 +00003084 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3085 TSK_ExplicitInstantiationDefinition;
3086 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3087 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003088 }
3089}
John McCall0c01d182010-03-24 05:22:00 +00003090
3091void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3092 const MultiLevelTemplateArgumentList &TemplateArgs) {
3093 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3094 E = Pattern->ddiag_end(); I != E; ++I) {
3095 DependentDiagnostic *DD = *I;
3096
3097 switch (DD->getKind()) {
3098 case DependentDiagnostic::Access:
3099 HandleDependentAccessCheck(*DD, TemplateArgs);
3100 break;
3101 }
3102 }
3103}