blob: 1d75da71f59715777673c854ae0f1e9d4c7966e2 [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) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000030 if (!OldDecl->getQualifierLoc())
31 return false;
32
33 NestedNameSpecifierLoc NewQualifierLoc
34 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
35 TemplateArgs);
36
37 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000038 return true;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000039
40 NewDecl->setQualifierInfo(NewQualifierLoc);
John McCallb6217662010-03-15 10:12:16 +000041 return false;
42}
43
44bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45 TagDecl *NewDecl) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000046 if (!OldDecl->getQualifierLoc())
47 return false;
48
49 NestedNameSpecifierLoc NewQualifierLoc
50 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
51 TemplateArgs);
52
53 if (!NewQualifierLoc)
John McCallb6217662010-03-15 10:12:16 +000054 return true;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +000055
56 NewDecl->setQualifierInfo(NewQualifierLoc);
John McCallb6217662010-03-15 10:12:16 +000057 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)) {
Richard Smith34b41d92011-02-20 03:19:35 +0000327 bool TypeMayContainAuto = true;
Douglas Gregor07a77b42011-01-14 17:12:22 +0000328 // Attach the initializer to the declaration, if we have one.
329 if (InitArgs.size() == 0)
Richard Smith34b41d92011-02-20 03:19:35 +0000330 SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000331 else if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000332 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000333 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000334 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000335 move_arg(InitArgs),
Richard Smith34b41d92011-02-20 03:19:35 +0000336 RParenLoc,
337 TypeMayContainAuto);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000338 } else {
339 assert(InitArgs.size() == 1);
John McCall9ae2f072010-08-23 23:25:46 +0000340 Expr *Init = InitArgs.take()[0];
Richard Smith34b41d92011-02-20 03:19:35 +0000341 SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000342 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000343 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000344 // FIXME: Not too happy about invalidating the declaration
345 // because of a bogus initializer.
346 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000347 }
348
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000349 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000350 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000351 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000352
Douglas Gregor5764f612010-05-08 23:05:03 +0000353 // Diagnose unused local variables.
354 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
355 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000356
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000357 return Var;
358}
359
Abramo Bagnara6206d532010-06-05 05:09:32 +0000360Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
361 AccessSpecDecl* AD
362 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
363 D->getAccessSpecifierLoc(), D->getColonLoc());
364 Owner->addHiddenDecl(AD);
365 return AD;
366}
367
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000368Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
369 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000370 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000371 if (DI->getType()->isDependentType() ||
372 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000373 DI = SemaRef.SubstType(DI, TemplateArgs,
374 D->getLocation(), D->getDeclName());
375 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000376 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000377 Invalid = true;
378 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000379 // C++ [temp.arg.type]p3:
380 // If a declaration acquires a function type through a type
381 // dependent on a template-parameter and this causes a
382 // declaration that does not use the syntactic form of a
383 // function declarator to have function type, the program is
384 // ill-formed.
385 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000386 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000387 Invalid = true;
388 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000389 } else {
390 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000391 }
392
393 Expr *BitWidth = D->getBitWidth();
394 if (Invalid)
395 BitWidth = 0;
396 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000397 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000398 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
John McCall60d7b3a2010-08-24 06:29:42 +0000400 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000401 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000402 if (InstantiatedBitWidth.isInvalid()) {
403 Invalid = true;
404 BitWidth = 0;
405 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000406 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000407 }
408
John McCall07fb6be2009-10-22 23:33:21 +0000409 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
410 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000411 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000412 D->getLocation(),
413 D->isMutable(),
414 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000415 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000416 D->getAccess(),
417 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000418 if (!Field) {
419 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000420 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
John McCall1d8d1cc2010-08-01 02:01:53 +0000423 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000424
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000425 if (Invalid)
426 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000428 if (!Field->getDeclName()) {
429 // Keep track of where this decl came from.
430 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000431 }
432 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
433 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000434 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000435 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000438 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000439 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000440 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000441
442 return Field;
443}
444
Francois Pichet87c2e122010-11-21 06:08:52 +0000445Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
446 NamedDecl **NamedChain =
447 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
448
449 int i = 0;
450 for (IndirectFieldDecl::chain_iterator PI =
451 D->chain_begin(), PE = D->chain_end();
452 PI != PE; ++PI)
453 NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
454 *PI, TemplateArgs));
455
Francois Pichet40e17752010-12-09 10:07:54 +0000456 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000457 IndirectFieldDecl* IndirectField
458 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000459 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000460 NamedChain, D->getChainingSize());
461
462
463 IndirectField->setImplicit(D->isImplicit());
464 IndirectField->setAccess(D->getAccess());
465 Owner->addDecl(IndirectField);
466 return IndirectField;
467}
468
John McCall02cace72009-08-28 07:59:38 +0000469Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000470 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000471 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000472 if (TypeSourceInfo *Ty = D->getFriendType()) {
473 TypeSourceInfo *InstTy =
474 SemaRef.SubstType(Ty, TemplateArgs,
475 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000476 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000477 return 0;
John McCall02cace72009-08-28 07:59:38 +0000478
Douglas Gregor06245bf2010-04-07 17:57:12 +0000479 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
480 if (!FD)
481 return 0;
482
483 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000484 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000485 Owner->addDecl(FD);
486 return FD;
487 }
488
489 NamedDecl *ND = D->getFriendDecl();
490 assert(ND && "friend decl must be a decl or a type!");
491
John McCallaf2094e2010-04-08 09:05:18 +0000492 // All of the Visit implementations for the various potential friend
493 // declarations have to be carefully written to work for friend
494 // objects, with the most important detail being that the target
495 // decl should almost certainly not be placed in Owner.
496 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000497 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
John McCall02cace72009-08-28 07:59:38 +0000499 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000500 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
501 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000502 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000503 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000504 Owner->addDecl(FD);
505 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000506}
507
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000508Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
509 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregorac7610d2009-06-22 20:57:11 +0000511 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000512 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
John McCall60d7b3a2010-08-24 06:29:42 +0000514 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000515 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000516 if (InstantiatedAssertExpr.isInvalid())
517 return 0;
518
John McCall60d7b3a2010-08-24 06:29:42 +0000519 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000520 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000521 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000522 InstantiatedAssertExpr.get(),
523 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000524}
525
526Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000528 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000529 D->getTagKeywordLoc(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000530 /*PrevDecl=*/0, D->isScoped(),
531 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000532 if (D->isFixed()) {
533 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
534 // If we have type source information for the underlying type, it means it
535 // has been explicitly set by the user. Perform substitution on it before
536 // moving on.
537 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
538 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
539 TemplateArgs,
540 UnderlyingLoc,
541 DeclarationName()));
542
543 if (!Enum->getIntegerTypeSourceInfo())
544 Enum->setIntegerType(SemaRef.Context.IntTy);
545 }
546 else {
547 assert(!D->getIntegerType()->isDependentType()
548 && "Dependent type without type source info");
549 Enum->setIntegerType(D->getIntegerType());
550 }
551 }
552
John McCall5b629aa2010-10-22 23:36:17 +0000553 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
554
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000555 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000556 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000557 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000558 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559 Enum->startDefinition();
560
Douglas Gregor96084f12010-03-01 19:00:07 +0000561 if (D->getDeclContext()->isFunctionOrMethod())
562 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
563
John McCalld226f652010-08-21 09:40:31 +0000564 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000565
566 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000567 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
568 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000569 EC != ECEnd; ++EC) {
570 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000571 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000572 if (Expr *UninstValue = EC->getInitExpr()) {
573 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000574 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000575 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
John McCallce3ff2b2009-08-25 22:02:44 +0000577 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000578 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000579
580 // Drop the initial value and continue.
581 bool isInvalid = false;
582 if (Value.isInvalid()) {
583 Value = SemaRef.Owned((Expr *)0);
584 isInvalid = true;
585 }
586
Mike Stump1eb44332009-09-09 15:08:12 +0000587 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000588 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
589 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000590 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000591
592 if (isInvalid) {
593 if (EnumConst)
594 EnumConst->setInvalidDecl();
595 Enum->setInvalidDecl();
596 }
597
598 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000599 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
600
John McCall3b85ecf2010-01-23 22:37:59 +0000601 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000602 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000603 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000604 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000605
606 if (D->getDeclContext()->isFunctionOrMethod()) {
607 // If the enumeration is within a function or method, record the enum
608 // constant as a local.
609 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
610 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000611 }
612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000614 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000615 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000616 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000617 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000618 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000619 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000620
621 return Enum;
622}
623
Douglas Gregor6477b692009-03-25 15:04:13 +0000624Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
625 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
626 return 0;
627}
628
John McCalle29ba202009-08-20 01:44:21 +0000629Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000630 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
631
Douglas Gregor550d9b22009-10-31 17:21:17 +0000632 // Create a local instantiation scope for this class template, which
633 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000634 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000635 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000636 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000637 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000638 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000639
640 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000641
642 // Instantiate the qualifier. We have to do this first in case
643 // we're a friend declaration, because if we are then we need to put
644 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000645 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
646 if (QualifierLoc) {
647 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
648 TemplateArgs);
649 if (!QualifierLoc)
650 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000651 }
652
653 CXXRecordDecl *PrevDecl = 0;
654 ClassTemplateDecl *PrevClassTemplate = 0;
655
Nick Lewycky37574f52010-11-08 23:29:42 +0000656 if (!isFriend && Pattern->getPreviousDeclaration()) {
657 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
658 if (Found.first != Found.second) {
659 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
660 if (PrevClassTemplate)
661 PrevDecl = PrevClassTemplate->getTemplatedDecl();
662 }
663 }
664
John McCall93ba8572010-03-25 06:39:04 +0000665 // If this isn't a friend, then it's a member template, in which
666 // case we just want to build the instantiation in the
667 // specialization. If it is a friend, we want to build it in
668 // the appropriate context.
669 DeclContext *DC = Owner;
670 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000671 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000672 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000673 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000674 DC = SemaRef.computeDeclContext(SS);
675 if (!DC) return 0;
676 } else {
677 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
678 Pattern->getDeclContext(),
679 TemplateArgs);
680 }
681
682 // Look for a previous declaration of the template in the owning
683 // context.
684 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
685 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
686 SemaRef.LookupQualifiedName(R, DC);
687
688 if (R.isSingleResult()) {
689 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
690 if (PrevClassTemplate)
691 PrevDecl = PrevClassTemplate->getTemplatedDecl();
692 }
693
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000694 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000695 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000696 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000697 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000698 return 0;
699 }
700
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000701 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000702 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000703 bool Complain = true;
704
705 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
706 // template for struct std::tr1::__detail::_Map_base, where the
707 // template parameters of the friend declaration don't match the
708 // template parameters of the original declaration. In this one
709 // case, we don't complain about the ill-formed friend
710 // declaration.
711 if (isFriend && Pattern->getIdentifier() &&
712 Pattern->getIdentifier()->isStr("_Map_base") &&
713 DC->isNamespace() &&
714 cast<NamespaceDecl>(DC)->getIdentifier() &&
715 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
716 DeclContext *DCParent = DC->getParent();
717 if (DCParent->isNamespace() &&
718 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
719 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
720 DeclContext *DCParent2 = DCParent->getParent();
721 if (DCParent2->isNamespace() &&
722 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
723 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
724 DCParent2->getParent()->isTranslationUnit())
725 Complain = false;
726 }
727 }
728
John McCall93ba8572010-03-25 06:39:04 +0000729 TemplateParameterList *PrevParams
730 = PrevClassTemplate->getTemplateParameters();
731
732 // Make sure the parameter lists match.
733 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000734 Complain,
735 Sema::TPL_TemplateMatch)) {
736 if (Complain)
737 return 0;
738
739 AdoptedPreviousTemplateParams = true;
740 InstParams = PrevParams;
741 }
John McCall93ba8572010-03-25 06:39:04 +0000742
743 // Do some additional validation, then merge default arguments
744 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000745 if (!AdoptedPreviousTemplateParams &&
746 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000747 Sema::TPC_ClassTemplate))
748 return 0;
749 }
750 }
751
John McCalle29ba202009-08-20 01:44:21 +0000752 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000753 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000754 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000755 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000756 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000757
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000758 if (QualifierLoc)
759 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000760
John McCalle29ba202009-08-20 01:44:21 +0000761 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000762 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
763 D->getIdentifier(), InstParams, RecordInst,
764 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000765 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000766
John McCall93ba8572010-03-25 06:39:04 +0000767 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000768 if (PrevClassTemplate)
769 Inst->setAccess(PrevClassTemplate->getAccess());
770 else
771 Inst->setAccess(D->getAccess());
772
John McCall93ba8572010-03-25 06:39:04 +0000773 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
774 // TODO: do we want to track the instantiation progeny of this
775 // friend target decl?
776 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000777 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000778 if (!PrevClassTemplate)
779 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000780 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000781
782 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000783 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000784 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000785
Douglas Gregor259571e2009-10-30 22:42:42 +0000786 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000787 if (isFriend) {
788 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000789 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000790 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000791
John McCalle29ba202009-08-20 01:44:21 +0000792 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000793
794 if (!PrevClassTemplate) {
795 // Queue up any out-of-line partial specializations of this member
796 // class template; the client will force their instantiation once
797 // the enclosing class has been instantiated.
798 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
799 D->getPartialSpecializations(PartialSpecs);
800 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
801 if (PartialSpecs[I]->isOutOfLine())
802 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
803 }
804
John McCalle29ba202009-08-20 01:44:21 +0000805 return Inst;
806}
807
Douglas Gregord60e1052009-08-27 16:57:43 +0000808Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000809TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
810 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000811 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
812
813 // Lookup the already-instantiated declaration in the instantiation
814 // of the class template and return that.
815 DeclContext::lookup_result Found
816 = Owner->lookup(ClassTemplate->getDeclName());
817 if (Found.first == Found.second)
818 return 0;
819
820 ClassTemplateDecl *InstClassTemplate
821 = dyn_cast<ClassTemplateDecl>(*Found.first);
822 if (!InstClassTemplate)
823 return 0;
824
Douglas Gregord65587f2010-11-10 19:44:59 +0000825 if (ClassTemplatePartialSpecializationDecl *Result
826 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
827 return Result;
828
829 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000830}
831
832Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000833TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000834 // Create a local instantiation scope for this function template, which
835 // will contain the instantiations of the template parameters and then get
836 // merged with the local instantiation scope for the function template
837 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000838 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000839
Douglas Gregord60e1052009-08-27 16:57:43 +0000840 TemplateParameterList *TempParams = D->getTemplateParameters();
841 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000842 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000843 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000844
Douglas Gregora735b202009-10-13 14:39:41 +0000845 FunctionDecl *Instantiated = 0;
846 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
847 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
848 InstParams));
849 else
850 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
851 D->getTemplatedDecl(),
852 InstParams));
853
854 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000855 return 0;
856
John McCall46460a62010-01-20 21:53:11 +0000857 Instantiated->setAccess(D->getAccess());
858
Mike Stump1eb44332009-09-09 15:08:12 +0000859 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000860 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000861 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000862 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000863 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000864 assert(InstTemplate &&
865 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000866
John McCallb1a56e72010-03-26 23:10:15 +0000867 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
868
John McCalle976ffe2009-12-14 23:19:40 +0000869 // Link the instantiation back to the pattern *unless* this is a
870 // non-definition friend declaration.
871 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000872 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000873 InstTemplate->setInstantiatedFromMemberTemplate(D);
874
John McCallb1a56e72010-03-26 23:10:15 +0000875 // Make declarations visible in the appropriate context.
876 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000877 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000878
Douglas Gregord60e1052009-08-27 16:57:43 +0000879 return InstTemplate;
880}
881
Douglas Gregord475b8d2009-03-25 21:17:03 +0000882Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
883 CXXRecordDecl *PrevDecl = 0;
884 if (D->isInjectedClassName())
885 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000886 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000887 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
888 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000889 TemplateArgs);
890 if (!Prev) return 0;
891 PrevDecl = cast<CXXRecordDecl>(Prev);
892 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000893
894 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000895 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000896 D->getLocation(), D->getIdentifier(),
897 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000898
899 // Substitute the nested name specifier, if any.
900 if (SubstQualifier(D, Record))
901 return 0;
902
Douglas Gregord475b8d2009-03-25 21:17:03 +0000903 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000904 // FIXME: Check against AS_none is an ugly hack to work around the issue that
905 // the tag decls introduced by friend class declarations don't have an access
906 // specifier. Remove once this area of the code gets sorted out.
907 if (D->getAccess() != AS_none)
908 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000909 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000910 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000911
John McCall02cace72009-08-28 07:59:38 +0000912 // If the original function was part of a friend declaration,
913 // inherit its namespace state.
914 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
915 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
916
Douglas Gregor9901c572010-05-21 00:31:19 +0000917 // Make sure that anonymous structs and unions are recorded.
918 if (D->isAnonymousStructOrUnion()) {
919 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000920 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000921 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
922 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000923
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000924 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000925 return Record;
926}
927
John McCall02cace72009-08-28 07:59:38 +0000928/// Normal class members are of more specific types and therefore
929/// don't make it here. This function serves two purposes:
930/// 1) instantiating function templates
931/// 2) substituting friend declarations
932/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000933Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000934 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000935 // Check whether there is already a function template specialization for
936 // this declaration.
937 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
938 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000939 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000940 std::pair<const TemplateArgument *, unsigned> Innermost
941 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000943 FunctionDecl *SpecFunc
944 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
945 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Douglas Gregor127102b2009-06-29 20:59:39 +0000947 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000948 if (SpecFunc)
949 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
John McCallb0cb0222010-03-27 05:57:59 +0000952 bool isFriend;
953 if (FunctionTemplate)
954 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
955 else
956 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
957
Douglas Gregor79c22782010-01-16 20:21:20 +0000958 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000959 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000960 !(isa<Decl>(Owner) &&
961 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000962 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregore53060f2009-06-25 22:08:12 +0000964 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000965 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
966 TInfo = SubstFunctionType(D, Params);
967 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000968 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000969 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000970
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000971 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
972 if (QualifierLoc) {
973 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
974 TemplateArgs);
975 if (!QualifierLoc)
976 return 0;
John McCalld325daa2010-03-26 04:53:08 +0000977 }
978
John McCall68b6b872010-02-06 01:50:47 +0000979 // If we're instantiating a local function declaration, put the result
980 // in the owner; otherwise we need to find the instantiated context.
981 DeclContext *DC;
982 if (D->getDeclContext()->isFunctionOrMethod())
983 DC = Owner;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000984 else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +0000985 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000986 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +0000987 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
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001000 if (QualifierLoc)
1001 Function->setQualifierInfo(QualifierLoc);
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
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001263 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1264 if (QualifierLoc) {
1265 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001266 TemplateArgs);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001267 if (!QualifierLoc)
1268 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001269 }
1270
1271 DeclContext *DC = Owner;
1272 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001273 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001274 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001275 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001276 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001277
1278 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1279 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001280 } else {
1281 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1282 D->getDeclContext(),
1283 TemplateArgs);
1284 }
1285 if (!DC) return 0;
1286 }
1287
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001288 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001289 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001290 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Abramo Bagnara25777432010-08-11 22:01:17 +00001292 DeclarationNameInfo NameInfo
1293 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001294 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001295 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001296 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001297 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001298 Constructor->isInlineSpecified(),
1299 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001300 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001301 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001302 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001303 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001304 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001305 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001306 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001307 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001308 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001309 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001310 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001311 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1312 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001313 D->isStatic(),
1314 D->getStorageClassAsWritten(),
1315 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001316 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001317
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001318 if (QualifierLoc)
1319 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001320
Douglas Gregord60e1052009-08-27 16:57:43 +00001321 if (TemplateParams) {
1322 // Our resulting instantiation is actually a function template, since we
1323 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001324 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001325 // template<typename T>
1326 // struct X {
1327 // template<typename U> void f(T, U);
1328 // };
1329 //
1330 // X<int> x;
1331 //
1332 // We are instantiating the member template "f" within X<int>, which means
1333 // substituting int for T, but leaving "f" as a member function template.
1334 // Build the function template itself.
1335 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1336 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001337 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001338 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001339 if (isFriend) {
1340 FunctionTemplate->setLexicalDeclContext(Owner);
1341 FunctionTemplate->setObjectOfFriendDecl(true);
1342 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001343 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001344 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001345 } else if (FunctionTemplate) {
1346 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001347 std::pair<const TemplateArgument *, unsigned> Innermost
1348 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001349 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001350 TemplateArgumentList::CreateCopy(SemaRef.Context,
1351 Innermost.first,
1352 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001353 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001354 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001355 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001356 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001357 }
1358
Mike Stump1eb44332009-09-09 15:08:12 +00001359 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001360 // out-of-line, the instantiation will have the same lexical
1361 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001362 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001363 if (NumTempParamLists)
1364 Method->setTemplateParameterListsInfo(SemaRef.Context,
1365 NumTempParamLists,
1366 TempParamLists.data());
1367
John McCallb0cb0222010-03-27 05:57:59 +00001368 Method->setLexicalDeclContext(Owner);
1369 Method->setObjectOfFriendDecl(true);
1370 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001371 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor5545e162009-03-24 00:38:23 +00001373 // Attach the parameters
1374 for (unsigned P = 0; P < Params.size(); ++P)
1375 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001376 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001377
1378 if (InitMethodInstantiation(Method, D))
1379 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001380
Abramo Bagnara25777432010-08-11 22:01:17 +00001381 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1382 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
John McCallb0cb0222010-03-27 05:57:59 +00001384 if (!FunctionTemplate || TemplateParams || isFriend) {
1385 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregordec06662009-08-21 18:42:58 +00001387 // In C++, the previous declaration we find might be a tag type
1388 // (class or enum). In this case, the new declaration will hide the
1389 // tag type. Note that this does does not apply if we're declaring a
1390 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001391 if (Previous.isSingleTagDecl())
1392 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001393 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001394
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001395 bool Redeclaration = false;
Peter Collingbournec80e8112011-01-21 02:08:54 +00001396 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001397
Douglas Gregor4ba31362009-12-01 17:24:26 +00001398 if (D->isPure())
1399 SemaRef.CheckPureMethod(Method, SourceRange());
1400
John McCall46460a62010-01-20 21:53:11 +00001401 Method->setAccess(D->getAccess());
1402
Anders Carlsson9eefa222011-01-20 06:52:44 +00001403 SemaRef.CheckOverrideControl(Method);
1404
John McCallb0cb0222010-03-27 05:57:59 +00001405 if (FunctionTemplate) {
1406 // If there's a function template, let our caller handle it.
1407 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1408 // Don't hide a (potentially) valid declaration with an invalid one.
1409 } else {
1410 NamedDecl *DeclToAdd = (TemplateParams
1411 ? cast<NamedDecl>(FunctionTemplate)
1412 : Method);
1413 if (isFriend)
1414 Record->makeDeclVisibleInContext(DeclToAdd);
1415 else
1416 Owner->addDecl(DeclToAdd);
1417 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001418
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001419 return Method;
1420}
1421
Douglas Gregor615c5d42009-03-24 16:43:20 +00001422Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001423 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001424}
1425
Douglas Gregor03b2b072009-03-24 00:15:49 +00001426Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001427 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001428}
1429
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001430Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001431 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001432}
1433
Douglas Gregor6477b692009-03-25 15:04:13 +00001434ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001435 return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001436}
1437
John McCalle29ba202009-08-20 01:44:21 +00001438Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1439 TemplateTypeParmDecl *D) {
1440 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001441 const Type* T = D->getTypeForDecl();
1442 assert(T->isTemplateTypeParmType());
1443 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001444
John McCalle29ba202009-08-20 01:44:21 +00001445 TemplateTypeParmDecl *Inst =
1446 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001447 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001448 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001449 D->wasDeclaredWithTypename(),
1450 D->isParameterPack());
1451
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001452 if (D->hasDefaultArgument())
1453 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001454
Douglas Gregor550d9b22009-10-31 17:21:17 +00001455 // Introduce this template parameter's instantiation into the instantiation
1456 // scope.
1457 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1458
John McCalle29ba202009-08-20 01:44:21 +00001459 return Inst;
1460}
1461
Douglas Gregor33642df2009-10-23 23:25:44 +00001462Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1463 NonTypeTemplateParmDecl *D) {
1464 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001465 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1466 llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1467 llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1468 bool IsExpandedParameterPack = false;
1469 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001470 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001471 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001472
1473 if (D->isExpandedParameterPack()) {
1474 // The non-type template parameter pack is an already-expanded pack
1475 // expansion of types. Substitute into each of the expanded types.
1476 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1477 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1478 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1479 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1480 TemplateArgs,
1481 D->getLocation(),
1482 D->getDeclName());
1483 if (!NewDI)
1484 return 0;
1485
1486 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1487 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1488 D->getLocation());
1489 if (NewT.isNull())
1490 return 0;
1491 ExpandedParameterPackTypes.push_back(NewT);
1492 }
1493
1494 IsExpandedParameterPack = true;
1495 DI = D->getTypeSourceInfo();
1496 T = DI->getType();
1497 } else if (isa<PackExpansionTypeLoc>(TL)) {
1498 // The non-type template parameter pack's type is a pack expansion of types.
1499 // Determine whether we need to expand this parameter pack into separate
1500 // types.
1501 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1502 TypeLoc Pattern = Expansion.getPatternLoc();
1503 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1504 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1505
1506 // Determine whether the set of unexpanded parameter packs can and should
1507 // be expanded.
1508 bool Expand = true;
1509 bool RetainExpansion = false;
1510 llvm::Optional<unsigned> OrigNumExpansions
1511 = Expansion.getTypePtr()->getNumExpansions();
1512 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1513 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1514 Pattern.getSourceRange(),
1515 Unexpanded.data(),
1516 Unexpanded.size(),
1517 TemplateArgs,
1518 Expand, RetainExpansion,
1519 NumExpansions))
1520 return 0;
1521
1522 if (Expand) {
1523 for (unsigned I = 0; I != *NumExpansions; ++I) {
1524 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1525 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1526 D->getLocation(),
1527 D->getDeclName());
1528 if (!NewDI)
1529 return 0;
1530
1531 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1532 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1533 NewDI->getType(),
1534 D->getLocation());
1535 if (NewT.isNull())
1536 return 0;
1537 ExpandedParameterPackTypes.push_back(NewT);
1538 }
1539
1540 // Note that we have an expanded parameter pack. The "type" of this
1541 // expanded parameter pack is the original expansion type, but callers
1542 // will end up using the expanded parameter pack types for type-checking.
1543 IsExpandedParameterPack = true;
1544 DI = D->getTypeSourceInfo();
1545 T = DI->getType();
1546 } else {
1547 // We cannot fully expand the pack expansion now, so substitute into the
1548 // pattern and create a new pack expansion type.
1549 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1550 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1551 D->getLocation(),
1552 D->getDeclName());
1553 if (!NewPattern)
1554 return 0;
1555
1556 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1557 NumExpansions);
1558 if (!DI)
1559 return 0;
1560
1561 T = DI->getType();
1562 }
1563 } else {
1564 // Simple case: substitution into a parameter that is not a parameter pack.
1565 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1566 D->getLocation(), D->getDeclName());
1567 if (!DI)
1568 return 0;
1569
1570 // Check that this type is acceptable for a non-type template parameter.
1571 bool Invalid = false;
1572 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1573 D->getLocation());
1574 if (T.isNull()) {
1575 T = SemaRef.Context.IntTy;
1576 Invalid = true;
1577 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001578 }
1579
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001580 NonTypeTemplateParmDecl *Param;
1581 if (IsExpandedParameterPack)
1582 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1583 D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001584 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001585 D->getPosition(),
1586 D->getIdentifier(), T,
1587 DI,
1588 ExpandedParameterPackTypes.data(),
1589 ExpandedParameterPackTypes.size(),
1590 ExpandedParameterPackTypesAsWritten.data());
1591 else
1592 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1593 D->getLocation(),
1594 D->getDepth() - TemplateArgs.getNumLevels(),
1595 D->getPosition(),
1596 D->getIdentifier(), T,
1597 D->isParameterPack(), DI);
1598
Douglas Gregor33642df2009-10-23 23:25:44 +00001599 if (Invalid)
1600 Param->setInvalidDecl();
1601
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001602 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001603
1604 // Introduce this template parameter's instantiation into the instantiation
1605 // scope.
1606 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001607 return Param;
1608}
1609
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001610Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001611TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1612 TemplateTemplateParmDecl *D) {
1613 // Instantiate the template parameter list of the template template parameter.
1614 TemplateParameterList *TempParams = D->getTemplateParameters();
1615 TemplateParameterList *InstParams;
1616 {
1617 // Perform the actual substitution of template parameters within a new,
1618 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001619 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001620 InstParams = SubstTemplateParams(TempParams);
1621 if (!InstParams)
1622 return NULL;
1623 }
1624
1625 // Build the template template parameter.
1626 TemplateTemplateParmDecl *Param
1627 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001628 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001629 D->getPosition(), D->isParameterPack(),
1630 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001631 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001632
Douglas Gregor9106ef72009-11-11 16:58:32 +00001633 // Introduce this template parameter's instantiation into the instantiation
1634 // scope.
1635 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1636
1637 return Param;
1638}
1639
Douglas Gregor48c32a72009-11-17 06:07:40 +00001640Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001641 // Using directives are never dependent (and never contain any types or
1642 // expressions), so they require no explicit instantiation work.
Douglas Gregor48c32a72009-11-17 06:07:40 +00001643
1644 UsingDirectiveDecl *Inst
1645 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1646 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001647 D->getQualifierLoc(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001648 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.
Douglas Gregor5149f372011-02-25 15:54:31 +00001665 NestedNameSpecifierLoc QualifierLoc
1666 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1667 TemplateArgs);
1668 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001669 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001670
1671 // The name info is non-dependent, so no transformation
1672 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001673 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001674
John McCall9f54ad42009-12-10 09:41:52 +00001675 // We only need to do redeclaration lookups if we're in a class
1676 // scope (in fact, it's not really even possible in non-class
1677 // scopes).
1678 bool CheckRedeclaration = Owner->isRecord();
1679
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001680 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1681 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001682
John McCalled976492009-12-04 22:46:56 +00001683 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001684 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001685 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001686 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001687 D->isTypeName());
1688
Douglas Gregor5149f372011-02-25 15:54:31 +00001689 CXXScopeSpec SS;
1690 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00001691 if (CheckRedeclaration) {
1692 Prev.setHideTags(false);
1693 SemaRef.LookupQualifiedName(Prev, Owner);
1694
1695 // Check for invalid redeclarations.
1696 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1697 D->isTypeName(), SS,
1698 D->getLocation(), Prev))
1699 NewUD->setInvalidDecl();
1700
1701 }
1702
1703 if (!NewUD->isInvalidDecl() &&
1704 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001705 D->getLocation()))
1706 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001707
John McCalled976492009-12-04 22:46:56 +00001708 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1709 NewUD->setAccess(D->getAccess());
1710 Owner->addDecl(NewUD);
1711
John McCall9f54ad42009-12-10 09:41:52 +00001712 // Don't process the shadow decls for an invalid decl.
1713 if (NewUD->isInvalidDecl())
1714 return NewUD;
1715
John McCall323c3102009-12-22 22:26:37 +00001716 bool isFunctionScope = Owner->isFunctionOrMethod();
1717
John McCall9f54ad42009-12-10 09:41:52 +00001718 // Process the shadow decls.
1719 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1720 I != E; ++I) {
1721 UsingShadowDecl *Shadow = *I;
1722 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001723 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1724 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001725 TemplateArgs));
1726
1727 if (CheckRedeclaration &&
1728 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1729 continue;
1730
1731 UsingShadowDecl *InstShadow
1732 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1733 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001734
1735 if (isFunctionScope)
1736 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001737 }
John McCalled976492009-12-04 22:46:56 +00001738
1739 return NewUD;
1740}
1741
1742Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001743 // Ignore these; we handle them in bulk when processing the UsingDecl.
1744 return 0;
John McCalled976492009-12-04 22:46:56 +00001745}
1746
John McCall7ba107a2009-11-18 02:36:19 +00001747Decl * TemplateDeclInstantiator
1748 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001749 NestedNameSpecifierLoc QualifierLoc
1750 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1751 TemplateArgs);
1752 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001753 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001755 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001756 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001758 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1759 // Hence, no tranformation is required for it.
1760 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001761 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001762 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001763 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001764 /*instantiation*/ true,
1765 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001766 if (UD)
John McCalled976492009-12-04 22:46:56 +00001767 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1768
John McCall7ba107a2009-11-18 02:36:19 +00001769 return UD;
1770}
1771
1772Decl * TemplateDeclInstantiator
1773 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001774 NestedNameSpecifierLoc QualifierLoc
1775 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
1776 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00001777 return 0;
Douglas Gregor5149f372011-02-25 15:54:31 +00001778
John McCall7ba107a2009-11-18 02:36:19 +00001779 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001780 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00001781
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001782 DeclarationNameInfo NameInfo
1783 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1784
John McCall7ba107a2009-11-18 02:36:19 +00001785 NamedDecl *UD =
1786 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001787 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001788 /*instantiation*/ true,
1789 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001790 if (UD)
John McCalled976492009-12-04 22:46:56 +00001791 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1792
Anders Carlsson0d8df782009-08-29 19:37:28 +00001793 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001794}
1795
John McCallce3ff2b2009-08-25 22:02:44 +00001796Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001797 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001798 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001799 if (D->isInvalidDecl())
1800 return 0;
1801
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001802 return Instantiator.Visit(D);
1803}
1804
John McCalle29ba202009-08-20 01:44:21 +00001805/// \brief Instantiates a nested template parameter list in the current
1806/// instantiation context.
1807///
1808/// \param L The parameter list to instantiate
1809///
1810/// \returns NULL if there was an error
1811TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001812TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001813 // Get errors for all the parameters before bailing out.
1814 bool Invalid = false;
1815
1816 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001817 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001818 ParamVector Params;
1819 Params.reserve(N);
1820 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1821 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001822 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001823 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001824 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001825 }
1826
1827 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001828 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001829 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001830
1831 TemplateParameterList *InstL
1832 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1833 L->getLAngleLoc(), &Params.front(), N,
1834 L->getRAngleLoc());
1835 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001836}
John McCalle29ba202009-08-20 01:44:21 +00001837
Douglas Gregored9c0f92009-10-29 00:04:11 +00001838/// \brief Instantiate the declaration of a class template partial
1839/// specialization.
1840///
1841/// \param ClassTemplate the (instantiated) class template that is partially
1842// specialized by the instantiation of \p PartialSpec.
1843///
1844/// \param PartialSpec the (uninstantiated) class template partial
1845/// specialization that we are instantiating.
1846///
Douglas Gregord65587f2010-11-10 19:44:59 +00001847/// \returns The instantiated partial specialization, if successful; otherwise,
1848/// NULL to indicate an error.
1849ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001850TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1851 ClassTemplateDecl *ClassTemplate,
1852 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001853 // Create a local instantiation scope for this class template partial
1854 // specialization, which will contain the instantiations of the template
1855 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001856 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001857
Douglas Gregored9c0f92009-10-29 00:04:11 +00001858 // Substitute into the template parameters of the class template partial
1859 // specialization.
1860 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1861 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1862 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001863 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001864
1865 // Substitute into the template arguments of the class template partial
1866 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00001867 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
Douglas Gregore02e2622010-12-22 21:19:48 +00001868 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1869 PartialSpec->getNumTemplateArgsAsWritten(),
1870 InstTemplateArgs, TemplateArgs))
1871 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001872
Douglas Gregored9c0f92009-10-29 00:04:11 +00001873 // Check that the template argument list is well-formed for this
1874 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001875 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001876 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1877 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001878 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001879 false,
1880 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001881 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001882
1883 // Figure out where to insert this class template partial specialization
1884 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001885 void *InsertPos = 0;
1886 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001887 = ClassTemplate->findPartialSpecialization(Converted.data(),
1888 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001889
1890 // Build the canonical type that describes the converted template
1891 // arguments of the class template partial specialization.
1892 QualType CanonType
1893 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001894 Converted.data(),
1895 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001896
1897 // Build the fully-sugared type for this class template
1898 // specialization as the user wrote in the specialization
1899 // itself. This means that we'll pretty-print the type retrieved
1900 // from the specialization's declaration the way that the user
1901 // actually wrote the specialization, rather than formatting the
1902 // name based on the "canonical" representation used to store the
1903 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001904 TypeSourceInfo *WrittenTy
1905 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1906 TemplateName(ClassTemplate),
1907 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001908 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001909 CanonType);
1910
1911 if (PrevDecl) {
1912 // We've already seen a partial specialization with the same template
1913 // parameters and template arguments. This can happen, for example, when
1914 // substituting the outer template arguments ends up causing two
1915 // class template partial specializations of a member class template
1916 // to have identical forms, e.g.,
1917 //
1918 // template<typename T, typename U>
1919 // struct Outer {
1920 // template<typename X, typename Y> struct Inner;
1921 // template<typename Y> struct Inner<T, Y>;
1922 // template<typename Y> struct Inner<U, Y>;
1923 // };
1924 //
1925 // Outer<int, int> outer; // error: the partial specializations of Inner
1926 // // have the same signature.
1927 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001928 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001929 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1930 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001931 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001932 }
1933
1934
1935 // Create the class template partial specialization declaration.
1936 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001937 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1938 PartialSpec->getTagKind(),
1939 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001940 PartialSpec->getLocation(),
1941 InstParams,
1942 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001943 Converted.data(),
1944 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001945 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001946 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001947 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001948 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001949 // Substitute the nested name specifier, if any.
1950 if (SubstQualifier(PartialSpec, InstPartialSpec))
1951 return 0;
1952
Douglas Gregored9c0f92009-10-29 00:04:11 +00001953 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001954 InstPartialSpec->setTypeAsWritten(WrittenTy);
1955
Douglas Gregored9c0f92009-10-29 00:04:11 +00001956 // Add this partial specialization to the set of class template partial
1957 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001958 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001959 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001960}
1961
John McCall21ef0fa2010-03-11 09:03:00 +00001962TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001963TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001964 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001965 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1966 assert(OldTInfo && "substituting function without type source info");
1967 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001968 TypeSourceInfo *NewTInfo
1969 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1970 D->getTypeSpecStartLoc(),
1971 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001972 if (!NewTInfo)
1973 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001974
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001975 if (NewTInfo != OldTInfo) {
1976 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001977 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001978 if (FunctionProtoTypeLoc *OldProtoLoc
1979 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001980 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001981 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1982 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001983 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
1984 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
1985 OldIdx != NumOldParams; ++OldIdx) {
1986 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
1987 if (!OldParam->isParameterPack() ||
1988 (NewIdx < NumNewParams &&
1989 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
1990 // Simple case: normal parameter, or a parameter pack that's
1991 // instantiated to a (still-dependent) parameter pack.
1992 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
1993 Params.push_back(NewParam);
1994 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
1995 NewParam);
1996 continue;
1997 }
1998
1999 // Parameter pack: make the instantiation an argument pack.
2000 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2001 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002002 unsigned NumArgumentsInExpansion
2003 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2004 TemplateArgs);
2005 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002006 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2007 Params.push_back(NewParam);
2008 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2009 NewParam);
2010 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002011 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002012 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002013 } else {
2014 // The function type itself was not dependent and therefore no
2015 // substitution occurred. However, we still need to instantiate
2016 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002017 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002018 if (FunctionProtoTypeLoc *OldProtoLoc
2019 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2020 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2021 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2022 if (!Parm)
2023 return 0;
2024 Params.push_back(Parm);
2025 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002026 }
2027 }
John McCall21ef0fa2010-03-11 09:03:00 +00002028 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002029}
2030
Mike Stump1eb44332009-09-09 15:08:12 +00002031/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002032/// declaration (New) from the corresponding fields of its template (Tmpl).
2033///
2034/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002035bool
2036TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002037 FunctionDecl *Tmpl) {
2038 if (Tmpl->isDeleted())
2039 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Douglas Gregorcca9e962009-07-01 22:01:06 +00002041 // If we are performing substituting explicitly-specified template arguments
2042 // or deduced template arguments into a function template and we reach this
2043 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002044 // to keeping the new function template specialization. We therefore
2045 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002046 // into a template instantiation for this specific function template
2047 // specialization, which is not a SFINAE context, so that we diagnose any
2048 // further errors in the declaration itself.
2049 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2050 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2051 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2052 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002053 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002054 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002055 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002056 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002057 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002058 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2059 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002060 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002061 }
2062 }
Mike Stump1eb44332009-09-09 15:08:12 +00002063
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002064 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2065 assert(Proto && "Function template without prototype?");
2066
2067 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2068 Proto->getNoReturnAttr()) {
2069 // The function has an exception specification or a "noreturn"
2070 // attribute. Substitute into each of the exception types.
2071 llvm::SmallVector<QualType, 4> Exceptions;
2072 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2073 // FIXME: Poor location information!
Douglas Gregorb99268b2010-12-21 00:52:54 +00002074 if (const PackExpansionType *PackExpansion
2075 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2076 // We have a pack expansion. Instantiate it.
2077 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2078 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2079 Unexpanded);
2080 assert(!Unexpanded.empty() &&
2081 "Pack expansion without parameter packs?");
2082
2083 bool Expand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002084 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002085 llvm::Optional<unsigned> NumExpansions
2086 = PackExpansion->getNumExpansions();
Douglas Gregorb99268b2010-12-21 00:52:54 +00002087 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2088 SourceRange(),
2089 Unexpanded.data(),
2090 Unexpanded.size(),
2091 TemplateArgs,
Douglas Gregord3731192011-01-10 07:32:04 +00002092 Expand,
2093 RetainExpansion,
2094 NumExpansions))
Douglas Gregorb99268b2010-12-21 00:52:54 +00002095 break;
2096
2097 if (!Expand) {
2098 // We can't expand this pack expansion into separate arguments yet;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002099 // just substitute into the pattern and create a new pack expansion
2100 // type.
Douglas Gregorb99268b2010-12-21 00:52:54 +00002101 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2102 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2103 TemplateArgs,
2104 New->getLocation(), New->getDeclName());
2105 if (T.isNull())
2106 break;
2107
Douglas Gregorcded4f62011-01-14 17:04:44 +00002108 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
Douglas Gregorb99268b2010-12-21 00:52:54 +00002109 Exceptions.push_back(T);
2110 continue;
2111 }
2112
2113 // Substitute into the pack expansion pattern for each template
2114 bool Invalid = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002115 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
Douglas Gregorb99268b2010-12-21 00:52:54 +00002116 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2117
2118 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2119 TemplateArgs,
2120 New->getLocation(), New->getDeclName());
2121 if (T.isNull()) {
2122 Invalid = true;
2123 break;
2124 }
2125
2126 Exceptions.push_back(T);
2127 }
2128
2129 if (Invalid)
2130 break;
2131
2132 continue;
2133 }
2134
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002135 QualType T
2136 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2137 New->getLocation(), New->getDeclName());
2138 if (T.isNull() ||
2139 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2140 continue;
2141
2142 Exceptions.push_back(T);
2143 }
2144
2145 // Rebuild the function type
2146
John McCalle23cf432010-12-14 08:05:40 +00002147 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2148 EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2149 EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2150 EPI.NumExceptions = Exceptions.size();
2151 EPI.Exceptions = Exceptions.data();
2152 EPI.ExtInfo = Proto->getExtInfo();
2153
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002154 const FunctionProtoType *NewProto
2155 = New->getType()->getAs<FunctionProtoType>();
2156 assert(NewProto && "Template instantiation without function prototype?");
2157 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2158 NewProto->arg_type_begin(),
2159 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +00002160 EPI));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002161 }
2162
John McCall1d8d1cc2010-08-01 02:01:53 +00002163 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002164
Douglas Gregore53060f2009-06-25 22:08:12 +00002165 return false;
2166}
2167
Douglas Gregor5545e162009-03-24 00:38:23 +00002168/// \brief Initializes common fields of an instantiated method
2169/// declaration (New) from the corresponding fields of its template
2170/// (Tmpl).
2171///
2172/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002173bool
2174TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002175 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002176 if (InitFunctionInstantiation(New, Tmpl))
2177 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Douglas Gregor5545e162009-03-24 00:38:23 +00002179 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002180 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002181 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002182
2183 // FIXME: attributes
2184 // FIXME: New needs a pointer to Tmpl
2185 return false;
2186}
Douglas Gregora58861f2009-05-13 20:28:22 +00002187
2188/// \brief Instantiate the definition of the given function from its
2189/// template.
2190///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002191/// \param PointOfInstantiation the point at which the instantiation was
2192/// required. Note that this is not precisely a "point of instantiation"
2193/// for the function, but it's close.
2194///
Douglas Gregora58861f2009-05-13 20:28:22 +00002195/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002196/// function template specialization or member function of a class template
2197/// specialization.
2198///
2199/// \param Recursive if true, recursively instantiates any functions that
2200/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002201///
2202/// \param DefinitionRequired if true, then we are performing an explicit
2203/// instantiation where the body of the function is required. Complain if
2204/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002205void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002206 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002207 bool Recursive,
2208 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002209 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002210 return;
2211
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002212 // Never instantiate an explicit specialization.
2213 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2214 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002215
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002216 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002217 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002218 Stmt *Pattern = 0;
2219 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002220 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002221
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002222 if (!Pattern) {
2223 if (DefinitionRequired) {
2224 if (Function->getPrimaryTemplate())
2225 Diag(PointOfInstantiation,
2226 diag::err_explicit_instantiation_undefined_func_template)
2227 << Function->getPrimaryTemplate();
2228 else
2229 Diag(PointOfInstantiation,
2230 diag::err_explicit_instantiation_undefined_member)
2231 << 1 << Function->getDeclName() << Function->getDeclContext();
2232
2233 if (PatternDecl)
2234 Diag(PatternDecl->getLocation(),
2235 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002236 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002237 } else if (Function->getTemplateSpecializationKind()
2238 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002239 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002240 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002241 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002242
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002243 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002244 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002245
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002246 // C++0x [temp.explicit]p9:
2247 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002248 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002249 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002250 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002251 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002252 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002253 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002255 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2256 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002257 return;
2258
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002259 // If we're performing recursive template instantiation, create our own
2260 // queue of pending implicit instantiations that we will instantiate later,
2261 // while we're still within our own instantiation context.
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002262 llvm::SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002263 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002264 if (Recursive) {
2265 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002266 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002267 }
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Douglas Gregor9679caf2010-05-12 17:27:19 +00002269 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002270 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002271 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002272
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002273 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002274 // recorded, unless we're actually a member function within a local
2275 // class, in which case we need to merge our results with the parent
2276 // scope (of the enclosing function).
2277 bool MergeWithParentScope = false;
2278 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2279 MergeWithParentScope = Rec->isLocalClass();
2280
2281 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002283 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002284 // instantiation scope, and set the parameter names to those used
2285 // in the template.
Douglas Gregor12c9c002011-01-07 16:43:16 +00002286 unsigned FParamIdx = 0;
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002287 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2288 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002289 if (!PatternParam->isParameterPack()) {
2290 // Simple case: not a parameter pack.
2291 assert(FParamIdx < Function->getNumParams());
2292 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2293 FunctionParam->setDeclName(PatternParam->getDeclName());
2294 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2295 ++FParamIdx;
2296 continue;
2297 }
2298
2299 // Expand the parameter pack.
2300 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2301 for (unsigned NumFParams = Function->getNumParams();
2302 FParamIdx < NumFParams;
2303 ++FParamIdx) {
2304 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2305 FunctionParam->setDeclName(PatternParam->getDeclName());
2306 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2307 }
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002308 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002309
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002310 // Enter the scope of this instantiation. We don't use
2311 // PushDeclContext because we don't have a scope.
John McCalleee1d542011-02-14 07:13:47 +00002312 Sema::ContextRAII savedContext(*this, Function);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002313
Mike Stump1eb44332009-09-09 15:08:12 +00002314 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002315 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002316
2317 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002318 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002319 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2320 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2321 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002322 }
2323
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002324 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002325 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002326
Douglas Gregor52604ab2009-09-11 21:19:12 +00002327 if (Body.isInvalid())
2328 Function->setInvalidDecl();
2329
John McCall9ae2f072010-08-23 23:25:46 +00002330 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002331 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002332
John McCall0c01d182010-03-24 05:22:00 +00002333 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2334
John McCalleee1d542011-02-14 07:13:47 +00002335 savedContext.pop();
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002336
2337 DeclGroupRef DG(Function);
2338 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002339
Douglas Gregor60406be2010-01-16 22:29:39 +00002340 // This class may have local implicit instantiations that need to be
2341 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002342 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002343 Scope.Exit();
2344
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002345 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002346 // Define any pending vtables.
2347 DefineUsedVTables();
2348
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002349 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002350 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002351 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002353 // Restore the set of pending vtables.
2354 VTableUses.swap(SavedVTableUses);
2355
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002356 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002357 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002358 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002359}
2360
2361/// \brief Instantiate the definition of the given variable from its
2362/// template.
2363///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002364/// \param PointOfInstantiation the point at which the instantiation was
2365/// required. Note that this is not precisely a "point of instantiation"
2366/// for the function, but it's close.
2367///
2368/// \param Var the already-instantiated declaration of a static member
2369/// variable of a class template specialization.
2370///
2371/// \param Recursive if true, recursively instantiates any functions that
2372/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002373///
2374/// \param DefinitionRequired if true, then we are performing an explicit
2375/// instantiation where an out-of-line definition of the member variable
2376/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002377void Sema::InstantiateStaticDataMemberDefinition(
2378 SourceLocation PointOfInstantiation,
2379 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002380 bool Recursive,
2381 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002382 if (Var->isInvalidDecl())
2383 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002384
Douglas Gregor7caa6822009-07-24 20:34:43 +00002385 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002386 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002387 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002388 assert(Def->isStaticDataMember() && "Not a static data member?");
2389 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Douglas Gregor0d035142009-10-27 18:42:08 +00002391 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002392 // We did not find an out-of-line definition of this static data member,
2393 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002394 // instantiate this definition (or provide a specialization for it) in
2395 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002396 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002397 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002398 Diag(PointOfInstantiation,
2399 diag::err_explicit_instantiation_undefined_member)
2400 << 2 << Var->getDeclName() << Var->getDeclContext();
2401 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002402 } else if (Var->getTemplateSpecializationKind()
2403 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002404 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002405 std::make_pair(Var, PointOfInstantiation));
2406 }
2407
Douglas Gregor7caa6822009-07-24 20:34:43 +00002408 return;
2409 }
2410
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002411 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002412 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002413 return;
2414
2415 // C++0x [temp.explicit]p9:
2416 // Except for inline functions, other explicit instantiation declarations
2417 // have the effect of suppressing the implicit instantiation of the entity
2418 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002419 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002420 == TSK_ExplicitInstantiationDeclaration)
2421 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Douglas Gregor7caa6822009-07-24 20:34:43 +00002423 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2424 if (Inst)
2425 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Douglas Gregor7caa6822009-07-24 20:34:43 +00002427 // If we're performing recursive template instantiation, create our own
2428 // queue of pending implicit instantiations that we will instantiate later,
2429 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002430 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002431 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002432 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Douglas Gregor7caa6822009-07-24 20:34:43 +00002434 // Enter the scope of this instantiation. We don't use
2435 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002436 ContextRAII previousContext(*this, Var->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002438 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002439 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002440 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002441
2442 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002443
2444 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002445 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2446 assert(MSInfo && "Missing member specialization information?");
2447 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2448 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002449 DeclGroupRef DG(Var);
2450 Consumer.HandleTopLevelDecl(DG);
2451 }
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Douglas Gregor7caa6822009-07-24 20:34:43 +00002453 if (Recursive) {
2454 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002455 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002456 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002457
Douglas Gregor7caa6822009-07-24 20:34:43 +00002458 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002459 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002460 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002461}
Douglas Gregor815215d2009-05-27 05:35:12 +00002462
Anders Carlsson09025312009-08-29 05:16:22 +00002463void
2464Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2465 const CXXConstructorDecl *Tmpl,
2466 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Anders Carlsson09025312009-08-29 05:16:22 +00002468 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002469 bool AnyErrors = false;
2470
Anders Carlsson09025312009-08-29 05:16:22 +00002471 // Instantiate all the initializers.
2472 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002473 InitsEnd = Tmpl->init_end();
2474 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002475 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002476
Chandler Carruth030ef472010-09-03 21:54:20 +00002477 // Only instantiate written initializers, let Sema re-construct implicit
2478 // ones.
2479 if (!Init->isWritten())
2480 continue;
2481
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002482 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002483 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002485 SourceLocation EllipsisLoc;
2486
2487 if (Init->isPackExpansion()) {
2488 // This is a pack expansion. We should expand it now.
2489 TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
2490 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2491 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2492 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002493 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002494 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002495 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
2496 BaseTL.getSourceRange(),
2497 Unexpanded.data(),
2498 Unexpanded.size(),
2499 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002500 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002501 NumExpansions)) {
2502 AnyErrors = true;
2503 New->setInvalidDecl();
2504 continue;
2505 }
2506 assert(ShouldExpand && "Partial instantiation of base initializer?");
2507
2508 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002509 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002510 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2511
2512 // Instantiate the initializer.
2513 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2514 LParenLoc, NewArgs, RParenLoc)) {
2515 AnyErrors = true;
2516 break;
2517 }
2518
2519 // Instantiate the base type.
2520 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2521 TemplateArgs,
2522 Init->getSourceLocation(),
2523 New->getDeclName());
2524 if (!BaseTInfo) {
2525 AnyErrors = true;
2526 break;
2527 }
2528
2529 // Build the initializer.
2530 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2531 BaseTInfo,
2532 (Expr **)NewArgs.data(),
2533 NewArgs.size(),
2534 Init->getLParenLoc(),
2535 Init->getRParenLoc(),
2536 New->getParent(),
2537 SourceLocation());
2538 if (NewInit.isInvalid()) {
2539 AnyErrors = true;
2540 break;
2541 }
2542
2543 NewInits.push_back(NewInit.get());
2544 NewArgs.clear();
2545 }
2546
2547 continue;
2548 }
2549
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002550 // Instantiate the initializer.
2551 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002552 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002553 AnyErrors = true;
2554 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002555 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002556
Anders Carlsson09025312009-08-29 05:16:22 +00002557 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002558 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002559 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002560 TemplateArgs,
2561 Init->getSourceLocation(),
2562 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002563 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002564 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002565 New->setInvalidDecl();
2566 continue;
2567 }
2568
John McCalla93c9342009-12-07 02:54:59 +00002569 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002570 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002571 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002572 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002573 Init->getRParenLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002574 New->getParent(),
2575 EllipsisLoc);
Anders Carlsson09025312009-08-29 05:16:22 +00002576 } else if (Init->isMemberInitializer()) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00002577 FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
2578 Init->getMemberLocation(),
2579 Init->getMember(),
2580 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002581
2582 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002583 NewArgs.size(),
2584 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002585 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002586 Init->getRParenLoc());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002587 } else if (Init->isIndirectMemberInitializer()) {
2588 IndirectFieldDecl *IndirectMember =
2589 cast<IndirectFieldDecl>(FindInstantiatedDecl(
2590 Init->getMemberLocation(),
2591 Init->getIndirectMember(), TemplateArgs));
2592
2593 NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2594 NewArgs.size(),
2595 Init->getSourceLocation(),
2596 Init->getLParenLoc(),
2597 Init->getRParenLoc());
Anders Carlsson09025312009-08-29 05:16:22 +00002598 }
2599
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002600 if (NewInit.isInvalid()) {
2601 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002602 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002603 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002604 // FIXME: It would be nice if ASTOwningVector had a release function.
2605 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Anders Carlsson09025312009-08-29 05:16:22 +00002607 NewInits.push_back((MemInitTy *)NewInit.get());
2608 }
2609 }
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Anders Carlsson09025312009-08-29 05:16:22 +00002611 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002612 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002613 /*FIXME: ColonLoc */
2614 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002615 NewInits.data(), NewInits.size(),
2616 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002617}
2618
John McCall52a575a2009-08-29 08:11:13 +00002619// TODO: this could be templated if the various decl types used the
2620// same method name.
2621static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2622 ClassTemplateDecl *Instance) {
2623 Pattern = Pattern->getCanonicalDecl();
2624
2625 do {
2626 Instance = Instance->getCanonicalDecl();
2627 if (Pattern == Instance) return true;
2628 Instance = Instance->getInstantiatedFromMemberTemplate();
2629 } while (Instance);
2630
2631 return false;
2632}
2633
Douglas Gregor0d696532009-09-28 06:34:35 +00002634static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2635 FunctionTemplateDecl *Instance) {
2636 Pattern = Pattern->getCanonicalDecl();
2637
2638 do {
2639 Instance = Instance->getCanonicalDecl();
2640 if (Pattern == Instance) return true;
2641 Instance = Instance->getInstantiatedFromMemberTemplate();
2642 } while (Instance);
2643
2644 return false;
2645}
2646
Douglas Gregored9c0f92009-10-29 00:04:11 +00002647static bool
2648isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2649 ClassTemplatePartialSpecializationDecl *Instance) {
2650 Pattern
2651 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2652 do {
2653 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2654 Instance->getCanonicalDecl());
2655 if (Pattern == Instance)
2656 return true;
2657 Instance = Instance->getInstantiatedFromMember();
2658 } while (Instance);
2659
2660 return false;
2661}
2662
John McCall52a575a2009-08-29 08:11:13 +00002663static bool isInstantiationOf(CXXRecordDecl *Pattern,
2664 CXXRecordDecl *Instance) {
2665 Pattern = Pattern->getCanonicalDecl();
2666
2667 do {
2668 Instance = Instance->getCanonicalDecl();
2669 if (Pattern == Instance) return true;
2670 Instance = Instance->getInstantiatedFromMemberClass();
2671 } while (Instance);
2672
2673 return false;
2674}
2675
2676static bool isInstantiationOf(FunctionDecl *Pattern,
2677 FunctionDecl *Instance) {
2678 Pattern = Pattern->getCanonicalDecl();
2679
2680 do {
2681 Instance = Instance->getCanonicalDecl();
2682 if (Pattern == Instance) return true;
2683 Instance = Instance->getInstantiatedFromMemberFunction();
2684 } while (Instance);
2685
2686 return false;
2687}
2688
2689static bool isInstantiationOf(EnumDecl *Pattern,
2690 EnumDecl *Instance) {
2691 Pattern = Pattern->getCanonicalDecl();
2692
2693 do {
2694 Instance = Instance->getCanonicalDecl();
2695 if (Pattern == Instance) return true;
2696 Instance = Instance->getInstantiatedFromMemberEnum();
2697 } while (Instance);
2698
2699 return false;
2700}
2701
John McCalled976492009-12-04 22:46:56 +00002702static bool isInstantiationOf(UsingShadowDecl *Pattern,
2703 UsingShadowDecl *Instance,
2704 ASTContext &C) {
2705 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2706}
2707
2708static bool isInstantiationOf(UsingDecl *Pattern,
2709 UsingDecl *Instance,
2710 ASTContext &C) {
2711 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2712}
2713
John McCall7ba107a2009-11-18 02:36:19 +00002714static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2715 UsingDecl *Instance,
2716 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002717 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002718}
2719
2720static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002721 UsingDecl *Instance,
2722 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002723 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002724}
2725
John McCall52a575a2009-08-29 08:11:13 +00002726static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2727 VarDecl *Instance) {
2728 assert(Instance->isStaticDataMember());
2729
2730 Pattern = Pattern->getCanonicalDecl();
2731
2732 do {
2733 Instance = Instance->getCanonicalDecl();
2734 if (Pattern == Instance) return true;
2735 Instance = Instance->getInstantiatedFromStaticDataMember();
2736 } while (Instance);
2737
2738 return false;
2739}
2740
John McCalled976492009-12-04 22:46:56 +00002741// Other is the prospective instantiation
2742// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002743static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002744 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002745 if (UnresolvedUsingTypenameDecl *UUD
2746 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2747 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2748 return isInstantiationOf(UUD, UD, Ctx);
2749 }
2750 }
2751
2752 if (UnresolvedUsingValueDecl *UUD
2753 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002754 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2755 return isInstantiationOf(UUD, UD, Ctx);
2756 }
2757 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002758
Anders Carlsson0d8df782009-08-29 19:37:28 +00002759 return false;
2760 }
Mike Stump1eb44332009-09-09 15:08:12 +00002761
John McCall52a575a2009-08-29 08:11:13 +00002762 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2763 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002764
John McCall52a575a2009-08-29 08:11:13 +00002765 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2766 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002767
John McCall52a575a2009-08-29 08:11:13 +00002768 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2769 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002770
Douglas Gregor7caa6822009-07-24 20:34:43 +00002771 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002772 if (Var->isStaticDataMember())
2773 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2774
2775 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2776 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002777
Douglas Gregor0d696532009-09-28 06:34:35 +00002778 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2779 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2780
Douglas Gregored9c0f92009-10-29 00:04:11 +00002781 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2782 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2783 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2784 PartialSpec);
2785
Anders Carlssond8b285f2009-09-01 04:26:58 +00002786 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2787 if (!Field->getDeclName()) {
2788 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002789 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002790 cast<FieldDecl>(D);
2791 }
2792 }
Mike Stump1eb44332009-09-09 15:08:12 +00002793
John McCalled976492009-12-04 22:46:56 +00002794 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2795 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2796
2797 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2798 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2799
Douglas Gregor815215d2009-05-27 05:35:12 +00002800 return D->getDeclName() && isa<NamedDecl>(Other) &&
2801 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2802}
2803
2804template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002805static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002806 NamedDecl *D,
2807 ForwardIterator first,
2808 ForwardIterator last) {
2809 for (; first != last; ++first)
2810 if (isInstantiationOf(Ctx, D, *first))
2811 return cast<NamedDecl>(*first);
2812
2813 return 0;
2814}
2815
John McCall02cace72009-08-28 07:59:38 +00002816/// \brief Finds the instantiation of the given declaration context
2817/// within the current instantiation.
2818///
2819/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002820DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002821 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002822 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002823 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002824 return cast_or_null<DeclContext>(ID);
2825 } else return DC;
2826}
2827
Douglas Gregored961e72009-05-27 17:54:46 +00002828/// \brief Find the instantiation of the given declaration within the
2829/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002830///
2831/// This routine is intended to be used when \p D is a declaration
2832/// referenced from within a template, that needs to mapped into the
2833/// corresponding declaration within an instantiation. For example,
2834/// given:
2835///
2836/// \code
2837/// template<typename T>
2838/// struct X {
2839/// enum Kind {
2840/// KnownValue = sizeof(T)
2841/// };
2842///
2843/// bool getKind() const { return KnownValue; }
2844/// };
2845///
2846/// template struct X<int>;
2847/// \endcode
2848///
2849/// In the instantiation of X<int>::getKind(), we need to map the
2850/// EnumConstantDecl for KnownValue (which refers to
2851/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002852/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2853/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002854NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002855 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002856 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002857 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002858 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002859 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002860 // D is a local of some kind. Look into the map of local
2861 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00002862 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
2863 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
2864 = CurrentInstantiationScope->findInstantiationOf(D);
Chris Lattnerd8e54992011-02-17 19:47:42 +00002865
Chris Lattner57ad3782011-02-17 20:34:02 +00002866 if (Found) {
2867 if (Decl *FD = Found->dyn_cast<Decl *>())
2868 return cast<NamedDecl>(FD);
2869
2870 unsigned PackIdx = ArgumentPackSubstitutionIndex;
2871 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
2872 }
2873
2874 // If we didn't find the decl, then we must have a label decl that hasn't
2875 // been found yet. Lazily instantiate it and return it now.
2876 assert(isa<LabelDecl>(D));
Chris Lattnerd8e54992011-02-17 19:47:42 +00002877
Chris Lattner57ad3782011-02-17 20:34:02 +00002878 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
2879 assert(Inst && "Failed to instantiate label??");
2880
2881 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
2882 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002883 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002884
Douglas Gregore95b4092009-09-16 18:34:49 +00002885 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2886 if (!Record->isDependentContext())
2887 return D;
2888
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002889 // If the RecordDecl is actually the injected-class-name or a
2890 // "templated" declaration for a class template, class template
2891 // partial specialization, or a member class of a class template,
2892 // substitute into the injected-class-name of the class template
2893 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002894 QualType T;
2895 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2896
2897 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002898 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002899 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2900 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002901 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002902
2903 // If we call SubstType with an InjectedClassNameType here we
2904 // can end up in an infinite loop.
2905 T = Context.getTypeDeclType(Record);
2906 assert(isa<InjectedClassNameType>(T) &&
2907 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002908 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002909 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002910
2911 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002912 // Substitute into the injected-class-name to get the type
2913 // corresponding to the instantiation we want, which may also be
2914 // the current instantiation (if we're in a template
2915 // definition). This substitution should never fail, since we
2916 // know we can instantiate the injected-class-name or we
2917 // wouldn't have gotten to the injected-class-name!
2918
2919 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2920 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002921 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2922 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2923
2924 if (!T->isDependentType()) {
2925 assert(T->isRecordType() && "Instantiation must produce a record type");
2926 return T->getAs<RecordType>()->getDecl();
2927 }
2928
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002929 // We are performing "partial" template instantiation to create
2930 // the member declarations for the members of a class template
2931 // specialization. Therefore, D is actually referring to something
2932 // in the current instantiation. Look through the current
2933 // context, which contains actual instantiations, to find the
2934 // instantiation of the "current instantiation" that D refers
2935 // to.
2936 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002937 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002938 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002939 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002940 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002941 if (isInstantiationOf(ClassTemplate,
2942 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002943 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002944
2945 if (!DC->isDependentContext())
2946 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002947 }
2948
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002949 // We're performing "instantiation" of a member of the current
2950 // instantiation while we are type-checking the
2951 // definition. Compute the declaration context and return that.
2952 assert(!SawNonDependentContext &&
2953 "No dependent context while instantiating record");
2954 DeclContext *DC = computeDeclContext(T);
2955 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002956 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002957 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002958 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002959
Douglas Gregore95b4092009-09-16 18:34:49 +00002960 // Fall through to deal with other dependent record types (e.g.,
2961 // anonymous unions in class templates).
2962 }
John McCall52a575a2009-08-29 08:11:13 +00002963
Douglas Gregore95b4092009-09-16 18:34:49 +00002964 if (!ParentDC->isDependentContext())
2965 return D;
2966
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002967 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002968 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002969 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002970
Douglas Gregor815215d2009-05-27 05:35:12 +00002971 if (ParentDC != D->getDeclContext()) {
2972 // We performed some kind of instantiation in the parent context,
2973 // so now we need to look into the instantiated parent context to
2974 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002975
John McCall3cb0ebd2010-03-10 03:28:59 +00002976 // If our context used to be dependent, we may need to instantiate
2977 // it before performing lookup into that context.
2978 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002979 if (!Spec->isDependentContext()) {
2980 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002981 const RecordType *Tag = T->getAs<RecordType>();
2982 assert(Tag && "type of non-dependent record is not a RecordType");
2983 if (!Tag->isBeingDefined() &&
2984 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2985 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002986
2987 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002988 }
2989 }
2990
Douglas Gregor815215d2009-05-27 05:35:12 +00002991 NamedDecl *Result = 0;
2992 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002993 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002994 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2995 } else {
2996 // Since we don't have a name for the entity we're looking for,
2997 // our only option is to walk through all of the declarations to
2998 // find that name. This will occur in a few cases:
2999 //
3000 // - anonymous struct/union within a template
3001 // - unnamed class/struct/union/enum within a template
3002 //
3003 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003004 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003005 ParentDC->decls_begin(),
3006 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003007 }
Mike Stump1eb44332009-09-09 15:08:12 +00003008
John McCall9f54ad42009-12-10 09:41:52 +00003009 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00003010 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
3011 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00003012 && "Unable to find instantiation of declaration!");
3013
Douglas Gregor815215d2009-05-27 05:35:12 +00003014 D = Result;
3015 }
3016
Douglas Gregor815215d2009-05-27 05:35:12 +00003017 return D;
3018}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003019
Mike Stump1eb44332009-09-09 15:08:12 +00003020/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003021/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003022void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003023 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003024 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003025 PendingImplicitInstantiation Inst;
3026
3027 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003028 Inst = PendingInstantiations.front();
3029 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003030 } else {
3031 Inst = PendingLocalImplicitInstantiations.front();
3032 PendingLocalImplicitInstantiations.pop_front();
3033 }
Mike Stump1eb44332009-09-09 15:08:12 +00003034
Douglas Gregor7caa6822009-07-24 20:34:43 +00003035 // Instantiate function definitions
3036 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003037 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3038 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003039 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3040 TSK_ExplicitInstantiationDefinition;
3041 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3042 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003043 continue;
3044 }
Mike Stump1eb44332009-09-09 15:08:12 +00003045
Douglas Gregor7caa6822009-07-24 20:34:43 +00003046 // Instantiate static data member definitions.
3047 VarDecl *Var = cast<VarDecl>(Inst.first);
3048 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003049
Chandler Carruth291b4412010-02-13 10:17:50 +00003050 // Don't try to instantiate declarations if the most recent redeclaration
3051 // is invalid.
3052 if (Var->getMostRecentDeclaration()->isInvalidDecl())
3053 continue;
3054
3055 // Check if the most recent declaration has changed the specialization kind
3056 // and removed the need for implicit instantiation.
3057 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3058 case TSK_Undeclared:
3059 assert(false && "Cannot instantitiate an undeclared specialization.");
3060 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003061 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003062 continue; // No longer need to instantiate this type.
3063 case TSK_ExplicitInstantiationDefinition:
3064 // We only need an instantiation if the pending instantiation *is* the
3065 // explicit instantiation.
3066 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003067 case TSK_ImplicitInstantiation:
3068 break;
3069 }
3070
John McCallf312b1e2010-08-26 23:41:50 +00003071 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3072 "instantiating static data member "
3073 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003074
Chandler Carruth58e390e2010-08-25 08:27:02 +00003075 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3076 TSK_ExplicitInstantiationDefinition;
3077 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3078 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003079 }
3080}
John McCall0c01d182010-03-24 05:22:00 +00003081
3082void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3083 const MultiLevelTemplateArgumentList &TemplateArgs) {
3084 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3085 E = Pattern->ddiag_end(); I != E; ++I) {
3086 DependentDiagnostic *DD = *I;
3087
3088 switch (DD->getKind()) {
3089 case DependentDiagnostic::Access:
3090 HandleDependentAccessCheck(*DD, TemplateArgs);
3091 break;
3092 }
3093 }
3094}