blob: 102ef5180899b8c099f964fb465adbf26cee1959 [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(),
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +0000123 D->getIdentifier(),
124 D->getQualifierLoc(),
John McCall3dbd3d52010-02-16 06:53:13 +0000125 D->getTargetNameLoc(),
126 D->getNamespace());
127 Owner->addDecl(Inst);
128 return Inst;
129}
130
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000131Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
132 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000133 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000134 if (DI->getType()->isDependentType() ||
135 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000136 DI = SemaRef.SubstType(DI, TemplateArgs,
137 D->getLocation(), D->getDeclName());
138 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000139 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000140 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000141 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000142 } else {
143 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000144 }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000146 // Create the new typedef
147 TypedefDecl *Typedef
Abramo Bagnara344577e2011-03-06 15:48:19 +0000148 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
149 D->getLocation(), D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000150 if (Invalid)
151 Typedef->setInvalidDecl();
152
John McCallcde5a402011-02-01 08:20:08 +0000153 // If the old typedef was the name for linkage purposes of an anonymous
154 // tag decl, re-establish that relationship for the new typedef.
155 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
156 TagDecl *oldTag = oldTagType->getDecl();
157 if (oldTag->getTypedefForAnonDecl() == D) {
158 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
159 assert(!newTag->getIdentifier() && !newTag->getTypedefForAnonDecl());
160 newTag->setTypedefForAnonDecl(Typedef);
161 }
Douglas Gregord57a38e2010-04-23 16:25:07 +0000162 }
163
John McCall5126fd02009-12-30 00:31:22 +0000164 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000165 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
166 TemplateArgs);
Douglas Gregorb7107222011-03-04 19:46:35 +0000167 if (!InstPrev)
168 return 0;
169
John McCall5126fd02009-12-30 00:31:22 +0000170 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
171 }
172
John McCall1d8d1cc2010-08-01 02:01:53 +0000173 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000174
John McCall46460a62010-01-20 21:53:11 +0000175 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000176 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000178 return Typedef;
179}
180
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000181/// \brief Instantiate an initializer, breaking it into separate
182/// initialization arguments.
183///
184/// \param S The semantic analysis object.
185///
186/// \param Init The initializer to instantiate.
187///
188/// \param TemplateArgs Template arguments to be substituted into the
189/// initializer.
190///
191/// \param NewArgs Will be filled in with the instantiation arguments.
192///
193/// \returns true if an error occurred, false otherwise
194static bool InstantiateInitializer(Sema &S, Expr *Init,
195 const MultiLevelTemplateArgumentList &TemplateArgs,
196 SourceLocation &LParenLoc,
Nick Lewycky7663f392010-11-20 01:29:55 +0000197 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000198 SourceLocation &RParenLoc) {
199 NewArgs.clear();
200 LParenLoc = SourceLocation();
201 RParenLoc = SourceLocation();
202
203 if (!Init)
204 return false;
205
John McCall4765fa02010-12-06 08:20:24 +0000206 if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000207 Init = ExprTemp->getSubExpr();
208
209 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
210 Init = Binder->getSubExpr();
211
212 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
213 Init = ICE->getSubExprAsWritten();
214
215 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
216 LParenLoc = ParenList->getLParenLoc();
217 RParenLoc = ParenList->getRParenLoc();
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000218 return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
219 true, TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000220 }
221
222 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000223 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000224 if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
225 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000226 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000227
Douglas Gregor28329e52010-03-24 21:22:47 +0000228 // FIXME: Fake locations!
229 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000230 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000231 return false;
232 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000233 }
234
John McCall60d7b3a2010-08-24 06:29:42 +0000235 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000236 if (Result.isInvalid())
237 return true;
238
239 NewArgs.push_back(Result.takeAs<Expr>());
240 return false;
241}
242
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000243Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000244 // If this is the variable for an anonymous struct or union,
245 // instantiate the anonymous struct/union type first.
246 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
247 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
248 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
249 return 0;
250
John McCallce3ff2b2009-08-25 22:02:44 +0000251 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000252 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000253 TemplateArgs,
254 D->getTypeSpecStartLoc(),
255 D->getDeclName());
256 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000257 return 0;
258
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000259 if (DI->getType()->isFunctionType()) {
260 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
261 << D->isStaticDataMember() << DI->getType();
262 return 0;
263 }
264
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000265 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000266 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
267 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000268 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000269 D->getStorageClass(),
270 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000271 Var->setThreadSpecified(D->isThreadSpecified());
272 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000273
John McCallb6217662010-03-15 10:12:16 +0000274 // Substitute the nested name specifier, if any.
275 if (SubstQualifier(D, Var))
276 return 0;
277
Mike Stump1eb44332009-09-09 15:08:12 +0000278 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000279 // out-of-line, the instantiation will have the same lexical
280 // context (which will be a namespace scope) as the template.
281 if (D->isOutOfLine())
282 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000283
John McCall46460a62010-01-20 21:53:11 +0000284 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000285
286 if (!D->isStaticDataMember())
287 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000288
Mike Stump390b4cc2009-05-16 07:39:55 +0000289 // FIXME: In theory, we could have a previous declaration for variables that
290 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000291 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000292 // FIXME: having to fake up a LookupResult is dumb.
293 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000294 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000295 if (D->isStaticDataMember())
296 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000297 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregor7caa6822009-07-24 20:34:43 +0000299 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000300 if (!D->isStaticDataMember())
301 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000302 Owner->makeDeclVisibleInContext(Var);
303 } else {
304 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000305 if (Owner->isFunctionOrMethod())
306 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000307 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000308 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000309
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000310 // Link instantiations of static data members back to the template from
311 // which they were instantiated.
312 if (Var->isStaticDataMember())
313 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000314 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000315
Douglas Gregor60c93c92010-02-09 07:26:29 +0000316 if (Var->getAnyInitializer()) {
317 // We already have an initializer in the class.
318 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000319 if (Var->isStaticDataMember() && !D->isOutOfLine())
320 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
321 else
322 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
323
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000324 // Instantiate the initializer.
325 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000326 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000327 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000328 InitArgs, RParenLoc)) {
Richard Smith34b41d92011-02-20 03:19:35 +0000329 bool TypeMayContainAuto = true;
Douglas Gregor07a77b42011-01-14 17:12:22 +0000330 // Attach the initializer to the declaration, if we have one.
331 if (InitArgs.size() == 0)
Richard Smith34b41d92011-02-20 03:19:35 +0000332 SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000333 else if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000334 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000335 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000336 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000337 move_arg(InitArgs),
Richard Smith34b41d92011-02-20 03:19:35 +0000338 RParenLoc,
339 TypeMayContainAuto);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000340 } else {
341 assert(InitArgs.size() == 1);
John McCall9ae2f072010-08-23 23:25:46 +0000342 Expr *Init = InitArgs.take()[0];
Richard Smith34b41d92011-02-20 03:19:35 +0000343 SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000344 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000345 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000346 // FIXME: Not too happy about invalidating the declaration
347 // because of a bogus initializer.
348 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000349 }
350
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000351 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000352 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000353 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000354
Douglas Gregor5764f612010-05-08 23:05:03 +0000355 // Diagnose unused local variables.
356 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
357 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000358
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000359 return Var;
360}
361
Abramo Bagnara6206d532010-06-05 05:09:32 +0000362Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
363 AccessSpecDecl* AD
364 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
365 D->getAccessSpecifierLoc(), D->getColonLoc());
366 Owner->addHiddenDecl(AD);
367 return AD;
368}
369
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000370Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
371 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000372 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000373 if (DI->getType()->isDependentType() ||
374 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000375 DI = SemaRef.SubstType(DI, TemplateArgs,
376 D->getLocation(), D->getDeclName());
377 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000378 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000379 Invalid = true;
380 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000381 // C++ [temp.arg.type]p3:
382 // If a declaration acquires a function type through a type
383 // dependent on a template-parameter and this causes a
384 // declaration that does not use the syntactic form of a
385 // function declarator to have function type, the program is
386 // ill-formed.
387 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000388 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000389 Invalid = true;
390 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000391 } else {
392 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000393 }
394
395 Expr *BitWidth = D->getBitWidth();
396 if (Invalid)
397 BitWidth = 0;
398 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000399 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000400 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000401
John McCall60d7b3a2010-08-24 06:29:42 +0000402 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000403 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000404 if (InstantiatedBitWidth.isInvalid()) {
405 Invalid = true;
406 BitWidth = 0;
407 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000408 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000409 }
410
John McCall07fb6be2009-10-22 23:33:21 +0000411 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
412 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000413 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000414 D->getLocation(),
415 D->isMutable(),
416 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000417 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000418 D->getAccess(),
419 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000420 if (!Field) {
421 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000422 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
John McCall1d8d1cc2010-08-01 02:01:53 +0000425 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000426
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000427 if (Invalid)
428 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000430 if (!Field->getDeclName()) {
431 // Keep track of where this decl came from.
432 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000433 }
434 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
435 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000436 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000437 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000440 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000441 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000442 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000443
444 return Field;
445}
446
Francois Pichet87c2e122010-11-21 06:08:52 +0000447Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
448 NamedDecl **NamedChain =
449 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
450
451 int i = 0;
452 for (IndirectFieldDecl::chain_iterator PI =
453 D->chain_begin(), PE = D->chain_end();
Douglas Gregorb7107222011-03-04 19:46:35 +0000454 PI != PE; ++PI) {
455 NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
456 TemplateArgs);
457 if (!Next)
458 return 0;
459
460 NamedChain[i++] = Next;
461 }
Francois Pichet87c2e122010-11-21 06:08:52 +0000462
Francois Pichet40e17752010-12-09 10:07:54 +0000463 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000464 IndirectFieldDecl* IndirectField
465 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000466 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000467 NamedChain, D->getChainingSize());
468
469
470 IndirectField->setImplicit(D->isImplicit());
471 IndirectField->setAccess(D->getAccess());
472 Owner->addDecl(IndirectField);
473 return IndirectField;
474}
475
John McCall02cace72009-08-28 07:59:38 +0000476Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000477 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000478 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000479 if (TypeSourceInfo *Ty = D->getFriendType()) {
480 TypeSourceInfo *InstTy =
481 SemaRef.SubstType(Ty, TemplateArgs,
482 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000483 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000484 return 0;
John McCall02cace72009-08-28 07:59:38 +0000485
Douglas Gregor06245bf2010-04-07 17:57:12 +0000486 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
487 if (!FD)
488 return 0;
489
490 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000491 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000492 Owner->addDecl(FD);
493 return FD;
494 }
495
496 NamedDecl *ND = D->getFriendDecl();
497 assert(ND && "friend decl must be a decl or a type!");
498
John McCallaf2094e2010-04-08 09:05:18 +0000499 // All of the Visit implementations for the various potential friend
500 // declarations have to be carefully written to work for friend
501 // objects, with the most important detail being that the target
502 // decl should almost certainly not be placed in Owner.
503 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000504 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
John McCall02cace72009-08-28 07:59:38 +0000506 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000507 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
508 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000509 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000510 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000511 Owner->addDecl(FD);
512 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000513}
514
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000515Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
516 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregorac7610d2009-06-22 20:57:11 +0000518 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000519 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
John McCall60d7b3a2010-08-24 06:29:42 +0000521 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000522 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000523 if (InstantiatedAssertExpr.isInvalid())
524 return 0;
525
John McCall60d7b3a2010-08-24 06:29:42 +0000526 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000527 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000528 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000529 InstantiatedAssertExpr.get(),
530 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000531}
532
533Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000534 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000535 D->getLocation(), D->getIdentifier(),
Abramo Bagnaraa868c372011-03-06 16:09:14 +0000536 D->getLocStart(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000537 /*PrevDecl=*/0, D->isScoped(),
538 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000539 if (D->isFixed()) {
540 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
541 // If we have type source information for the underlying type, it means it
542 // has been explicitly set by the user. Perform substitution on it before
543 // moving on.
544 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
545 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
546 TemplateArgs,
547 UnderlyingLoc,
548 DeclarationName()));
549
550 if (!Enum->getIntegerTypeSourceInfo())
551 Enum->setIntegerType(SemaRef.Context.IntTy);
552 }
553 else {
554 assert(!D->getIntegerType()->isDependentType()
555 && "Dependent type without type source info");
556 Enum->setIntegerType(D->getIntegerType());
557 }
558 }
559
John McCall5b629aa2010-10-22 23:36:17 +0000560 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
561
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000562 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000563 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000564 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000565 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000566 Enum->startDefinition();
567
Douglas Gregor96084f12010-03-01 19:00:07 +0000568 if (D->getDeclContext()->isFunctionOrMethod())
569 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
570
John McCalld226f652010-08-21 09:40:31 +0000571 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000572
573 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000574 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
575 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000576 EC != ECEnd; ++EC) {
577 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000578 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000579 if (Expr *UninstValue = EC->getInitExpr()) {
580 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000581 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000582 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
John McCallce3ff2b2009-08-25 22:02:44 +0000584 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000585 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000586
587 // Drop the initial value and continue.
588 bool isInvalid = false;
589 if (Value.isInvalid()) {
590 Value = SemaRef.Owned((Expr *)0);
591 isInvalid = true;
592 }
593
Mike Stump1eb44332009-09-09 15:08:12 +0000594 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000595 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
596 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000597 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000598
599 if (isInvalid) {
600 if (EnumConst)
601 EnumConst->setInvalidDecl();
602 Enum->setInvalidDecl();
603 }
604
605 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000606 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
607
John McCall3b85ecf2010-01-23 22:37:59 +0000608 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000609 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000610 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000611 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000612
613 if (D->getDeclContext()->isFunctionOrMethod()) {
614 // If the enumeration is within a function or method, record the enum
615 // constant as a local.
616 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
617 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000618 }
619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000621 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000622 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000623 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000624 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000625 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000626 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000627
628 return Enum;
629}
630
Douglas Gregor6477b692009-03-25 15:04:13 +0000631Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
632 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
633 return 0;
634}
635
John McCalle29ba202009-08-20 01:44:21 +0000636Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000637 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
638
Douglas Gregor550d9b22009-10-31 17:21:17 +0000639 // Create a local instantiation scope for this class template, which
640 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000641 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000642 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000643 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000644 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000645 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000646
647 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000648
649 // Instantiate the qualifier. We have to do this first in case
650 // we're a friend declaration, because if we are then we need to put
651 // the new declaration in the appropriate context.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000652 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
653 if (QualifierLoc) {
654 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
655 TemplateArgs);
656 if (!QualifierLoc)
657 return 0;
John McCall93ba8572010-03-25 06:39:04 +0000658 }
659
660 CXXRecordDecl *PrevDecl = 0;
661 ClassTemplateDecl *PrevClassTemplate = 0;
662
Nick Lewycky37574f52010-11-08 23:29:42 +0000663 if (!isFriend && Pattern->getPreviousDeclaration()) {
664 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
665 if (Found.first != Found.second) {
666 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
667 if (PrevClassTemplate)
668 PrevDecl = PrevClassTemplate->getTemplatedDecl();
669 }
670 }
671
John McCall93ba8572010-03-25 06:39:04 +0000672 // If this isn't a friend, then it's a member template, in which
673 // case we just want to build the instantiation in the
674 // specialization. If it is a friend, we want to build it in
675 // the appropriate context.
676 DeclContext *DC = Owner;
677 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000678 if (QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000679 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000680 SS.Adopt(QualifierLoc);
John McCall93ba8572010-03-25 06:39:04 +0000681 DC = SemaRef.computeDeclContext(SS);
682 if (!DC) return 0;
683 } else {
684 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
685 Pattern->getDeclContext(),
686 TemplateArgs);
687 }
688
689 // Look for a previous declaration of the template in the owning
690 // context.
691 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
692 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
693 SemaRef.LookupQualifiedName(R, DC);
694
695 if (R.isSingleResult()) {
696 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
697 if (PrevClassTemplate)
698 PrevDecl = PrevClassTemplate->getTemplatedDecl();
699 }
700
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000701 if (!PrevClassTemplate && QualifierLoc) {
John McCall93ba8572010-03-25 06:39:04 +0000702 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000703 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000704 << QualifierLoc.getSourceRange();
John McCall93ba8572010-03-25 06:39:04 +0000705 return 0;
706 }
707
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000708 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000709 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000710 bool Complain = true;
711
712 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
713 // template for struct std::tr1::__detail::_Map_base, where the
714 // template parameters of the friend declaration don't match the
715 // template parameters of the original declaration. In this one
716 // case, we don't complain about the ill-formed friend
717 // declaration.
718 if (isFriend && Pattern->getIdentifier() &&
719 Pattern->getIdentifier()->isStr("_Map_base") &&
720 DC->isNamespace() &&
721 cast<NamespaceDecl>(DC)->getIdentifier() &&
722 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
723 DeclContext *DCParent = DC->getParent();
724 if (DCParent->isNamespace() &&
725 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
726 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
727 DeclContext *DCParent2 = DCParent->getParent();
728 if (DCParent2->isNamespace() &&
729 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
730 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
731 DCParent2->getParent()->isTranslationUnit())
732 Complain = false;
733 }
734 }
735
John McCall93ba8572010-03-25 06:39:04 +0000736 TemplateParameterList *PrevParams
737 = PrevClassTemplate->getTemplateParameters();
738
739 // Make sure the parameter lists match.
740 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000741 Complain,
742 Sema::TPL_TemplateMatch)) {
743 if (Complain)
744 return 0;
745
746 AdoptedPreviousTemplateParams = true;
747 InstParams = PrevParams;
748 }
John McCall93ba8572010-03-25 06:39:04 +0000749
750 // Do some additional validation, then merge default arguments
751 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000752 if (!AdoptedPreviousTemplateParams &&
753 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000754 Sema::TPC_ClassTemplate))
755 return 0;
756 }
757 }
758
John McCalle29ba202009-08-20 01:44:21 +0000759 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000760 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000761 Pattern->getLocation(), Pattern->getIdentifier(),
Abramo Bagnaraa868c372011-03-06 16:09:14 +0000762 Pattern->getLocStart(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000763 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000764
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000765 if (QualifierLoc)
766 RecordInst->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +0000767
John McCalle29ba202009-08-20 01:44:21 +0000768 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000769 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
770 D->getIdentifier(), InstParams, RecordInst,
771 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000772 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000773
John McCall93ba8572010-03-25 06:39:04 +0000774 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000775 if (PrevClassTemplate)
776 Inst->setAccess(PrevClassTemplate->getAccess());
777 else
778 Inst->setAccess(D->getAccess());
779
John McCall93ba8572010-03-25 06:39:04 +0000780 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
781 // TODO: do we want to track the instantiation progeny of this
782 // friend target decl?
783 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000784 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000785 if (!PrevClassTemplate)
786 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000787 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000788
789 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000790 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000791 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000792
Douglas Gregor259571e2009-10-30 22:42:42 +0000793 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000794 if (isFriend) {
795 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000796 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000797 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000798
John McCalle29ba202009-08-20 01:44:21 +0000799 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000800
801 if (!PrevClassTemplate) {
802 // Queue up any out-of-line partial specializations of this member
803 // class template; the client will force their instantiation once
804 // the enclosing class has been instantiated.
805 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
806 D->getPartialSpecializations(PartialSpecs);
807 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
808 if (PartialSpecs[I]->isOutOfLine())
809 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
810 }
811
John McCalle29ba202009-08-20 01:44:21 +0000812 return Inst;
813}
814
Douglas Gregord60e1052009-08-27 16:57:43 +0000815Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000816TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
817 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000818 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
819
820 // Lookup the already-instantiated declaration in the instantiation
821 // of the class template and return that.
822 DeclContext::lookup_result Found
823 = Owner->lookup(ClassTemplate->getDeclName());
824 if (Found.first == Found.second)
825 return 0;
826
827 ClassTemplateDecl *InstClassTemplate
828 = dyn_cast<ClassTemplateDecl>(*Found.first);
829 if (!InstClassTemplate)
830 return 0;
831
Douglas Gregord65587f2010-11-10 19:44:59 +0000832 if (ClassTemplatePartialSpecializationDecl *Result
833 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
834 return Result;
835
836 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000837}
838
839Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000840TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000841 // Create a local instantiation scope for this function template, which
842 // will contain the instantiations of the template parameters and then get
843 // merged with the local instantiation scope for the function template
844 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000845 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000846
Douglas Gregord60e1052009-08-27 16:57:43 +0000847 TemplateParameterList *TempParams = D->getTemplateParameters();
848 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000849 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000850 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000851
Douglas Gregora735b202009-10-13 14:39:41 +0000852 FunctionDecl *Instantiated = 0;
853 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
854 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
855 InstParams));
856 else
857 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
858 D->getTemplatedDecl(),
859 InstParams));
860
861 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000862 return 0;
863
John McCall46460a62010-01-20 21:53:11 +0000864 Instantiated->setAccess(D->getAccess());
865
Mike Stump1eb44332009-09-09 15:08:12 +0000866 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000867 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000868 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000869 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000870 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000871 assert(InstTemplate &&
872 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000873
John McCallb1a56e72010-03-26 23:10:15 +0000874 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
875
John McCalle976ffe2009-12-14 23:19:40 +0000876 // Link the instantiation back to the pattern *unless* this is a
877 // non-definition friend declaration.
878 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000879 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000880 InstTemplate->setInstantiatedFromMemberTemplate(D);
881
John McCallb1a56e72010-03-26 23:10:15 +0000882 // Make declarations visible in the appropriate context.
883 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000884 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000885
Douglas Gregord60e1052009-08-27 16:57:43 +0000886 return InstTemplate;
887}
888
Douglas Gregord475b8d2009-03-25 21:17:03 +0000889Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
890 CXXRecordDecl *PrevDecl = 0;
891 if (D->isInjectedClassName())
892 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000893 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000894 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
895 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000896 TemplateArgs);
897 if (!Prev) return 0;
898 PrevDecl = cast<CXXRecordDecl>(Prev);
899 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000900
901 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000902 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000903 D->getLocation(), D->getIdentifier(),
Abramo Bagnaraa868c372011-03-06 16:09:14 +0000904 D->getLocStart(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000905
906 // Substitute the nested name specifier, if any.
907 if (SubstQualifier(D, Record))
908 return 0;
909
Douglas Gregord475b8d2009-03-25 21:17:03 +0000910 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000911 // FIXME: Check against AS_none is an ugly hack to work around the issue that
912 // the tag decls introduced by friend class declarations don't have an access
913 // specifier. Remove once this area of the code gets sorted out.
914 if (D->getAccess() != AS_none)
915 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000916 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000917 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000918
John McCall02cace72009-08-28 07:59:38 +0000919 // If the original function was part of a friend declaration,
920 // inherit its namespace state.
921 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
922 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
923
Douglas Gregor9901c572010-05-21 00:31:19 +0000924 // Make sure that anonymous structs and unions are recorded.
925 if (D->isAnonymousStructOrUnion()) {
926 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000927 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000928 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
929 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000930
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000931 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000932 return Record;
933}
934
John McCall02cace72009-08-28 07:59:38 +0000935/// Normal class members are of more specific types and therefore
936/// don't make it here. This function serves two purposes:
937/// 1) instantiating function templates
938/// 2) substituting friend declarations
939/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000940Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000941 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000942 // Check whether there is already a function template specialization for
943 // this declaration.
944 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
945 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000946 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000947 std::pair<const TemplateArgument *, unsigned> Innermost
948 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000950 FunctionDecl *SpecFunc
951 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
952 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Douglas Gregor127102b2009-06-29 20:59:39 +0000954 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000955 if (SpecFunc)
956 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
John McCallb0cb0222010-03-27 05:57:59 +0000959 bool isFriend;
960 if (FunctionTemplate)
961 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
962 else
963 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
964
Douglas Gregor79c22782010-01-16 20:21:20 +0000965 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000966 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000967 !(isa<Decl>(Owner) &&
968 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000969 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Douglas Gregore53060f2009-06-25 22:08:12 +0000971 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000972 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
973 TInfo = SubstFunctionType(D, Params);
974 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000975 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000976 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000977
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000978 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
979 if (QualifierLoc) {
980 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
981 TemplateArgs);
982 if (!QualifierLoc)
983 return 0;
John McCalld325daa2010-03-26 04:53:08 +0000984 }
985
John McCall68b6b872010-02-06 01:50:47 +0000986 // If we're instantiating a local function declaration, put the result
987 // in the owner; otherwise we need to find the instantiated context.
988 DeclContext *DC;
989 if (D->getDeclContext()->isFunctionOrMethod())
990 DC = Owner;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000991 else if (isFriend && QualifierLoc) {
John McCalld325daa2010-03-26 04:53:08 +0000992 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000993 SS.Adopt(QualifierLoc);
John McCalld325daa2010-03-26 04:53:08 +0000994 DC = SemaRef.computeDeclContext(SS);
995 if (!DC) return 0;
996 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000997 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
998 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000999 }
John McCall68b6b872010-02-06 01:50:47 +00001000
John McCall02cace72009-08-28 07:59:38 +00001001 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001002 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001003 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001004 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001005 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001006
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001007 if (QualifierLoc)
1008 Function->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001009
John McCallb1a56e72010-03-26 23:10:15 +00001010 DeclContext *LexicalDC = Owner;
1011 if (!isFriend && D->isOutOfLine()) {
1012 assert(D->getDeclContext()->isFileContext());
1013 LexicalDC = D->getDeclContext();
1014 }
1015
1016 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregore53060f2009-06-25 22:08:12 +00001018 // Attach the parameters
1019 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001020 if (Params[P])
1021 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001022 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001023
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001024 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001025 if (TemplateParams) {
1026 // Our resulting instantiation is actually a function template, since we
1027 // are substituting only the outer template parameters. For example, given
1028 //
1029 // template<typename T>
1030 // struct X {
1031 // template<typename U> friend void f(T, U);
1032 // };
1033 //
1034 // X<int> x;
1035 //
1036 // We are instantiating the friend function template "f" within X<int>,
1037 // which means substituting int for T, but leaving "f" as a friend function
1038 // template.
1039 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001040 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001041 Function->getLocation(),
1042 Function->getDeclName(),
1043 TemplateParams, Function);
1044 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001045
1046 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001047
1048 if (isFriend && D->isThisDeclarationADefinition()) {
1049 // TODO: should we remember this connection regardless of whether
1050 // the friend declaration provided a body?
1051 FunctionTemplate->setInstantiatedFromMemberTemplate(
1052 D->getDescribedFunctionTemplate());
1053 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001054 } else if (FunctionTemplate) {
1055 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001056 std::pair<const TemplateArgument *, unsigned> Innermost
1057 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001058 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001059 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001060 Innermost.first,
1061 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001062 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001063 } else if (isFriend && D->isThisDeclarationADefinition()) {
1064 // TODO: should we remember this connection regardless of whether
1065 // the friend declaration provided a body?
1066 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001067 }
Douglas Gregora735b202009-10-13 14:39:41 +00001068
Douglas Gregore53060f2009-06-25 22:08:12 +00001069 if (InitFunctionInstantiation(Function, D))
1070 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregore53060f2009-06-25 22:08:12 +00001072 bool Redeclaration = false;
John McCallaf2094e2010-04-08 09:05:18 +00001073 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001074
John McCall68263142009-11-18 22:49:29 +00001075 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1076 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1077
John McCallaf2094e2010-04-08 09:05:18 +00001078 if (DependentFunctionTemplateSpecializationInfo *Info
1079 = D->getDependentSpecializationInfo()) {
1080 assert(isFriend && "non-friend has dependent specialization info?");
1081
1082 // This needs to be set now for future sanity.
1083 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1084
1085 // Instantiate the explicit template arguments.
1086 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1087 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001088 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1089 ExplicitArgs, TemplateArgs))
1090 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001091
1092 // Map the candidate templates to their instantiations.
1093 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1094 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1095 Info->getTemplate(I),
1096 TemplateArgs);
1097 if (!Temp) return 0;
1098
1099 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1100 }
1101
1102 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1103 &ExplicitArgs,
1104 Previous))
1105 Function->setInvalidDecl();
1106
1107 isExplicitSpecialization = true;
1108
1109 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001110 // Look only into the namespace where the friend would be declared to
1111 // find a previous declaration. This is the innermost enclosing namespace,
1112 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001113 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001114
Douglas Gregora735b202009-10-13 14:39:41 +00001115 // In C++, the previous declaration we find might be a tag type
1116 // (class or enum). In this case, the new declaration will hide the
1117 // tag type. Note that this does does not apply if we're declaring a
1118 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001119 if (Previous.isSingleTagDecl())
1120 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001121 }
1122
John McCall9f54ad42009-12-10 09:41:52 +00001123 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
Peter Collingbournec80e8112011-01-21 02:08:54 +00001124 isExplicitSpecialization, Redeclaration);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001125
John McCall76d32642010-04-24 01:30:58 +00001126 NamedDecl *PrincipalDecl = (TemplateParams
1127 ? cast<NamedDecl>(FunctionTemplate)
1128 : Function);
1129
Douglas Gregora735b202009-10-13 14:39:41 +00001130 // If the original function was part of a friend declaration,
1131 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001132 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001133 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001134 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001135 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001136 else
Douglas Gregora735b202009-10-13 14:39:41 +00001137 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001138
1139 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1140 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001141
Gabor Greif77535df2010-08-30 22:25:56 +00001142 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001143
Douglas Gregor238058c2010-05-18 05:45:02 +00001144 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1145 D->isThisDeclarationADefinition()) {
1146 // Check for a function body.
1147 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001148 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001149 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1150 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1151 << Function->getDeclName();
1152 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1153 Function->setInvalidDecl();
1154 }
1155 // Check for redefinitions due to other instantiations of this or
1156 // a similar friend function.
1157 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1158 REnd = Function->redecls_end();
1159 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001160 if (*R == Function)
1161 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001162 switch (R->getFriendObjectKind()) {
1163 case Decl::FOK_None:
1164 if (!queuedInstantiation && R->isUsed(false)) {
1165 if (MemberSpecializationInfo *MSInfo
1166 = Function->getMemberSpecializationInfo()) {
1167 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1168 SourceLocation Loc = R->getLocation(); // FIXME
1169 MSInfo->setPointOfInstantiation(Loc);
1170 SemaRef.PendingLocalImplicitInstantiations.push_back(
1171 std::make_pair(Function, Loc));
1172 queuedInstantiation = true;
1173 }
1174 }
1175 }
1176 break;
1177 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001178 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001179 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001180 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001181 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1182 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001183 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001184 Function->setInvalidDecl();
1185 break;
1186 }
1187 }
1188 }
1189 }
Douglas Gregora735b202009-10-13 14:39:41 +00001190 }
1191
John McCall76d32642010-04-24 01:30:58 +00001192 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1193 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1194 PrincipalDecl->setNonMemberOperator();
1195
Douglas Gregore53060f2009-06-25 22:08:12 +00001196 return Function;
1197}
1198
Douglas Gregord60e1052009-08-27 16:57:43 +00001199Decl *
1200TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1201 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001202 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1203 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001204 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001205 // We are creating a function template specialization from a function
1206 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001207 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001208 std::pair<const TemplateArgument *, unsigned> Innermost
1209 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001211 FunctionDecl *SpecFunc
1212 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1213 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Douglas Gregor6b906862009-08-21 00:16:32 +00001215 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001216 if (SpecFunc)
1217 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001218 }
1219
John McCallb0cb0222010-03-27 05:57:59 +00001220 bool isFriend;
1221 if (FunctionTemplate)
1222 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1223 else
1224 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1225
Douglas Gregor79c22782010-01-16 20:21:20 +00001226 bool MergeWithParentScope = (TemplateParams != 0) ||
1227 !(isa<Decl>(Owner) &&
1228 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001229 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001230
John McCall4eab39f2010-10-19 02:26:41 +00001231 // Instantiate enclosing template arguments for friends.
1232 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1233 unsigned NumTempParamLists = 0;
1234 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1235 TempParamLists.set_size(NumTempParamLists);
1236 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1237 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1238 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1239 if (!InstParams)
1240 return NULL;
1241 TempParamLists[I] = InstParams;
1242 }
1243 }
1244
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001245 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001246 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1247 TInfo = SubstFunctionType(D, Params);
1248 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001249 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001250 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001251
Abramo Bagnara723df242010-12-14 22:11:44 +00001252 // \brief If the type of this function, after ignoring parentheses,
1253 // is not *directly* a function type, then we're instantiating a function
1254 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001255 //
1256 // typedef int functype(int, int);
1257 // functype func;
1258 //
1259 // In this case, we'll just go instantiate the ParmVarDecls that we
1260 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001261 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001262 assert(!Params.size() && "Instantiating type could not yield parameters");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001263 llvm::SmallVector<QualType, 4> ParamTypes;
1264 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1265 D->getNumParams(), TemplateArgs, ParamTypes,
1266 &Params))
1267 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001268 }
1269
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001270 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1271 if (QualifierLoc) {
1272 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
John McCallb0cb0222010-03-27 05:57:59 +00001273 TemplateArgs);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001274 if (!QualifierLoc)
1275 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001276 }
1277
1278 DeclContext *DC = Owner;
1279 if (isFriend) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001280 if (QualifierLoc) {
John McCallb0cb0222010-03-27 05:57:59 +00001281 CXXScopeSpec SS;
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001282 SS.Adopt(QualifierLoc);
John McCallb0cb0222010-03-27 05:57:59 +00001283 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001284
1285 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1286 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001287 } else {
1288 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1289 D->getDeclContext(),
1290 TemplateArgs);
1291 }
1292 if (!DC) return 0;
1293 }
1294
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001295 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001296 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001297 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Abramo Bagnara25777432010-08-11 22:01:17 +00001299 DeclarationNameInfo NameInfo
1300 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001301 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001302 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001303 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001304 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001305 Constructor->isInlineSpecified(),
1306 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001307 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001308 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001309 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001310 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001311 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001312 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001313 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001314 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001315 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001316 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001317 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001318 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1319 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001320 D->isStatic(),
1321 D->getStorageClassAsWritten(),
1322 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001323 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001324
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00001325 if (QualifierLoc)
1326 Method->setQualifierInfo(QualifierLoc);
John McCallb6217662010-03-15 10:12:16 +00001327
Douglas Gregord60e1052009-08-27 16:57:43 +00001328 if (TemplateParams) {
1329 // Our resulting instantiation is actually a function template, since we
1330 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001331 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001332 // template<typename T>
1333 // struct X {
1334 // template<typename U> void f(T, U);
1335 // };
1336 //
1337 // X<int> x;
1338 //
1339 // We are instantiating the member template "f" within X<int>, which means
1340 // substituting int for T, but leaving "f" as a member function template.
1341 // Build the function template itself.
1342 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1343 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001344 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001345 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001346 if (isFriend) {
1347 FunctionTemplate->setLexicalDeclContext(Owner);
1348 FunctionTemplate->setObjectOfFriendDecl(true);
1349 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001350 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001351 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001352 } else if (FunctionTemplate) {
1353 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001354 std::pair<const TemplateArgument *, unsigned> Innermost
1355 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001356 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001357 TemplateArgumentList::CreateCopy(SemaRef.Context,
1358 Innermost.first,
1359 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001360 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001361 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001362 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001363 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001364 }
1365
Mike Stump1eb44332009-09-09 15:08:12 +00001366 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001367 // out-of-line, the instantiation will have the same lexical
1368 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001369 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001370 if (NumTempParamLists)
1371 Method->setTemplateParameterListsInfo(SemaRef.Context,
1372 NumTempParamLists,
1373 TempParamLists.data());
1374
John McCallb0cb0222010-03-27 05:57:59 +00001375 Method->setLexicalDeclContext(Owner);
1376 Method->setObjectOfFriendDecl(true);
1377 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001378 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregor5545e162009-03-24 00:38:23 +00001380 // Attach the parameters
1381 for (unsigned P = 0; P < Params.size(); ++P)
1382 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001383 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001384
1385 if (InitMethodInstantiation(Method, D))
1386 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001387
Abramo Bagnara25777432010-08-11 22:01:17 +00001388 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1389 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001390
John McCallb0cb0222010-03-27 05:57:59 +00001391 if (!FunctionTemplate || TemplateParams || isFriend) {
1392 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Douglas Gregordec06662009-08-21 18:42:58 +00001394 // In C++, the previous declaration we find might be a tag type
1395 // (class or enum). In this case, the new declaration will hide the
1396 // tag type. Note that this does does not apply if we're declaring a
1397 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001398 if (Previous.isSingleTagDecl())
1399 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001400 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001401
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001402 bool Redeclaration = false;
Peter Collingbournec80e8112011-01-21 02:08:54 +00001403 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001404
Douglas Gregor4ba31362009-12-01 17:24:26 +00001405 if (D->isPure())
1406 SemaRef.CheckPureMethod(Method, SourceRange());
1407
John McCall46460a62010-01-20 21:53:11 +00001408 Method->setAccess(D->getAccess());
1409
Anders Carlsson9eefa222011-01-20 06:52:44 +00001410 SemaRef.CheckOverrideControl(Method);
1411
John McCallb0cb0222010-03-27 05:57:59 +00001412 if (FunctionTemplate) {
1413 // If there's a function template, let our caller handle it.
1414 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1415 // Don't hide a (potentially) valid declaration with an invalid one.
1416 } else {
1417 NamedDecl *DeclToAdd = (TemplateParams
1418 ? cast<NamedDecl>(FunctionTemplate)
1419 : Method);
1420 if (isFriend)
1421 Record->makeDeclVisibleInContext(DeclToAdd);
1422 else
1423 Owner->addDecl(DeclToAdd);
1424 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001425
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001426 return Method;
1427}
1428
Douglas Gregor615c5d42009-03-24 16:43:20 +00001429Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001430 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001431}
1432
Douglas Gregor03b2b072009-03-24 00:15:49 +00001433Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001434 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001435}
1436
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001437Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001438 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001439}
1440
Douglas Gregor6477b692009-03-25 15:04:13 +00001441ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001442 return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001443}
1444
John McCalle29ba202009-08-20 01:44:21 +00001445Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1446 TemplateTypeParmDecl *D) {
1447 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001448 const Type* T = D->getTypeForDecl();
1449 assert(T->isTemplateTypeParmType());
1450 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001451
John McCalle29ba202009-08-20 01:44:21 +00001452 TemplateTypeParmDecl *Inst =
Abramo Bagnara344577e2011-03-06 15:48:19 +00001453 TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1454 D->getLocStart(), D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001455 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001456 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001457 D->wasDeclaredWithTypename(),
1458 D->isParameterPack());
Douglas Gregor9a299e02011-03-04 17:52:15 +00001459 Inst->setAccess(AS_public);
1460
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001461 if (D->hasDefaultArgument())
1462 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001463
Douglas Gregor550d9b22009-10-31 17:21:17 +00001464 // Introduce this template parameter's instantiation into the instantiation
1465 // scope.
1466 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1467
John McCalle29ba202009-08-20 01:44:21 +00001468 return Inst;
1469}
1470
Douglas Gregor33642df2009-10-23 23:25:44 +00001471Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1472 NonTypeTemplateParmDecl *D) {
1473 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001474 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1475 llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1476 llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1477 bool IsExpandedParameterPack = false;
1478 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001479 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001480 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001481
1482 if (D->isExpandedParameterPack()) {
1483 // The non-type template parameter pack is an already-expanded pack
1484 // expansion of types. Substitute into each of the expanded types.
1485 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1486 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1487 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1488 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1489 TemplateArgs,
1490 D->getLocation(),
1491 D->getDeclName());
1492 if (!NewDI)
1493 return 0;
1494
1495 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1496 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1497 D->getLocation());
1498 if (NewT.isNull())
1499 return 0;
1500 ExpandedParameterPackTypes.push_back(NewT);
1501 }
1502
1503 IsExpandedParameterPack = true;
1504 DI = D->getTypeSourceInfo();
1505 T = DI->getType();
1506 } else if (isa<PackExpansionTypeLoc>(TL)) {
1507 // The non-type template parameter pack's type is a pack expansion of types.
1508 // Determine whether we need to expand this parameter pack into separate
1509 // types.
1510 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1511 TypeLoc Pattern = Expansion.getPatternLoc();
1512 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1513 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1514
1515 // Determine whether the set of unexpanded parameter packs can and should
1516 // be expanded.
1517 bool Expand = true;
1518 bool RetainExpansion = false;
1519 llvm::Optional<unsigned> OrigNumExpansions
1520 = Expansion.getTypePtr()->getNumExpansions();
1521 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1522 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1523 Pattern.getSourceRange(),
1524 Unexpanded.data(),
1525 Unexpanded.size(),
1526 TemplateArgs,
1527 Expand, RetainExpansion,
1528 NumExpansions))
1529 return 0;
1530
1531 if (Expand) {
1532 for (unsigned I = 0; I != *NumExpansions; ++I) {
1533 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1534 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1535 D->getLocation(),
1536 D->getDeclName());
1537 if (!NewDI)
1538 return 0;
1539
1540 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1541 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1542 NewDI->getType(),
1543 D->getLocation());
1544 if (NewT.isNull())
1545 return 0;
1546 ExpandedParameterPackTypes.push_back(NewT);
1547 }
1548
1549 // Note that we have an expanded parameter pack. The "type" of this
1550 // expanded parameter pack is the original expansion type, but callers
1551 // will end up using the expanded parameter pack types for type-checking.
1552 IsExpandedParameterPack = true;
1553 DI = D->getTypeSourceInfo();
1554 T = DI->getType();
1555 } else {
1556 // We cannot fully expand the pack expansion now, so substitute into the
1557 // pattern and create a new pack expansion type.
1558 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1559 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1560 D->getLocation(),
1561 D->getDeclName());
1562 if (!NewPattern)
1563 return 0;
1564
1565 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1566 NumExpansions);
1567 if (!DI)
1568 return 0;
1569
1570 T = DI->getType();
1571 }
1572 } else {
1573 // Simple case: substitution into a parameter that is not a parameter pack.
1574 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1575 D->getLocation(), D->getDeclName());
1576 if (!DI)
1577 return 0;
1578
1579 // Check that this type is acceptable for a non-type template parameter.
1580 bool Invalid = false;
1581 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1582 D->getLocation());
1583 if (T.isNull()) {
1584 T = SemaRef.Context.IntTy;
1585 Invalid = true;
1586 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001587 }
1588
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001589 NonTypeTemplateParmDecl *Param;
1590 if (IsExpandedParameterPack)
1591 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1592 D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001593 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001594 D->getPosition(),
1595 D->getIdentifier(), T,
1596 DI,
1597 ExpandedParameterPackTypes.data(),
1598 ExpandedParameterPackTypes.size(),
1599 ExpandedParameterPackTypesAsWritten.data());
1600 else
1601 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1602 D->getLocation(),
1603 D->getDepth() - TemplateArgs.getNumLevels(),
1604 D->getPosition(),
1605 D->getIdentifier(), T,
1606 D->isParameterPack(), DI);
1607
Douglas Gregor9a299e02011-03-04 17:52:15 +00001608 Param->setAccess(AS_public);
Douglas Gregor33642df2009-10-23 23:25:44 +00001609 if (Invalid)
1610 Param->setInvalidDecl();
1611
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001612 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001613
1614 // Introduce this template parameter's instantiation into the instantiation
1615 // scope.
1616 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001617 return Param;
1618}
1619
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001620Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001621TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1622 TemplateTemplateParmDecl *D) {
1623 // Instantiate the template parameter list of the template template parameter.
1624 TemplateParameterList *TempParams = D->getTemplateParameters();
1625 TemplateParameterList *InstParams;
1626 {
1627 // Perform the actual substitution of template parameters within a new,
1628 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001629 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001630 InstParams = SubstTemplateParams(TempParams);
1631 if (!InstParams)
1632 return NULL;
1633 }
1634
1635 // Build the template template parameter.
1636 TemplateTemplateParmDecl *Param
1637 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001638 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001639 D->getPosition(), D->isParameterPack(),
1640 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001641 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor9a299e02011-03-04 17:52:15 +00001642 Param->setAccess(AS_public);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001643
Douglas Gregor9106ef72009-11-11 16:58:32 +00001644 // Introduce this template parameter's instantiation into the instantiation
1645 // scope.
1646 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1647
1648 return Param;
1649}
1650
Douglas Gregor48c32a72009-11-17 06:07:40 +00001651Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Douglas Gregordb992412011-02-25 16:33:46 +00001652 // Using directives are never dependent (and never contain any types or
1653 // expressions), so they require no explicit instantiation work.
Douglas Gregor48c32a72009-11-17 06:07:40 +00001654
1655 UsingDirectiveDecl *Inst
1656 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1657 D->getNamespaceKeyLocation(),
Douglas Gregordb992412011-02-25 16:33:46 +00001658 D->getQualifierLoc(),
Douglas Gregor48c32a72009-11-17 06:07:40 +00001659 D->getIdentLocation(),
1660 D->getNominatedNamespace(),
1661 D->getCommonAncestor());
1662 Owner->addDecl(Inst);
1663 return Inst;
1664}
1665
John McCalled976492009-12-04 22:46:56 +00001666Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001667
1668 // The nested name specifier may be dependent, for example
1669 // template <typename T> struct t {
1670 // struct s1 { T f1(); };
1671 // struct s2 : s1 { using s1::f1; };
1672 // };
1673 // template struct t<int>;
1674 // Here, in using s1::f1, s1 refers to t<T>::s1;
1675 // we need to substitute for t<int>::s1.
Douglas Gregor5149f372011-02-25 15:54:31 +00001676 NestedNameSpecifierLoc QualifierLoc
1677 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1678 TemplateArgs);
1679 if (!QualifierLoc)
Douglas Gregordc355712011-02-25 00:36:19 +00001680 return 0;
Douglas Gregor1b398202010-09-29 17:58:28 +00001681
1682 // The name info is non-dependent, so no transformation
1683 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001684 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001685
John McCall9f54ad42009-12-10 09:41:52 +00001686 // We only need to do redeclaration lookups if we're in a class
1687 // scope (in fact, it's not really even possible in non-class
1688 // scopes).
1689 bool CheckRedeclaration = Owner->isRecord();
1690
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001691 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1692 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001693
John McCalled976492009-12-04 22:46:56 +00001694 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001695 D->getUsingLocation(),
Douglas Gregor5149f372011-02-25 15:54:31 +00001696 QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001697 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001698 D->isTypeName());
1699
Douglas Gregor5149f372011-02-25 15:54:31 +00001700 CXXScopeSpec SS;
1701 SS.Adopt(QualifierLoc);
John McCall9f54ad42009-12-10 09:41:52 +00001702 if (CheckRedeclaration) {
1703 Prev.setHideTags(false);
1704 SemaRef.LookupQualifiedName(Prev, Owner);
1705
1706 // Check for invalid redeclarations.
1707 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1708 D->isTypeName(), SS,
1709 D->getLocation(), Prev))
1710 NewUD->setInvalidDecl();
1711
1712 }
1713
1714 if (!NewUD->isInvalidDecl() &&
1715 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001716 D->getLocation()))
1717 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001718
John McCalled976492009-12-04 22:46:56 +00001719 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1720 NewUD->setAccess(D->getAccess());
1721 Owner->addDecl(NewUD);
1722
John McCall9f54ad42009-12-10 09:41:52 +00001723 // Don't process the shadow decls for an invalid decl.
1724 if (NewUD->isInvalidDecl())
1725 return NewUD;
1726
John McCall323c3102009-12-22 22:26:37 +00001727 bool isFunctionScope = Owner->isFunctionOrMethod();
1728
John McCall9f54ad42009-12-10 09:41:52 +00001729 // Process the shadow decls.
1730 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1731 I != E; ++I) {
1732 UsingShadowDecl *Shadow = *I;
1733 NamedDecl *InstTarget =
Douglas Gregorb7107222011-03-04 19:46:35 +00001734 cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
1735 Shadow->getLocation(),
1736 Shadow->getTargetDecl(),
1737 TemplateArgs));
1738 if (!InstTarget)
1739 return 0;
John McCall9f54ad42009-12-10 09:41:52 +00001740
1741 if (CheckRedeclaration &&
1742 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1743 continue;
1744
1745 UsingShadowDecl *InstShadow
1746 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1747 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001748
1749 if (isFunctionScope)
1750 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001751 }
John McCalled976492009-12-04 22:46:56 +00001752
1753 return NewUD;
1754}
1755
1756Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001757 // Ignore these; we handle them in bulk when processing the UsingDecl.
1758 return 0;
John McCalled976492009-12-04 22:46:56 +00001759}
1760
John McCall7ba107a2009-11-18 02:36:19 +00001761Decl * TemplateDeclInstantiator
1762 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001763 NestedNameSpecifierLoc QualifierLoc
1764 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
1765 TemplateArgs);
1766 if (!QualifierLoc)
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001767 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001769 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001770 SS.Adopt(QualifierLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001772 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1773 // Hence, no tranformation is required for it.
1774 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001775 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001776 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001777 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001778 /*instantiation*/ true,
1779 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001780 if (UD)
John McCalled976492009-12-04 22:46:56 +00001781 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1782
John McCall7ba107a2009-11-18 02:36:19 +00001783 return UD;
1784}
1785
1786Decl * TemplateDeclInstantiator
1787 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Douglas Gregor5149f372011-02-25 15:54:31 +00001788 NestedNameSpecifierLoc QualifierLoc
1789 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
1790 if (!QualifierLoc)
John McCall7ba107a2009-11-18 02:36:19 +00001791 return 0;
Douglas Gregor5149f372011-02-25 15:54:31 +00001792
John McCall7ba107a2009-11-18 02:36:19 +00001793 CXXScopeSpec SS;
Douglas Gregor5149f372011-02-25 15:54:31 +00001794 SS.Adopt(QualifierLoc);
John McCall7ba107a2009-11-18 02:36:19 +00001795
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001796 DeclarationNameInfo NameInfo
1797 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1798
John McCall7ba107a2009-11-18 02:36:19 +00001799 NamedDecl *UD =
1800 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001801 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001802 /*instantiation*/ true,
1803 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001804 if (UD)
John McCalled976492009-12-04 22:46:56 +00001805 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1806
Anders Carlsson0d8df782009-08-29 19:37:28 +00001807 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001808}
1809
John McCallce3ff2b2009-08-25 22:02:44 +00001810Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001811 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001812 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001813 if (D->isInvalidDecl())
1814 return 0;
1815
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001816 return Instantiator.Visit(D);
1817}
1818
John McCalle29ba202009-08-20 01:44:21 +00001819/// \brief Instantiates a nested template parameter list in the current
1820/// instantiation context.
1821///
1822/// \param L The parameter list to instantiate
1823///
1824/// \returns NULL if there was an error
1825TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001826TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001827 // Get errors for all the parameters before bailing out.
1828 bool Invalid = false;
1829
1830 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001831 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001832 ParamVector Params;
1833 Params.reserve(N);
1834 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1835 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001836 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001837 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001838 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001839 }
1840
1841 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001842 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001843 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001844
1845 TemplateParameterList *InstL
1846 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1847 L->getLAngleLoc(), &Params.front(), N,
1848 L->getRAngleLoc());
1849 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001850}
John McCalle29ba202009-08-20 01:44:21 +00001851
Douglas Gregored9c0f92009-10-29 00:04:11 +00001852/// \brief Instantiate the declaration of a class template partial
1853/// specialization.
1854///
1855/// \param ClassTemplate the (instantiated) class template that is partially
1856// specialized by the instantiation of \p PartialSpec.
1857///
1858/// \param PartialSpec the (uninstantiated) class template partial
1859/// specialization that we are instantiating.
1860///
Douglas Gregord65587f2010-11-10 19:44:59 +00001861/// \returns The instantiated partial specialization, if successful; otherwise,
1862/// NULL to indicate an error.
1863ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001864TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1865 ClassTemplateDecl *ClassTemplate,
1866 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001867 // Create a local instantiation scope for this class template partial
1868 // specialization, which will contain the instantiations of the template
1869 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001870 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001871
Douglas Gregored9c0f92009-10-29 00:04:11 +00001872 // Substitute into the template parameters of the class template partial
1873 // specialization.
1874 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1875 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1876 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001877 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001878
1879 // Substitute into the template arguments of the class template partial
1880 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00001881 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
Douglas Gregore02e2622010-12-22 21:19:48 +00001882 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1883 PartialSpec->getNumTemplateArgsAsWritten(),
1884 InstTemplateArgs, TemplateArgs))
1885 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001886
Douglas Gregored9c0f92009-10-29 00:04:11 +00001887 // Check that the template argument list is well-formed for this
1888 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001889 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001890 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1891 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001892 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001893 false,
1894 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001895 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001896
1897 // Figure out where to insert this class template partial specialization
1898 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001899 void *InsertPos = 0;
1900 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001901 = ClassTemplate->findPartialSpecialization(Converted.data(),
1902 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001903
1904 // Build the canonical type that describes the converted template
1905 // arguments of the class template partial specialization.
1906 QualType CanonType
1907 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001908 Converted.data(),
1909 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001910
1911 // Build the fully-sugared type for this class template
1912 // specialization as the user wrote in the specialization
1913 // itself. This means that we'll pretty-print the type retrieved
1914 // from the specialization's declaration the way that the user
1915 // actually wrote the specialization, rather than formatting the
1916 // name based on the "canonical" representation used to store the
1917 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001918 TypeSourceInfo *WrittenTy
1919 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1920 TemplateName(ClassTemplate),
1921 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001922 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001923 CanonType);
1924
1925 if (PrevDecl) {
1926 // We've already seen a partial specialization with the same template
1927 // parameters and template arguments. This can happen, for example, when
1928 // substituting the outer template arguments ends up causing two
1929 // class template partial specializations of a member class template
1930 // to have identical forms, e.g.,
1931 //
1932 // template<typename T, typename U>
1933 // struct Outer {
1934 // template<typename X, typename Y> struct Inner;
1935 // template<typename Y> struct Inner<T, Y>;
1936 // template<typename Y> struct Inner<U, Y>;
1937 // };
1938 //
1939 // Outer<int, int> outer; // error: the partial specializations of Inner
1940 // // have the same signature.
1941 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001942 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001943 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1944 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001945 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001946 }
1947
1948
1949 // Create the class template partial specialization declaration.
1950 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001951 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1952 PartialSpec->getTagKind(),
1953 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001954 PartialSpec->getLocation(),
1955 InstParams,
1956 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001957 Converted.data(),
1958 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001959 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001960 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001961 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001962 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001963 // Substitute the nested name specifier, if any.
1964 if (SubstQualifier(PartialSpec, InstPartialSpec))
1965 return 0;
1966
Douglas Gregored9c0f92009-10-29 00:04:11 +00001967 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001968 InstPartialSpec->setTypeAsWritten(WrittenTy);
1969
Douglas Gregored9c0f92009-10-29 00:04:11 +00001970 // Add this partial specialization to the set of class template partial
1971 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001972 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001973 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001974}
1975
John McCall21ef0fa2010-03-11 09:03:00 +00001976TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001977TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001978 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001979 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1980 assert(OldTInfo && "substituting function without type source info");
1981 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001982 TypeSourceInfo *NewTInfo
1983 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1984 D->getTypeSpecStartLoc(),
1985 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001986 if (!NewTInfo)
1987 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001988
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001989 if (NewTInfo != OldTInfo) {
1990 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001991 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001992 if (FunctionProtoTypeLoc *OldProtoLoc
1993 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001994 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001995 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1996 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001997 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
1998 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
1999 OldIdx != NumOldParams; ++OldIdx) {
2000 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
2001 if (!OldParam->isParameterPack() ||
2002 (NewIdx < NumNewParams &&
2003 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
2004 // Simple case: normal parameter, or a parameter pack that's
2005 // instantiated to a (still-dependent) parameter pack.
2006 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2007 Params.push_back(NewParam);
2008 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
2009 NewParam);
2010 continue;
2011 }
2012
2013 // Parameter pack: make the instantiation an argument pack.
2014 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2015 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002016 unsigned NumArgumentsInExpansion
2017 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2018 TemplateArgs);
2019 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002020 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2021 Params.push_back(NewParam);
2022 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2023 NewParam);
2024 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002025 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002026 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002027 } else {
2028 // The function type itself was not dependent and therefore no
2029 // substitution occurred. However, we still need to instantiate
2030 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002031 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002032 if (FunctionProtoTypeLoc *OldProtoLoc
2033 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2034 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2035 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2036 if (!Parm)
2037 return 0;
2038 Params.push_back(Parm);
2039 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002040 }
2041 }
John McCall21ef0fa2010-03-11 09:03:00 +00002042 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002043}
2044
Mike Stump1eb44332009-09-09 15:08:12 +00002045/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002046/// declaration (New) from the corresponding fields of its template (Tmpl).
2047///
2048/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002049bool
2050TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002051 FunctionDecl *Tmpl) {
2052 if (Tmpl->isDeleted())
2053 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Douglas Gregorcca9e962009-07-01 22:01:06 +00002055 // If we are performing substituting explicitly-specified template arguments
2056 // or deduced template arguments into a function template and we reach this
2057 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002058 // to keeping the new function template specialization. We therefore
2059 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002060 // into a template instantiation for this specific function template
2061 // specialization, which is not a SFINAE context, so that we diagnose any
2062 // further errors in the declaration itself.
2063 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2064 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2065 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2066 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002067 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002068 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002069 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002070 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002071 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002072 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2073 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002074 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002075 }
2076 }
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002078 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2079 assert(Proto && "Function template without prototype?");
2080
2081 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2082 Proto->getNoReturnAttr()) {
2083 // The function has an exception specification or a "noreturn"
2084 // attribute. Substitute into each of the exception types.
2085 llvm::SmallVector<QualType, 4> Exceptions;
2086 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2087 // FIXME: Poor location information!
Douglas Gregorb99268b2010-12-21 00:52:54 +00002088 if (const PackExpansionType *PackExpansion
2089 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2090 // We have a pack expansion. Instantiate it.
2091 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2092 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2093 Unexpanded);
2094 assert(!Unexpanded.empty() &&
2095 "Pack expansion without parameter packs?");
2096
2097 bool Expand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002098 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002099 llvm::Optional<unsigned> NumExpansions
2100 = PackExpansion->getNumExpansions();
Douglas Gregorb99268b2010-12-21 00:52:54 +00002101 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2102 SourceRange(),
2103 Unexpanded.data(),
2104 Unexpanded.size(),
2105 TemplateArgs,
Douglas Gregord3731192011-01-10 07:32:04 +00002106 Expand,
2107 RetainExpansion,
2108 NumExpansions))
Douglas Gregorb99268b2010-12-21 00:52:54 +00002109 break;
2110
2111 if (!Expand) {
2112 // We can't expand this pack expansion into separate arguments yet;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002113 // just substitute into the pattern and create a new pack expansion
2114 // type.
Douglas Gregorb99268b2010-12-21 00:52:54 +00002115 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2116 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2117 TemplateArgs,
2118 New->getLocation(), New->getDeclName());
2119 if (T.isNull())
2120 break;
2121
Douglas Gregorcded4f62011-01-14 17:04:44 +00002122 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
Douglas Gregorb99268b2010-12-21 00:52:54 +00002123 Exceptions.push_back(T);
2124 continue;
2125 }
2126
2127 // Substitute into the pack expansion pattern for each template
2128 bool Invalid = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002129 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
Douglas Gregorb99268b2010-12-21 00:52:54 +00002130 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2131
2132 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2133 TemplateArgs,
2134 New->getLocation(), New->getDeclName());
2135 if (T.isNull()) {
2136 Invalid = true;
2137 break;
2138 }
2139
2140 Exceptions.push_back(T);
2141 }
2142
2143 if (Invalid)
2144 break;
2145
2146 continue;
2147 }
2148
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002149 QualType T
2150 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2151 New->getLocation(), New->getDeclName());
2152 if (T.isNull() ||
2153 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2154 continue;
2155
2156 Exceptions.push_back(T);
2157 }
2158
2159 // Rebuild the function type
2160
John McCalle23cf432010-12-14 08:05:40 +00002161 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
Sebastian Redl8b5b4092011-03-06 10:52:04 +00002162 EPI.ExceptionSpecType = Proto->hasExceptionSpec() ?
2163 (Proto->hasAnyExceptionSpec() ? EST_DynamicAny : EST_Dynamic) :
2164 EST_None;
John McCalle23cf432010-12-14 08:05:40 +00002165 EPI.NumExceptions = Exceptions.size();
2166 EPI.Exceptions = Exceptions.data();
2167 EPI.ExtInfo = Proto->getExtInfo();
2168
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002169 const FunctionProtoType *NewProto
2170 = New->getType()->getAs<FunctionProtoType>();
2171 assert(NewProto && "Template instantiation without function prototype?");
2172 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2173 NewProto->arg_type_begin(),
2174 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +00002175 EPI));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002176 }
2177
John McCall1d8d1cc2010-08-01 02:01:53 +00002178 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002179
Douglas Gregore53060f2009-06-25 22:08:12 +00002180 return false;
2181}
2182
Douglas Gregor5545e162009-03-24 00:38:23 +00002183/// \brief Initializes common fields of an instantiated method
2184/// declaration (New) from the corresponding fields of its template
2185/// (Tmpl).
2186///
2187/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002188bool
2189TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002190 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002191 if (InitFunctionInstantiation(New, Tmpl))
2192 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002193
Douglas Gregor5545e162009-03-24 00:38:23 +00002194 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002195 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002196 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002197
2198 // FIXME: attributes
2199 // FIXME: New needs a pointer to Tmpl
2200 return false;
2201}
Douglas Gregora58861f2009-05-13 20:28:22 +00002202
2203/// \brief Instantiate the definition of the given function from its
2204/// template.
2205///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002206/// \param PointOfInstantiation the point at which the instantiation was
2207/// required. Note that this is not precisely a "point of instantiation"
2208/// for the function, but it's close.
2209///
Douglas Gregora58861f2009-05-13 20:28:22 +00002210/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002211/// function template specialization or member function of a class template
2212/// specialization.
2213///
2214/// \param Recursive if true, recursively instantiates any functions that
2215/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002216///
2217/// \param DefinitionRequired if true, then we are performing an explicit
2218/// instantiation where the body of the function is required. Complain if
2219/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002220void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002221 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002222 bool Recursive,
2223 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002224 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002225 return;
2226
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002227 // Never instantiate an explicit specialization.
2228 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2229 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002230
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002231 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002232 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002233 Stmt *Pattern = 0;
2234 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002235 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002236
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002237 if (!Pattern) {
2238 if (DefinitionRequired) {
2239 if (Function->getPrimaryTemplate())
2240 Diag(PointOfInstantiation,
2241 diag::err_explicit_instantiation_undefined_func_template)
2242 << Function->getPrimaryTemplate();
2243 else
2244 Diag(PointOfInstantiation,
2245 diag::err_explicit_instantiation_undefined_member)
2246 << 1 << Function->getDeclName() << Function->getDeclContext();
2247
2248 if (PatternDecl)
2249 Diag(PatternDecl->getLocation(),
2250 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002251 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002252 } else if (Function->getTemplateSpecializationKind()
2253 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002254 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002255 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002256 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002257
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002258 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002259 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002260
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002261 // C++0x [temp.explicit]p9:
2262 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002263 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002264 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002265 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002266 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002267 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002268 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002270 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2271 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002272 return;
2273
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002274 // If we're performing recursive template instantiation, create our own
2275 // queue of pending implicit instantiations that we will instantiate later,
2276 // while we're still within our own instantiation context.
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002277 llvm::SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002278 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002279 if (Recursive) {
2280 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002281 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002282 }
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Douglas Gregor9679caf2010-05-12 17:27:19 +00002284 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002285 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002286 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002287
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002288 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002289 // recorded, unless we're actually a member function within a local
2290 // class, in which case we need to merge our results with the parent
2291 // scope (of the enclosing function).
2292 bool MergeWithParentScope = false;
2293 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2294 MergeWithParentScope = Rec->isLocalClass();
2295
2296 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002298 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002299 // instantiation scope, and set the parameter names to those used
2300 // in the template.
Douglas Gregor12c9c002011-01-07 16:43:16 +00002301 unsigned FParamIdx = 0;
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002302 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2303 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002304 if (!PatternParam->isParameterPack()) {
2305 // Simple case: not a parameter pack.
2306 assert(FParamIdx < Function->getNumParams());
2307 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2308 FunctionParam->setDeclName(PatternParam->getDeclName());
2309 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2310 ++FParamIdx;
2311 continue;
2312 }
2313
2314 // Expand the parameter pack.
2315 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2316 for (unsigned NumFParams = Function->getNumParams();
2317 FParamIdx < NumFParams;
2318 ++FParamIdx) {
2319 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2320 FunctionParam->setDeclName(PatternParam->getDeclName());
2321 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2322 }
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002323 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002324
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002325 // Enter the scope of this instantiation. We don't use
2326 // PushDeclContext because we don't have a scope.
John McCalleee1d542011-02-14 07:13:47 +00002327 Sema::ContextRAII savedContext(*this, Function);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002328
Mike Stump1eb44332009-09-09 15:08:12 +00002329 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002330 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002331
2332 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002333 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002334 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2335 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2336 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002337 }
2338
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002339 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002340 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002341
Douglas Gregor52604ab2009-09-11 21:19:12 +00002342 if (Body.isInvalid())
2343 Function->setInvalidDecl();
2344
John McCall9ae2f072010-08-23 23:25:46 +00002345 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002346 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002347
John McCall0c01d182010-03-24 05:22:00 +00002348 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2349
John McCalleee1d542011-02-14 07:13:47 +00002350 savedContext.pop();
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002351
2352 DeclGroupRef DG(Function);
2353 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002354
Douglas Gregor60406be2010-01-16 22:29:39 +00002355 // This class may have local implicit instantiations that need to be
2356 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002357 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002358 Scope.Exit();
2359
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002360 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002361 // Define any pending vtables.
2362 DefineUsedVTables();
2363
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002364 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002365 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002366 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002368 // Restore the set of pending vtables.
2369 VTableUses.swap(SavedVTableUses);
2370
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002371 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002372 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002373 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002374}
2375
2376/// \brief Instantiate the definition of the given variable from its
2377/// template.
2378///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002379/// \param PointOfInstantiation the point at which the instantiation was
2380/// required. Note that this is not precisely a "point of instantiation"
2381/// for the function, but it's close.
2382///
2383/// \param Var the already-instantiated declaration of a static member
2384/// variable of a class template specialization.
2385///
2386/// \param Recursive if true, recursively instantiates any functions that
2387/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002388///
2389/// \param DefinitionRequired if true, then we are performing an explicit
2390/// instantiation where an out-of-line definition of the member variable
2391/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002392void Sema::InstantiateStaticDataMemberDefinition(
2393 SourceLocation PointOfInstantiation,
2394 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002395 bool Recursive,
2396 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002397 if (Var->isInvalidDecl())
2398 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Douglas Gregor7caa6822009-07-24 20:34:43 +00002400 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002401 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002402 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002403 assert(Def->isStaticDataMember() && "Not a static data member?");
2404 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002405
Douglas Gregor0d035142009-10-27 18:42:08 +00002406 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002407 // We did not find an out-of-line definition of this static data member,
2408 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002409 // instantiate this definition (or provide a specialization for it) in
2410 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002411 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002412 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002413 Diag(PointOfInstantiation,
2414 diag::err_explicit_instantiation_undefined_member)
2415 << 2 << Var->getDeclName() << Var->getDeclContext();
2416 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002417 } else if (Var->getTemplateSpecializationKind()
2418 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002419 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002420 std::make_pair(Var, PointOfInstantiation));
2421 }
2422
Douglas Gregor7caa6822009-07-24 20:34:43 +00002423 return;
2424 }
2425
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002426 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002427 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002428 return;
2429
2430 // C++0x [temp.explicit]p9:
2431 // Except for inline functions, other explicit instantiation declarations
2432 // have the effect of suppressing the implicit instantiation of the entity
2433 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002434 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002435 == TSK_ExplicitInstantiationDeclaration)
2436 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Douglas Gregor7caa6822009-07-24 20:34:43 +00002438 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2439 if (Inst)
2440 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Douglas Gregor7caa6822009-07-24 20:34:43 +00002442 // If we're performing recursive template instantiation, create our own
2443 // queue of pending implicit instantiations that we will instantiate later,
2444 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002445 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002446 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002447 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002448
Douglas Gregor7caa6822009-07-24 20:34:43 +00002449 // Enter the scope of this instantiation. We don't use
2450 // PushDeclContext because we don't have a scope.
John McCallf5ba7e02011-02-14 20:37:25 +00002451 ContextRAII previousContext(*this, Var->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002453 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002454 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002455 getTemplateInstantiationArgs(Var)));
John McCallf5ba7e02011-02-14 20:37:25 +00002456
2457 previousContext.pop();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002458
2459 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002460 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2461 assert(MSInfo && "Missing member specialization information?");
2462 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2463 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002464 DeclGroupRef DG(Var);
2465 Consumer.HandleTopLevelDecl(DG);
2466 }
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Douglas Gregor7caa6822009-07-24 20:34:43 +00002468 if (Recursive) {
2469 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002470 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002471 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002472
Douglas Gregor7caa6822009-07-24 20:34:43 +00002473 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002474 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002475 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002476}
Douglas Gregor815215d2009-05-27 05:35:12 +00002477
Anders Carlsson09025312009-08-29 05:16:22 +00002478void
2479Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2480 const CXXConstructorDecl *Tmpl,
2481 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Anders Carlsson09025312009-08-29 05:16:22 +00002483 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002484 bool AnyErrors = false;
2485
Anders Carlsson09025312009-08-29 05:16:22 +00002486 // Instantiate all the initializers.
2487 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002488 InitsEnd = Tmpl->init_end();
2489 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002490 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002491
Chandler Carruth030ef472010-09-03 21:54:20 +00002492 // Only instantiate written initializers, let Sema re-construct implicit
2493 // ones.
2494 if (!Init->isWritten())
2495 continue;
2496
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002497 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002498 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002499
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002500 SourceLocation EllipsisLoc;
2501
2502 if (Init->isPackExpansion()) {
2503 // This is a pack expansion. We should expand it now.
2504 TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
2505 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2506 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2507 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002508 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002509 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002510 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
2511 BaseTL.getSourceRange(),
2512 Unexpanded.data(),
2513 Unexpanded.size(),
2514 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002515 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002516 NumExpansions)) {
2517 AnyErrors = true;
2518 New->setInvalidDecl();
2519 continue;
2520 }
2521 assert(ShouldExpand && "Partial instantiation of base initializer?");
2522
2523 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002524 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002525 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2526
2527 // Instantiate the initializer.
2528 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2529 LParenLoc, NewArgs, RParenLoc)) {
2530 AnyErrors = true;
2531 break;
2532 }
2533
2534 // Instantiate the base type.
2535 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2536 TemplateArgs,
2537 Init->getSourceLocation(),
2538 New->getDeclName());
2539 if (!BaseTInfo) {
2540 AnyErrors = true;
2541 break;
2542 }
2543
2544 // Build the initializer.
2545 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2546 BaseTInfo,
2547 (Expr **)NewArgs.data(),
2548 NewArgs.size(),
2549 Init->getLParenLoc(),
2550 Init->getRParenLoc(),
2551 New->getParent(),
2552 SourceLocation());
2553 if (NewInit.isInvalid()) {
2554 AnyErrors = true;
2555 break;
2556 }
2557
2558 NewInits.push_back(NewInit.get());
2559 NewArgs.clear();
2560 }
2561
2562 continue;
2563 }
2564
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002565 // Instantiate the initializer.
2566 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002567 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002568 AnyErrors = true;
2569 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002570 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002571
Anders Carlsson09025312009-08-29 05:16:22 +00002572 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002573 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002574 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002575 TemplateArgs,
2576 Init->getSourceLocation(),
2577 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002578 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002579 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002580 New->setInvalidDecl();
2581 continue;
2582 }
2583
John McCalla93c9342009-12-07 02:54:59 +00002584 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002585 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002586 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002587 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002588 Init->getRParenLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002589 New->getParent(),
2590 EllipsisLoc);
Anders Carlsson09025312009-08-29 05:16:22 +00002591 } else if (Init->isMemberInitializer()) {
Douglas Gregorb7107222011-03-04 19:46:35 +00002592 FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00002593 Init->getMemberLocation(),
2594 Init->getMember(),
2595 TemplateArgs));
Douglas Gregorb7107222011-03-04 19:46:35 +00002596 if (!Member) {
2597 AnyErrors = true;
2598 New->setInvalidDecl();
2599 continue;
2600 }
Mike Stump1eb44332009-09-09 15:08:12 +00002601
2602 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002603 NewArgs.size(),
2604 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002605 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002606 Init->getRParenLoc());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002607 } else if (Init->isIndirectMemberInitializer()) {
2608 IndirectFieldDecl *IndirectMember =
Douglas Gregorb7107222011-03-04 19:46:35 +00002609 cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
Francois Pichet00eb3f92010-12-04 09:14:42 +00002610 Init->getMemberLocation(),
2611 Init->getIndirectMember(), TemplateArgs));
2612
Douglas Gregorb7107222011-03-04 19:46:35 +00002613 if (!IndirectMember) {
2614 AnyErrors = true;
2615 New->setInvalidDecl();
2616 continue;
2617 }
2618
Francois Pichet00eb3f92010-12-04 09:14:42 +00002619 NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2620 NewArgs.size(),
2621 Init->getSourceLocation(),
2622 Init->getLParenLoc(),
2623 Init->getRParenLoc());
Anders Carlsson09025312009-08-29 05:16:22 +00002624 }
2625
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002626 if (NewInit.isInvalid()) {
2627 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002628 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002629 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002630 // FIXME: It would be nice if ASTOwningVector had a release function.
2631 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002632
Anders Carlsson09025312009-08-29 05:16:22 +00002633 NewInits.push_back((MemInitTy *)NewInit.get());
2634 }
2635 }
Mike Stump1eb44332009-09-09 15:08:12 +00002636
Anders Carlsson09025312009-08-29 05:16:22 +00002637 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002638 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002639 /*FIXME: ColonLoc */
2640 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002641 NewInits.data(), NewInits.size(),
2642 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002643}
2644
John McCall52a575a2009-08-29 08:11:13 +00002645// TODO: this could be templated if the various decl types used the
2646// same method name.
2647static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2648 ClassTemplateDecl *Instance) {
2649 Pattern = Pattern->getCanonicalDecl();
2650
2651 do {
2652 Instance = Instance->getCanonicalDecl();
2653 if (Pattern == Instance) return true;
2654 Instance = Instance->getInstantiatedFromMemberTemplate();
2655 } while (Instance);
2656
2657 return false;
2658}
2659
Douglas Gregor0d696532009-09-28 06:34:35 +00002660static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2661 FunctionTemplateDecl *Instance) {
2662 Pattern = Pattern->getCanonicalDecl();
2663
2664 do {
2665 Instance = Instance->getCanonicalDecl();
2666 if (Pattern == Instance) return true;
2667 Instance = Instance->getInstantiatedFromMemberTemplate();
2668 } while (Instance);
2669
2670 return false;
2671}
2672
Douglas Gregored9c0f92009-10-29 00:04:11 +00002673static bool
2674isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2675 ClassTemplatePartialSpecializationDecl *Instance) {
2676 Pattern
2677 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2678 do {
2679 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2680 Instance->getCanonicalDecl());
2681 if (Pattern == Instance)
2682 return true;
2683 Instance = Instance->getInstantiatedFromMember();
2684 } while (Instance);
2685
2686 return false;
2687}
2688
John McCall52a575a2009-08-29 08:11:13 +00002689static bool isInstantiationOf(CXXRecordDecl *Pattern,
2690 CXXRecordDecl *Instance) {
2691 Pattern = Pattern->getCanonicalDecl();
2692
2693 do {
2694 Instance = Instance->getCanonicalDecl();
2695 if (Pattern == Instance) return true;
2696 Instance = Instance->getInstantiatedFromMemberClass();
2697 } while (Instance);
2698
2699 return false;
2700}
2701
2702static bool isInstantiationOf(FunctionDecl *Pattern,
2703 FunctionDecl *Instance) {
2704 Pattern = Pattern->getCanonicalDecl();
2705
2706 do {
2707 Instance = Instance->getCanonicalDecl();
2708 if (Pattern == Instance) return true;
2709 Instance = Instance->getInstantiatedFromMemberFunction();
2710 } while (Instance);
2711
2712 return false;
2713}
2714
2715static bool isInstantiationOf(EnumDecl *Pattern,
2716 EnumDecl *Instance) {
2717 Pattern = Pattern->getCanonicalDecl();
2718
2719 do {
2720 Instance = Instance->getCanonicalDecl();
2721 if (Pattern == Instance) return true;
2722 Instance = Instance->getInstantiatedFromMemberEnum();
2723 } while (Instance);
2724
2725 return false;
2726}
2727
John McCalled976492009-12-04 22:46:56 +00002728static bool isInstantiationOf(UsingShadowDecl *Pattern,
2729 UsingShadowDecl *Instance,
2730 ASTContext &C) {
2731 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2732}
2733
2734static bool isInstantiationOf(UsingDecl *Pattern,
2735 UsingDecl *Instance,
2736 ASTContext &C) {
2737 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2738}
2739
John McCall7ba107a2009-11-18 02:36:19 +00002740static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2741 UsingDecl *Instance,
2742 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002743 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002744}
2745
2746static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002747 UsingDecl *Instance,
2748 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002749 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002750}
2751
John McCall52a575a2009-08-29 08:11:13 +00002752static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2753 VarDecl *Instance) {
2754 assert(Instance->isStaticDataMember());
2755
2756 Pattern = Pattern->getCanonicalDecl();
2757
2758 do {
2759 Instance = Instance->getCanonicalDecl();
2760 if (Pattern == Instance) return true;
2761 Instance = Instance->getInstantiatedFromStaticDataMember();
2762 } while (Instance);
2763
2764 return false;
2765}
2766
John McCalled976492009-12-04 22:46:56 +00002767// Other is the prospective instantiation
2768// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002769static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002770 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002771 if (UnresolvedUsingTypenameDecl *UUD
2772 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2773 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2774 return isInstantiationOf(UUD, UD, Ctx);
2775 }
2776 }
2777
2778 if (UnresolvedUsingValueDecl *UUD
2779 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002780 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2781 return isInstantiationOf(UUD, UD, Ctx);
2782 }
2783 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002784
Anders Carlsson0d8df782009-08-29 19:37:28 +00002785 return false;
2786 }
Mike Stump1eb44332009-09-09 15:08:12 +00002787
John McCall52a575a2009-08-29 08:11:13 +00002788 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2789 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002790
John McCall52a575a2009-08-29 08:11:13 +00002791 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2792 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002793
John McCall52a575a2009-08-29 08:11:13 +00002794 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2795 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002796
Douglas Gregor7caa6822009-07-24 20:34:43 +00002797 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002798 if (Var->isStaticDataMember())
2799 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2800
2801 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2802 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002803
Douglas Gregor0d696532009-09-28 06:34:35 +00002804 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2805 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2806
Douglas Gregored9c0f92009-10-29 00:04:11 +00002807 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2808 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2809 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2810 PartialSpec);
2811
Anders Carlssond8b285f2009-09-01 04:26:58 +00002812 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2813 if (!Field->getDeclName()) {
2814 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002815 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002816 cast<FieldDecl>(D);
2817 }
2818 }
Mike Stump1eb44332009-09-09 15:08:12 +00002819
John McCalled976492009-12-04 22:46:56 +00002820 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2821 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2822
2823 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2824 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2825
Douglas Gregor815215d2009-05-27 05:35:12 +00002826 return D->getDeclName() && isa<NamedDecl>(Other) &&
2827 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2828}
2829
2830template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002831static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002832 NamedDecl *D,
2833 ForwardIterator first,
2834 ForwardIterator last) {
2835 for (; first != last; ++first)
2836 if (isInstantiationOf(Ctx, D, *first))
2837 return cast<NamedDecl>(*first);
2838
2839 return 0;
2840}
2841
John McCall02cace72009-08-28 07:59:38 +00002842/// \brief Finds the instantiation of the given declaration context
2843/// within the current instantiation.
2844///
2845/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002846DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002847 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002848 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002849 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002850 return cast_or_null<DeclContext>(ID);
2851 } else return DC;
2852}
2853
Douglas Gregored961e72009-05-27 17:54:46 +00002854/// \brief Find the instantiation of the given declaration within the
2855/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002856///
2857/// This routine is intended to be used when \p D is a declaration
2858/// referenced from within a template, that needs to mapped into the
2859/// corresponding declaration within an instantiation. For example,
2860/// given:
2861///
2862/// \code
2863/// template<typename T>
2864/// struct X {
2865/// enum Kind {
2866/// KnownValue = sizeof(T)
2867/// };
2868///
2869/// bool getKind() const { return KnownValue; }
2870/// };
2871///
2872/// template struct X<int>;
2873/// \endcode
2874///
2875/// In the instantiation of X<int>::getKind(), we need to map the
2876/// EnumConstantDecl for KnownValue (which refers to
2877/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002878/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2879/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002880NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002881 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002882 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002883 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002884 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002885 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002886 // D is a local of some kind. Look into the map of local
2887 // declarations to their instantiations.
Chris Lattnerd8e54992011-02-17 19:47:42 +00002888 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
2889 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
2890 = CurrentInstantiationScope->findInstantiationOf(D);
Chris Lattnerd8e54992011-02-17 19:47:42 +00002891
Chris Lattner57ad3782011-02-17 20:34:02 +00002892 if (Found) {
2893 if (Decl *FD = Found->dyn_cast<Decl *>())
2894 return cast<NamedDecl>(FD);
2895
2896 unsigned PackIdx = ArgumentPackSubstitutionIndex;
2897 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
2898 }
2899
2900 // If we didn't find the decl, then we must have a label decl that hasn't
2901 // been found yet. Lazily instantiate it and return it now.
2902 assert(isa<LabelDecl>(D));
Chris Lattnerd8e54992011-02-17 19:47:42 +00002903
Chris Lattner57ad3782011-02-17 20:34:02 +00002904 Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
2905 assert(Inst && "Failed to instantiate label??");
2906
2907 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
2908 return cast<LabelDecl>(Inst);
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002909 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002910
Douglas Gregore95b4092009-09-16 18:34:49 +00002911 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2912 if (!Record->isDependentContext())
2913 return D;
2914
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002915 // If the RecordDecl is actually the injected-class-name or a
2916 // "templated" declaration for a class template, class template
2917 // partial specialization, or a member class of a class template,
2918 // substitute into the injected-class-name of the class template
2919 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002920 QualType T;
2921 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2922
2923 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002924 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002925 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2926 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002927 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002928
2929 // If we call SubstType with an InjectedClassNameType here we
2930 // can end up in an infinite loop.
2931 T = Context.getTypeDeclType(Record);
2932 assert(isa<InjectedClassNameType>(T) &&
2933 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002934 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002935 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002936
2937 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002938 // Substitute into the injected-class-name to get the type
2939 // corresponding to the instantiation we want, which may also be
2940 // the current instantiation (if we're in a template
2941 // definition). This substitution should never fail, since we
2942 // know we can instantiate the injected-class-name or we
2943 // wouldn't have gotten to the injected-class-name!
2944
2945 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2946 // extra instantiation in the common case?
Douglas Gregorb46ae392011-03-03 21:48:55 +00002947 T = SubstType(T, TemplateArgs, Loc, DeclarationName());
Douglas Gregore95b4092009-09-16 18:34:49 +00002948 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2949
2950 if (!T->isDependentType()) {
2951 assert(T->isRecordType() && "Instantiation must produce a record type");
2952 return T->getAs<RecordType>()->getDecl();
2953 }
2954
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002955 // We are performing "partial" template instantiation to create
2956 // the member declarations for the members of a class template
2957 // specialization. Therefore, D is actually referring to something
2958 // in the current instantiation. Look through the current
2959 // context, which contains actual instantiations, to find the
2960 // instantiation of the "current instantiation" that D refers
2961 // to.
2962 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002963 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002964 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002965 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002966 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002967 if (isInstantiationOf(ClassTemplate,
2968 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002969 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002970
2971 if (!DC->isDependentContext())
2972 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002973 }
2974
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002975 // We're performing "instantiation" of a member of the current
2976 // instantiation while we are type-checking the
2977 // definition. Compute the declaration context and return that.
2978 assert(!SawNonDependentContext &&
2979 "No dependent context while instantiating record");
2980 DeclContext *DC = computeDeclContext(T);
2981 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002982 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002983 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002984 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002985
Douglas Gregore95b4092009-09-16 18:34:49 +00002986 // Fall through to deal with other dependent record types (e.g.,
2987 // anonymous unions in class templates).
2988 }
John McCall52a575a2009-08-29 08:11:13 +00002989
Douglas Gregore95b4092009-09-16 18:34:49 +00002990 if (!ParentDC->isDependentContext())
2991 return D;
2992
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002993 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002994 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002995 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002996
Douglas Gregor815215d2009-05-27 05:35:12 +00002997 if (ParentDC != D->getDeclContext()) {
2998 // We performed some kind of instantiation in the parent context,
2999 // so now we need to look into the instantiated parent context to
3000 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003001
John McCall3cb0ebd2010-03-10 03:28:59 +00003002 // If our context used to be dependent, we may need to instantiate
3003 // it before performing lookup into that context.
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003004 bool IsBeingInstantiated = false;
John McCall3cb0ebd2010-03-10 03:28:59 +00003005 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003006 if (!Spec->isDependentContext()) {
3007 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00003008 const RecordType *Tag = T->getAs<RecordType>();
3009 assert(Tag && "type of non-dependent record is not a RecordType");
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003010 if (Tag->isBeingDefined())
3011 IsBeingInstantiated = true;
John McCall3cb0ebd2010-03-10 03:28:59 +00003012 if (!Tag->isBeingDefined() &&
3013 RequireCompleteType(Loc, T, diag::err_incomplete_type))
3014 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00003015
3016 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00003017 }
3018 }
3019
Douglas Gregor815215d2009-05-27 05:35:12 +00003020 NamedDecl *Result = 0;
3021 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003022 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00003023 Result = findInstantiationOf(Context, D, Found.first, Found.second);
3024 } else {
3025 // Since we don't have a name for the entity we're looking for,
3026 // our only option is to walk through all of the declarations to
3027 // find that name. This will occur in a few cases:
3028 //
3029 // - anonymous struct/union within a template
3030 // - unnamed class/struct/union/enum within a template
3031 //
3032 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00003033 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003034 ParentDC->decls_begin(),
3035 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00003036 }
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Douglas Gregoreff1dbe2011-03-06 20:12:45 +00003038 if (!Result) {
3039 if (isa<UsingShadowDecl>(D)) {
3040 // UsingShadowDecls can instantiate to nothing because of using hiding.
3041 } else if (Diags.hasErrorOccurred()) {
3042 // We've already complained about something, so most likely this
3043 // declaration failed to instantiate. There's no point in complaining
3044 // further, since this is normal in invalid code.
3045 } else if (IsBeingInstantiated) {
3046 // The class in which this member exists is currently being
3047 // instantiated, and we haven't gotten around to instantiating this
3048 // member yet. This can happen when the code uses forward declarations
3049 // of member classes, and introduces ordering dependencies via
3050 // template instantiation.
3051 Diag(Loc, diag::err_member_not_yet_instantiated)
3052 << D->getDeclName()
3053 << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3054 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
3055 } else {
3056 // We should have found something, but didn't.
3057 llvm_unreachable("Unable to find instantiation of declaration!");
3058 }
3059 }
3060
Douglas Gregor815215d2009-05-27 05:35:12 +00003061 D = Result;
3062 }
3063
Douglas Gregor815215d2009-05-27 05:35:12 +00003064 return D;
3065}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003066
Mike Stump1eb44332009-09-09 15:08:12 +00003067/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003068/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003069void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003070 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003071 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003072 PendingImplicitInstantiation Inst;
3073
3074 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003075 Inst = PendingInstantiations.front();
3076 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003077 } else {
3078 Inst = PendingLocalImplicitInstantiations.front();
3079 PendingLocalImplicitInstantiations.pop_front();
3080 }
Mike Stump1eb44332009-09-09 15:08:12 +00003081
Douglas Gregor7caa6822009-07-24 20:34:43 +00003082 // Instantiate function definitions
3083 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003084 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3085 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003086 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3087 TSK_ExplicitInstantiationDefinition;
3088 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3089 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003090 continue;
3091 }
Mike Stump1eb44332009-09-09 15:08:12 +00003092
Douglas Gregor7caa6822009-07-24 20:34:43 +00003093 // Instantiate static data member definitions.
3094 VarDecl *Var = cast<VarDecl>(Inst.first);
3095 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003096
Chandler Carruth291b4412010-02-13 10:17:50 +00003097 // Don't try to instantiate declarations if the most recent redeclaration
3098 // is invalid.
3099 if (Var->getMostRecentDeclaration()->isInvalidDecl())
3100 continue;
3101
3102 // Check if the most recent declaration has changed the specialization kind
3103 // and removed the need for implicit instantiation.
3104 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3105 case TSK_Undeclared:
3106 assert(false && "Cannot instantitiate an undeclared specialization.");
3107 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003108 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003109 continue; // No longer need to instantiate this type.
3110 case TSK_ExplicitInstantiationDefinition:
3111 // We only need an instantiation if the pending instantiation *is* the
3112 // explicit instantiation.
3113 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003114 case TSK_ImplicitInstantiation:
3115 break;
3116 }
3117
John McCallf312b1e2010-08-26 23:41:50 +00003118 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3119 "instantiating static data member "
3120 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Chandler Carruth58e390e2010-08-25 08:27:02 +00003122 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3123 TSK_ExplicitInstantiationDefinition;
3124 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3125 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003126 }
3127}
John McCall0c01d182010-03-24 05:22:00 +00003128
3129void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3130 const MultiLevelTemplateArgumentList &TemplateArgs) {
3131 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3132 E = Pattern->ddiag_end(); I != E; ++I) {
3133 DependentDiagnostic *DD = *I;
3134
3135 switch (DD->getKind()) {
3136 case DependentDiagnostic::Access:
3137 HandleDependentAccessCheck(*DD, TemplateArgs);
3138 break;
3139 }
3140 }
3141}