blob: 36a190029935969c7baa563c6037066e3587b3fb [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation for declarations.
10//
11//===----------------------------------------------------------------------===/
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCallf312b1e2010-08-26 23:41:50 +000014#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000020#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000021#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000022#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000023#include "clang/AST/TypeLoc.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000024#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025
26using namespace clang;
27
John McCallb6217662010-03-15 10:12:16 +000028bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29 DeclaratorDecl *NewDecl) {
30 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
31 if (!OldQual) return false;
32
33 SourceRange QualRange = OldDecl->getQualifierRange();
34
35 NestedNameSpecifier *NewQual
36 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
37 if (!NewQual)
38 return true;
39
40 NewDecl->setQualifierInfo(NewQual, QualRange);
41 return false;
42}
43
44bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45 TagDecl *NewDecl) {
46 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
47 if (!OldQual) return false;
48
49 SourceRange QualRange = OldDecl->getQualifierRange();
50
51 NestedNameSpecifier *NewQual
52 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
53 if (!NewQual)
54 return true;
55
56 NewDecl->setQualifierInfo(NewQual, QualRange);
57 return false;
58}
59
Chandler Carruth4ced79f2010-06-25 03:22:07 +000060// FIXME: Is this still too simple?
John McCall1d8d1cc2010-08-01 02:01:53 +000061void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
62 Decl *Tmpl, Decl *New) {
Sean Huntcf807c42010-08-18 23:23:40 +000063 for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
64 i != e; ++i) {
65 const Attr *TmplAttr = *i;
Chandler Carruth4ced79f2010-06-25 03:22:07 +000066 // FIXME: This should be generalized to more than just the AlignedAttr.
67 if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
Sean Huntcf807c42010-08-18 23:23:40 +000068 if (Aligned->isAlignmentDependent()) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +000069 // The alignment expression is not potentially evaluated.
John McCall1d8d1cc2010-08-01 02:01:53 +000070 EnterExpressionEvaluationContext Unevaluated(*this,
John McCallf312b1e2010-08-26 23:41:50 +000071 Sema::Unevaluated);
Chandler Carruth4ced79f2010-06-25 03:22:07 +000072
Sean Huntcf807c42010-08-18 23:23:40 +000073 if (Aligned->isAlignmentExpr()) {
John McCall60d7b3a2010-08-24 06:29:42 +000074 ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
Nick Lewycky7663f392010-11-20 01:29:55 +000075 TemplateArgs);
Sean Huntcf807c42010-08-18 23:23:40 +000076 if (!Result.isInvalid())
77 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
78 }
79 else {
80 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
Nick Lewycky7663f392010-11-20 01:29:55 +000081 TemplateArgs,
82 Aligned->getLocation(),
83 DeclarationName());
Sean Huntcf807c42010-08-18 23:23:40 +000084 if (Result)
85 AddAlignedAttr(Aligned->getLocation(), New, Result);
86 }
Chandler Carruth4ced79f2010-06-25 03:22:07 +000087 continue;
88 }
89 }
90
Anders Carlssond8fe2d52009-11-07 06:07:58 +000091 // FIXME: Is cloning correct for all attributes?
John McCall1d8d1cc2010-08-01 02:01:53 +000092 Attr *NewAttr = TmplAttr->clone(Context);
Anders Carlssond8fe2d52009-11-07 06:07:58 +000093 New->addAttr(NewAttr);
94 }
95}
96
Douglas Gregor4f722be2009-03-25 15:45:12 +000097Decl *
98TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
99 assert(false && "Translation units cannot be instantiated");
100 return D;
101}
102
103Decl *
104TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
105 assert(false && "Namespaces cannot be instantiated");
106 return D;
107}
108
John McCall3dbd3d52010-02-16 06:53:13 +0000109Decl *
110TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
111 NamespaceAliasDecl *Inst
112 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
113 D->getNamespaceLoc(),
114 D->getAliasLoc(),
115 D->getNamespace()->getIdentifier(),
116 D->getQualifierRange(),
117 D->getQualifier(),
118 D->getTargetNameLoc(),
119 D->getNamespace());
120 Owner->addDecl(Inst);
121 return Inst;
122}
123
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000124Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
125 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000126 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000127 if (DI->getType()->isDependentType() ||
128 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000129 DI = SemaRef.SubstType(DI, TemplateArgs,
130 D->getLocation(), D->getDeclName());
131 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000132 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000133 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000134 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000135 } else {
136 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000139 // Create the new typedef
140 TypedefDecl *Typedef
141 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000142 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000143 if (Invalid)
144 Typedef->setInvalidDecl();
145
Douglas Gregord57a38e2010-04-23 16:25:07 +0000146 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
147 TagDecl *TD = TT->getDecl();
148
149 // If the TagDecl that the TypedefDecl points to is an anonymous decl
150 // keep track of the TypedefDecl.
151 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
152 TD->setTypedefForAnonDecl(Typedef);
153 }
154
John McCall5126fd02009-12-30 00:31:22 +0000155 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000156 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
157 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000158 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
159 }
160
John McCall1d8d1cc2010-08-01 02:01:53 +0000161 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000162
John McCall46460a62010-01-20 21:53:11 +0000163 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000164 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000166 return Typedef;
167}
168
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000169/// \brief Instantiate an initializer, breaking it into separate
170/// initialization arguments.
171///
172/// \param S The semantic analysis object.
173///
174/// \param Init The initializer to instantiate.
175///
176/// \param TemplateArgs Template arguments to be substituted into the
177/// initializer.
178///
179/// \param NewArgs Will be filled in with the instantiation arguments.
180///
181/// \returns true if an error occurred, false otherwise
182static bool InstantiateInitializer(Sema &S, Expr *Init,
183 const MultiLevelTemplateArgumentList &TemplateArgs,
184 SourceLocation &LParenLoc,
Nick Lewycky7663f392010-11-20 01:29:55 +0000185 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000186 SourceLocation &RParenLoc) {
187 NewArgs.clear();
188 LParenLoc = SourceLocation();
189 RParenLoc = SourceLocation();
190
191 if (!Init)
192 return false;
193
John McCall4765fa02010-12-06 08:20:24 +0000194 if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000195 Init = ExprTemp->getSubExpr();
196
197 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
198 Init = Binder->getSubExpr();
199
200 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
201 Init = ICE->getSubExprAsWritten();
202
203 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
204 LParenLoc = ParenList->getLParenLoc();
205 RParenLoc = ParenList->getRParenLoc();
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000206 return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
207 true, TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000208 }
209
210 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000211 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
Douglas Gregor91fc73e2011-01-07 19:35:17 +0000212 if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
213 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000214 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000215
Douglas Gregor28329e52010-03-24 21:22:47 +0000216 // FIXME: Fake locations!
217 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000218 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000219 return false;
220 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000221 }
222
John McCall60d7b3a2010-08-24 06:29:42 +0000223 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000224 if (Result.isInvalid())
225 return true;
226
227 NewArgs.push_back(Result.takeAs<Expr>());
228 return false;
229}
230
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000231Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000232 // If this is the variable for an anonymous struct or union,
233 // instantiate the anonymous struct/union type first.
234 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
235 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
236 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
237 return 0;
238
John McCallce3ff2b2009-08-25 22:02:44 +0000239 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000240 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000241 TemplateArgs,
242 D->getTypeSpecStartLoc(),
243 D->getDeclName());
244 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000245 return 0;
246
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000247 if (DI->getType()->isFunctionType()) {
248 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
249 << D->isStaticDataMember() << DI->getType();
250 return 0;
251 }
252
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000253 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000254 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
255 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000256 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000257 D->getStorageClass(),
258 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000259 Var->setThreadSpecified(D->isThreadSpecified());
260 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000261
John McCallb6217662010-03-15 10:12:16 +0000262 // Substitute the nested name specifier, if any.
263 if (SubstQualifier(D, Var))
264 return 0;
265
Mike Stump1eb44332009-09-09 15:08:12 +0000266 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000267 // out-of-line, the instantiation will have the same lexical
268 // context (which will be a namespace scope) as the template.
269 if (D->isOutOfLine())
270 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000271
John McCall46460a62010-01-20 21:53:11 +0000272 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000273
274 if (!D->isStaticDataMember())
275 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000276
Mike Stump390b4cc2009-05-16 07:39:55 +0000277 // FIXME: In theory, we could have a previous declaration for variables that
278 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000279 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000280 // FIXME: having to fake up a LookupResult is dumb.
281 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000282 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000283 if (D->isStaticDataMember())
284 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000285 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Douglas Gregor7caa6822009-07-24 20:34:43 +0000287 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000288 if (!D->isStaticDataMember())
289 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000290 Owner->makeDeclVisibleInContext(Var);
291 } else {
292 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000293 if (Owner->isFunctionOrMethod())
294 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000295 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000296 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000297
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000298 // Link instantiations of static data members back to the template from
299 // which they were instantiated.
300 if (Var->isStaticDataMember())
301 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000302 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000303
Douglas Gregor60c93c92010-02-09 07:26:29 +0000304 if (Var->getAnyInitializer()) {
305 // We already have an initializer in the class.
306 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000307 if (Var->isStaticDataMember() && !D->isOutOfLine())
308 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
309 else
310 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
311
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000312 // Instantiate the initializer.
313 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000314 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000315 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000316 InitArgs, RParenLoc)) {
Douglas Gregor07a77b42011-01-14 17:12:22 +0000317 // Attach the initializer to the declaration, if we have one.
318 if (InitArgs.size() == 0)
319 SemaRef.ActOnUninitializedDecl(Var, false);
320 else if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000321 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000322 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000323 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000324 move_arg(InitArgs),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000325 RParenLoc);
Douglas Gregor07a77b42011-01-14 17:12:22 +0000326 } else {
327 assert(InitArgs.size() == 1);
John McCall9ae2f072010-08-23 23:25:46 +0000328 Expr *Init = InitArgs.take()[0];
329 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000330 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000331 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000332 // FIXME: Not too happy about invalidating the declaration
333 // because of a bogus initializer.
334 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000335 }
336
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000337 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000338 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000339 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000340
Douglas Gregor5764f612010-05-08 23:05:03 +0000341 // Diagnose unused local variables.
342 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
343 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000344
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000345 return Var;
346}
347
Abramo Bagnara6206d532010-06-05 05:09:32 +0000348Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
349 AccessSpecDecl* AD
350 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
351 D->getAccessSpecifierLoc(), D->getColonLoc());
352 Owner->addHiddenDecl(AD);
353 return AD;
354}
355
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000356Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
357 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000358 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000359 if (DI->getType()->isDependentType() ||
360 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000361 DI = SemaRef.SubstType(DI, TemplateArgs,
362 D->getLocation(), D->getDeclName());
363 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000364 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000365 Invalid = true;
366 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000367 // C++ [temp.arg.type]p3:
368 // If a declaration acquires a function type through a type
369 // dependent on a template-parameter and this causes a
370 // declaration that does not use the syntactic form of a
371 // function declarator to have function type, the program is
372 // ill-formed.
373 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000374 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000375 Invalid = true;
376 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000377 } else {
378 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000379 }
380
381 Expr *BitWidth = D->getBitWidth();
382 if (Invalid)
383 BitWidth = 0;
384 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000385 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000386 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
John McCall60d7b3a2010-08-24 06:29:42 +0000388 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000389 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000390 if (InstantiatedBitWidth.isInvalid()) {
391 Invalid = true;
392 BitWidth = 0;
393 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000394 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000395 }
396
John McCall07fb6be2009-10-22 23:33:21 +0000397 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
398 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000399 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000400 D->getLocation(),
401 D->isMutable(),
402 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000403 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000404 D->getAccess(),
405 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000406 if (!Field) {
407 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000408 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
John McCall1d8d1cc2010-08-01 02:01:53 +0000411 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000412
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000413 if (Invalid)
414 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000416 if (!Field->getDeclName()) {
417 // Keep track of where this decl came from.
418 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000419 }
420 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
421 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000422 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000423 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000426 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000427 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000428 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000429
430 return Field;
431}
432
Francois Pichet87c2e122010-11-21 06:08:52 +0000433Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
434 NamedDecl **NamedChain =
435 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
436
437 int i = 0;
438 for (IndirectFieldDecl::chain_iterator PI =
439 D->chain_begin(), PE = D->chain_end();
440 PI != PE; ++PI)
441 NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
442 *PI, TemplateArgs));
443
Francois Pichet40e17752010-12-09 10:07:54 +0000444 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
Francois Pichet87c2e122010-11-21 06:08:52 +0000445 IndirectFieldDecl* IndirectField
446 = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Francois Pichet40e17752010-12-09 10:07:54 +0000447 D->getIdentifier(), T,
Francois Pichet87c2e122010-11-21 06:08:52 +0000448 NamedChain, D->getChainingSize());
449
450
451 IndirectField->setImplicit(D->isImplicit());
452 IndirectField->setAccess(D->getAccess());
453 Owner->addDecl(IndirectField);
454 return IndirectField;
455}
456
John McCall02cace72009-08-28 07:59:38 +0000457Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000458 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000459 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000460 if (TypeSourceInfo *Ty = D->getFriendType()) {
461 TypeSourceInfo *InstTy =
462 SemaRef.SubstType(Ty, TemplateArgs,
463 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000464 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000465 return 0;
John McCall02cace72009-08-28 07:59:38 +0000466
Douglas Gregor06245bf2010-04-07 17:57:12 +0000467 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
468 if (!FD)
469 return 0;
470
471 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000472 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000473 Owner->addDecl(FD);
474 return FD;
475 }
476
477 NamedDecl *ND = D->getFriendDecl();
478 assert(ND && "friend decl must be a decl or a type!");
479
John McCallaf2094e2010-04-08 09:05:18 +0000480 // All of the Visit implementations for the various potential friend
481 // declarations have to be carefully written to work for friend
482 // objects, with the most important detail being that the target
483 // decl should almost certainly not be placed in Owner.
484 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000485 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
John McCall02cace72009-08-28 07:59:38 +0000487 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000488 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
489 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000490 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000491 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000492 Owner->addDecl(FD);
493 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000494}
495
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000496Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
497 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregorac7610d2009-06-22 20:57:11 +0000499 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000500 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
John McCall60d7b3a2010-08-24 06:29:42 +0000502 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000503 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000504 if (InstantiatedAssertExpr.isInvalid())
505 return 0;
506
John McCall60d7b3a2010-08-24 06:29:42 +0000507 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000508 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000509 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000510 InstantiatedAssertExpr.get(),
511 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000512}
513
514Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000515 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000516 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000517 D->getTagKeywordLoc(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000518 /*PrevDecl=*/0, D->isScoped(),
519 D->isScopedUsingClassTag(), D->isFixed());
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000520 if (D->isFixed()) {
521 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
522 // If we have type source information for the underlying type, it means it
523 // has been explicitly set by the user. Perform substitution on it before
524 // moving on.
525 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
526 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
527 TemplateArgs,
528 UnderlyingLoc,
529 DeclarationName()));
530
531 if (!Enum->getIntegerTypeSourceInfo())
532 Enum->setIntegerType(SemaRef.Context.IntTy);
533 }
534 else {
535 assert(!D->getIntegerType()->isDependentType()
536 && "Dependent type without type source info");
537 Enum->setIntegerType(D->getIntegerType());
538 }
539 }
540
John McCall5b629aa2010-10-22 23:36:17 +0000541 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
542
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000543 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000544 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000545 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000547 Enum->startDefinition();
548
Douglas Gregor96084f12010-03-01 19:00:07 +0000549 if (D->getDeclContext()->isFunctionOrMethod())
550 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
551
John McCalld226f652010-08-21 09:40:31 +0000552 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000553
554 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000555 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
556 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000557 EC != ECEnd; ++EC) {
558 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000559 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000560 if (Expr *UninstValue = EC->getInitExpr()) {
561 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000562 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000563 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
John McCallce3ff2b2009-08-25 22:02:44 +0000565 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000566 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000567
568 // Drop the initial value and continue.
569 bool isInvalid = false;
570 if (Value.isInvalid()) {
571 Value = SemaRef.Owned((Expr *)0);
572 isInvalid = true;
573 }
574
Mike Stump1eb44332009-09-09 15:08:12 +0000575 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000576 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
577 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000578 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000579
580 if (isInvalid) {
581 if (EnumConst)
582 EnumConst->setInvalidDecl();
583 Enum->setInvalidDecl();
584 }
585
586 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000587 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
588
John McCall3b85ecf2010-01-23 22:37:59 +0000589 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000590 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000591 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000592 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000593
594 if (D->getDeclContext()->isFunctionOrMethod()) {
595 // If the enumeration is within a function or method, record the enum
596 // constant as a local.
597 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
598 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000599 }
600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000602 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000603 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000604 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000605 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000606 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000607 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000608
609 return Enum;
610}
611
Douglas Gregor6477b692009-03-25 15:04:13 +0000612Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
613 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
614 return 0;
615}
616
John McCalle29ba202009-08-20 01:44:21 +0000617Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000618 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
619
Douglas Gregor550d9b22009-10-31 17:21:17 +0000620 // Create a local instantiation scope for this class template, which
621 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000622 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000623 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000624 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000625 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000626 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000627
628 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000629
630 // Instantiate the qualifier. We have to do this first in case
631 // we're a friend declaration, because if we are then we need to put
632 // the new declaration in the appropriate context.
633 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
634 if (Qualifier) {
635 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
636 Pattern->getQualifierRange(),
637 TemplateArgs);
638 if (!Qualifier) return 0;
639 }
640
641 CXXRecordDecl *PrevDecl = 0;
642 ClassTemplateDecl *PrevClassTemplate = 0;
643
Nick Lewycky37574f52010-11-08 23:29:42 +0000644 if (!isFriend && Pattern->getPreviousDeclaration()) {
645 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
646 if (Found.first != Found.second) {
647 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
648 if (PrevClassTemplate)
649 PrevDecl = PrevClassTemplate->getTemplatedDecl();
650 }
651 }
652
John McCall93ba8572010-03-25 06:39:04 +0000653 // If this isn't a friend, then it's a member template, in which
654 // case we just want to build the instantiation in the
655 // specialization. If it is a friend, we want to build it in
656 // the appropriate context.
657 DeclContext *DC = Owner;
658 if (isFriend) {
659 if (Qualifier) {
660 CXXScopeSpec SS;
661 SS.setScopeRep(Qualifier);
662 SS.setRange(Pattern->getQualifierRange());
663 DC = SemaRef.computeDeclContext(SS);
664 if (!DC) return 0;
665 } else {
666 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
667 Pattern->getDeclContext(),
668 TemplateArgs);
669 }
670
671 // Look for a previous declaration of the template in the owning
672 // context.
673 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
674 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
675 SemaRef.LookupQualifiedName(R, DC);
676
677 if (R.isSingleResult()) {
678 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
679 if (PrevClassTemplate)
680 PrevDecl = PrevClassTemplate->getTemplatedDecl();
681 }
682
683 if (!PrevClassTemplate && Qualifier) {
684 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000685 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
686 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000687 return 0;
688 }
689
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000690 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000691 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000692 bool Complain = true;
693
694 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
695 // template for struct std::tr1::__detail::_Map_base, where the
696 // template parameters of the friend declaration don't match the
697 // template parameters of the original declaration. In this one
698 // case, we don't complain about the ill-formed friend
699 // declaration.
700 if (isFriend && Pattern->getIdentifier() &&
701 Pattern->getIdentifier()->isStr("_Map_base") &&
702 DC->isNamespace() &&
703 cast<NamespaceDecl>(DC)->getIdentifier() &&
704 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
705 DeclContext *DCParent = DC->getParent();
706 if (DCParent->isNamespace() &&
707 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
708 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
709 DeclContext *DCParent2 = DCParent->getParent();
710 if (DCParent2->isNamespace() &&
711 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
712 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
713 DCParent2->getParent()->isTranslationUnit())
714 Complain = false;
715 }
716 }
717
John McCall93ba8572010-03-25 06:39:04 +0000718 TemplateParameterList *PrevParams
719 = PrevClassTemplate->getTemplateParameters();
720
721 // Make sure the parameter lists match.
722 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000723 Complain,
724 Sema::TPL_TemplateMatch)) {
725 if (Complain)
726 return 0;
727
728 AdoptedPreviousTemplateParams = true;
729 InstParams = PrevParams;
730 }
John McCall93ba8572010-03-25 06:39:04 +0000731
732 // Do some additional validation, then merge default arguments
733 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000734 if (!AdoptedPreviousTemplateParams &&
735 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000736 Sema::TPC_ClassTemplate))
737 return 0;
738 }
739 }
740
John McCalle29ba202009-08-20 01:44:21 +0000741 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000742 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000743 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000744 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000745 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000746
John McCall93ba8572010-03-25 06:39:04 +0000747 if (Qualifier)
748 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000749
John McCalle29ba202009-08-20 01:44:21 +0000750 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000751 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
752 D->getIdentifier(), InstParams, RecordInst,
753 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000754 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000755
John McCall93ba8572010-03-25 06:39:04 +0000756 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000757 if (PrevClassTemplate)
758 Inst->setAccess(PrevClassTemplate->getAccess());
759 else
760 Inst->setAccess(D->getAccess());
761
John McCall93ba8572010-03-25 06:39:04 +0000762 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
763 // TODO: do we want to track the instantiation progeny of this
764 // friend target decl?
765 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000766 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000767 if (!PrevClassTemplate)
768 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000769 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000770
771 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000772 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000773 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000774
Douglas Gregor259571e2009-10-30 22:42:42 +0000775 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000776 if (isFriend) {
777 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000778 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000779 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000780
John McCalle29ba202009-08-20 01:44:21 +0000781 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000782
783 if (!PrevClassTemplate) {
784 // Queue up any out-of-line partial specializations of this member
785 // class template; the client will force their instantiation once
786 // the enclosing class has been instantiated.
787 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
788 D->getPartialSpecializations(PartialSpecs);
789 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
790 if (PartialSpecs[I]->isOutOfLine())
791 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
792 }
793
John McCalle29ba202009-08-20 01:44:21 +0000794 return Inst;
795}
796
Douglas Gregord60e1052009-08-27 16:57:43 +0000797Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000798TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
799 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000800 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
801
802 // Lookup the already-instantiated declaration in the instantiation
803 // of the class template and return that.
804 DeclContext::lookup_result Found
805 = Owner->lookup(ClassTemplate->getDeclName());
806 if (Found.first == Found.second)
807 return 0;
808
809 ClassTemplateDecl *InstClassTemplate
810 = dyn_cast<ClassTemplateDecl>(*Found.first);
811 if (!InstClassTemplate)
812 return 0;
813
Douglas Gregord65587f2010-11-10 19:44:59 +0000814 if (ClassTemplatePartialSpecializationDecl *Result
815 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
816 return Result;
817
818 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000819}
820
821Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000822TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000823 // Create a local instantiation scope for this function template, which
824 // will contain the instantiations of the template parameters and then get
825 // merged with the local instantiation scope for the function template
826 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000827 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000828
Douglas Gregord60e1052009-08-27 16:57:43 +0000829 TemplateParameterList *TempParams = D->getTemplateParameters();
830 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000831 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000832 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000833
Douglas Gregora735b202009-10-13 14:39:41 +0000834 FunctionDecl *Instantiated = 0;
835 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
836 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
837 InstParams));
838 else
839 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
840 D->getTemplatedDecl(),
841 InstParams));
842
843 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000844 return 0;
845
John McCall46460a62010-01-20 21:53:11 +0000846 Instantiated->setAccess(D->getAccess());
847
Mike Stump1eb44332009-09-09 15:08:12 +0000848 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000849 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000850 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000851 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000852 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000853 assert(InstTemplate &&
854 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000855
John McCallb1a56e72010-03-26 23:10:15 +0000856 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
857
John McCalle976ffe2009-12-14 23:19:40 +0000858 // Link the instantiation back to the pattern *unless* this is a
859 // non-definition friend declaration.
860 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000861 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000862 InstTemplate->setInstantiatedFromMemberTemplate(D);
863
John McCallb1a56e72010-03-26 23:10:15 +0000864 // Make declarations visible in the appropriate context.
865 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000866 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000867
Douglas Gregord60e1052009-08-27 16:57:43 +0000868 return InstTemplate;
869}
870
Douglas Gregord475b8d2009-03-25 21:17:03 +0000871Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
872 CXXRecordDecl *PrevDecl = 0;
873 if (D->isInjectedClassName())
874 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000875 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000876 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
877 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000878 TemplateArgs);
879 if (!Prev) return 0;
880 PrevDecl = cast<CXXRecordDecl>(Prev);
881 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000882
883 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000884 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000885 D->getLocation(), D->getIdentifier(),
886 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000887
888 // Substitute the nested name specifier, if any.
889 if (SubstQualifier(D, Record))
890 return 0;
891
Douglas Gregord475b8d2009-03-25 21:17:03 +0000892 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000893 // FIXME: Check against AS_none is an ugly hack to work around the issue that
894 // the tag decls introduced by friend class declarations don't have an access
895 // specifier. Remove once this area of the code gets sorted out.
896 if (D->getAccess() != AS_none)
897 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000898 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000899 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000900
John McCall02cace72009-08-28 07:59:38 +0000901 // If the original function was part of a friend declaration,
902 // inherit its namespace state.
903 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
904 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
905
Douglas Gregor9901c572010-05-21 00:31:19 +0000906 // Make sure that anonymous structs and unions are recorded.
907 if (D->isAnonymousStructOrUnion()) {
908 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000909 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000910 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
911 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000912
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000913 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000914 return Record;
915}
916
John McCall02cace72009-08-28 07:59:38 +0000917/// Normal class members are of more specific types and therefore
918/// don't make it here. This function serves two purposes:
919/// 1) instantiating function templates
920/// 2) substituting friend declarations
921/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000922Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000923 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000924 // Check whether there is already a function template specialization for
925 // this declaration.
926 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
927 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000928 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000929 std::pair<const TemplateArgument *, unsigned> Innermost
930 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000932 FunctionDecl *SpecFunc
933 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
934 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor127102b2009-06-29 20:59:39 +0000936 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000937 if (SpecFunc)
938 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
John McCallb0cb0222010-03-27 05:57:59 +0000941 bool isFriend;
942 if (FunctionTemplate)
943 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
944 else
945 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
946
Douglas Gregor79c22782010-01-16 20:21:20 +0000947 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000948 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000949 !(isa<Decl>(Owner) &&
950 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000951 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Douglas Gregore53060f2009-06-25 22:08:12 +0000953 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000954 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
955 TInfo = SubstFunctionType(D, Params);
956 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000957 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000958 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000959
John McCalld325daa2010-03-26 04:53:08 +0000960 NestedNameSpecifier *Qualifier = D->getQualifier();
961 if (Qualifier) {
962 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
963 D->getQualifierRange(),
964 TemplateArgs);
965 if (!Qualifier) return 0;
966 }
967
John McCall68b6b872010-02-06 01:50:47 +0000968 // If we're instantiating a local function declaration, put the result
969 // in the owner; otherwise we need to find the instantiated context.
970 DeclContext *DC;
971 if (D->getDeclContext()->isFunctionOrMethod())
972 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000973 else if (isFriend && Qualifier) {
974 CXXScopeSpec SS;
975 SS.setScopeRep(Qualifier);
976 SS.setRange(D->getQualifierRange());
977 DC = SemaRef.computeDeclContext(SS);
978 if (!DC) return 0;
979 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000980 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
981 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000982 }
John McCall68b6b872010-02-06 01:50:47 +0000983
John McCall02cace72009-08-28 07:59:38 +0000984 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000985 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000986 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000987 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000988 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000989
John McCalld325daa2010-03-26 04:53:08 +0000990 if (Qualifier)
991 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000992
John McCallb1a56e72010-03-26 23:10:15 +0000993 DeclContext *LexicalDC = Owner;
994 if (!isFriend && D->isOutOfLine()) {
995 assert(D->getDeclContext()->isFileContext());
996 LexicalDC = D->getDeclContext();
997 }
998
999 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregore53060f2009-06-25 22:08:12 +00001001 // Attach the parameters
1002 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001003 if (Params[P])
1004 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001005 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001006
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001007 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001008 if (TemplateParams) {
1009 // Our resulting instantiation is actually a function template, since we
1010 // are substituting only the outer template parameters. For example, given
1011 //
1012 // template<typename T>
1013 // struct X {
1014 // template<typename U> friend void f(T, U);
1015 // };
1016 //
1017 // X<int> x;
1018 //
1019 // We are instantiating the friend function template "f" within X<int>,
1020 // which means substituting int for T, but leaving "f" as a friend function
1021 // template.
1022 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001023 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001024 Function->getLocation(),
1025 Function->getDeclName(),
1026 TemplateParams, Function);
1027 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001028
1029 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001030
1031 if (isFriend && D->isThisDeclarationADefinition()) {
1032 // TODO: should we remember this connection regardless of whether
1033 // the friend declaration provided a body?
1034 FunctionTemplate->setInstantiatedFromMemberTemplate(
1035 D->getDescribedFunctionTemplate());
1036 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001037 } else if (FunctionTemplate) {
1038 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001039 std::pair<const TemplateArgument *, unsigned> Innermost
1040 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001041 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001042 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001043 Innermost.first,
1044 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001045 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001046 } else if (isFriend && D->isThisDeclarationADefinition()) {
1047 // TODO: should we remember this connection regardless of whether
1048 // the friend declaration provided a body?
1049 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001050 }
Douglas Gregora735b202009-10-13 14:39:41 +00001051
Douglas Gregore53060f2009-06-25 22:08:12 +00001052 if (InitFunctionInstantiation(Function, D))
1053 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Douglas Gregore53060f2009-06-25 22:08:12 +00001055 bool Redeclaration = false;
1056 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001057 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001058
John McCall68263142009-11-18 22:49:29 +00001059 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1060 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1061
John McCallaf2094e2010-04-08 09:05:18 +00001062 if (DependentFunctionTemplateSpecializationInfo *Info
1063 = D->getDependentSpecializationInfo()) {
1064 assert(isFriend && "non-friend has dependent specialization info?");
1065
1066 // This needs to be set now for future sanity.
1067 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1068
1069 // Instantiate the explicit template arguments.
1070 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1071 Info->getRAngleLoc());
Douglas Gregore02e2622010-12-22 21:19:48 +00001072 if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1073 ExplicitArgs, TemplateArgs))
1074 return 0;
John McCallaf2094e2010-04-08 09:05:18 +00001075
1076 // Map the candidate templates to their instantiations.
1077 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1078 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1079 Info->getTemplate(I),
1080 TemplateArgs);
1081 if (!Temp) return 0;
1082
1083 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1084 }
1085
1086 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1087 &ExplicitArgs,
1088 Previous))
1089 Function->setInvalidDecl();
1090
1091 isExplicitSpecialization = true;
1092
1093 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001094 // Look only into the namespace where the friend would be declared to
1095 // find a previous declaration. This is the innermost enclosing namespace,
1096 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001097 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001098
Douglas Gregora735b202009-10-13 14:39:41 +00001099 // In C++, the previous declaration we find might be a tag type
1100 // (class or enum). In this case, the new declaration will hide the
1101 // tag type. Note that this does does not apply if we're declaring a
1102 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001103 if (Previous.isSingleTagDecl())
1104 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001105 }
1106
John McCall9f54ad42009-12-10 09:41:52 +00001107 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001108 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001109 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001110
John McCall76d32642010-04-24 01:30:58 +00001111 NamedDecl *PrincipalDecl = (TemplateParams
1112 ? cast<NamedDecl>(FunctionTemplate)
1113 : Function);
1114
Douglas Gregora735b202009-10-13 14:39:41 +00001115 // If the original function was part of a friend declaration,
1116 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001117 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001118 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001119 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001120 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001121 else
Douglas Gregora735b202009-10-13 14:39:41 +00001122 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001123
1124 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1125 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001126
Gabor Greif77535df2010-08-30 22:25:56 +00001127 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001128
Douglas Gregor238058c2010-05-18 05:45:02 +00001129 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1130 D->isThisDeclarationADefinition()) {
1131 // Check for a function body.
1132 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001133 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001134 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1135 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1136 << Function->getDeclName();
1137 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1138 Function->setInvalidDecl();
1139 }
1140 // Check for redefinitions due to other instantiations of this or
1141 // a similar friend function.
1142 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1143 REnd = Function->redecls_end();
1144 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001145 if (*R == Function)
1146 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001147 switch (R->getFriendObjectKind()) {
1148 case Decl::FOK_None:
1149 if (!queuedInstantiation && R->isUsed(false)) {
1150 if (MemberSpecializationInfo *MSInfo
1151 = Function->getMemberSpecializationInfo()) {
1152 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1153 SourceLocation Loc = R->getLocation(); // FIXME
1154 MSInfo->setPointOfInstantiation(Loc);
1155 SemaRef.PendingLocalImplicitInstantiations.push_back(
1156 std::make_pair(Function, Loc));
1157 queuedInstantiation = true;
1158 }
1159 }
1160 }
1161 break;
1162 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001163 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001164 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001165 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001166 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1167 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001168 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001169 Function->setInvalidDecl();
1170 break;
1171 }
1172 }
1173 }
1174 }
Douglas Gregora735b202009-10-13 14:39:41 +00001175 }
1176
John McCall76d32642010-04-24 01:30:58 +00001177 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1178 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1179 PrincipalDecl->setNonMemberOperator();
1180
Douglas Gregore53060f2009-06-25 22:08:12 +00001181 return Function;
1182}
1183
Douglas Gregord60e1052009-08-27 16:57:43 +00001184Decl *
1185TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1186 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001187 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1188 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001189 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001190 // We are creating a function template specialization from a function
1191 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001192 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001193 std::pair<const TemplateArgument *, unsigned> Innermost
1194 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001196 FunctionDecl *SpecFunc
1197 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1198 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Douglas Gregor6b906862009-08-21 00:16:32 +00001200 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001201 if (SpecFunc)
1202 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001203 }
1204
John McCallb0cb0222010-03-27 05:57:59 +00001205 bool isFriend;
1206 if (FunctionTemplate)
1207 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1208 else
1209 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1210
Douglas Gregor79c22782010-01-16 20:21:20 +00001211 bool MergeWithParentScope = (TemplateParams != 0) ||
1212 !(isa<Decl>(Owner) &&
1213 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001214 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001215
John McCall4eab39f2010-10-19 02:26:41 +00001216 // Instantiate enclosing template arguments for friends.
1217 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1218 unsigned NumTempParamLists = 0;
1219 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1220 TempParamLists.set_size(NumTempParamLists);
1221 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1222 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1223 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1224 if (!InstParams)
1225 return NULL;
1226 TempParamLists[I] = InstParams;
1227 }
1228 }
1229
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001230 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001231 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1232 TInfo = SubstFunctionType(D, Params);
1233 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001234 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001235 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001236
Abramo Bagnara723df242010-12-14 22:11:44 +00001237 // \brief If the type of this function, after ignoring parentheses,
1238 // is not *directly* a function type, then we're instantiating a function
1239 // that was declared via a typedef, e.g.,
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001240 //
1241 // typedef int functype(int, int);
1242 // functype func;
1243 //
1244 // In this case, we'll just go instantiate the ParmVarDecls that we
1245 // synthesized in the method declaration.
Abramo Bagnara723df242010-12-14 22:11:44 +00001246 if (!isa<FunctionProtoType>(T.IgnoreParens())) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001247 assert(!Params.size() && "Instantiating type could not yield parameters");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001248 llvm::SmallVector<QualType, 4> ParamTypes;
1249 if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1250 D->getNumParams(), TemplateArgs, ParamTypes,
1251 &Params))
1252 return 0;
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001253 }
1254
John McCallb0cb0222010-03-27 05:57:59 +00001255 NestedNameSpecifier *Qualifier = D->getQualifier();
1256 if (Qualifier) {
1257 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1258 D->getQualifierRange(),
1259 TemplateArgs);
1260 if (!Qualifier) return 0;
1261 }
1262
1263 DeclContext *DC = Owner;
1264 if (isFriend) {
1265 if (Qualifier) {
1266 CXXScopeSpec SS;
1267 SS.setScopeRep(Qualifier);
1268 SS.setRange(D->getQualifierRange());
1269 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001270
1271 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1272 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001273 } else {
1274 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1275 D->getDeclContext(),
1276 TemplateArgs);
1277 }
1278 if (!DC) return 0;
1279 }
1280
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001281 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001282 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001283 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Abramo Bagnara25777432010-08-11 22:01:17 +00001285 DeclarationNameInfo NameInfo
1286 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001287 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001288 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001289 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001290 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001291 Constructor->isInlineSpecified(),
1292 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001293 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001294 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001295 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001296 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001297 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001298 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001299 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001300 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001301 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001302 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001303 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001304 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1305 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001306 D->isStatic(),
1307 D->getStorageClassAsWritten(),
1308 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001309 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001310
John McCallb0cb0222010-03-27 05:57:59 +00001311 if (Qualifier)
1312 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001313
Douglas Gregord60e1052009-08-27 16:57:43 +00001314 if (TemplateParams) {
1315 // Our resulting instantiation is actually a function template, since we
1316 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001317 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001318 // template<typename T>
1319 // struct X {
1320 // template<typename U> void f(T, U);
1321 // };
1322 //
1323 // X<int> x;
1324 //
1325 // We are instantiating the member template "f" within X<int>, which means
1326 // substituting int for T, but leaving "f" as a member function template.
1327 // Build the function template itself.
1328 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1329 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001330 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001331 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001332 if (isFriend) {
1333 FunctionTemplate->setLexicalDeclContext(Owner);
1334 FunctionTemplate->setObjectOfFriendDecl(true);
1335 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001336 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001337 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001338 } else if (FunctionTemplate) {
1339 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001340 std::pair<const TemplateArgument *, unsigned> Innermost
1341 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001342 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001343 TemplateArgumentList::CreateCopy(SemaRef.Context,
1344 Innermost.first,
1345 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001346 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001347 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001348 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001349 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001350 }
1351
Mike Stump1eb44332009-09-09 15:08:12 +00001352 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001353 // out-of-line, the instantiation will have the same lexical
1354 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001355 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001356 if (NumTempParamLists)
1357 Method->setTemplateParameterListsInfo(SemaRef.Context,
1358 NumTempParamLists,
1359 TempParamLists.data());
1360
John McCallb0cb0222010-03-27 05:57:59 +00001361 Method->setLexicalDeclContext(Owner);
1362 Method->setObjectOfFriendDecl(true);
1363 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001364 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Douglas Gregor5545e162009-03-24 00:38:23 +00001366 // Attach the parameters
1367 for (unsigned P = 0; P < Params.size(); ++P)
1368 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001369 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001370
1371 if (InitMethodInstantiation(Method, D))
1372 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001373
Abramo Bagnara25777432010-08-11 22:01:17 +00001374 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1375 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001376
John McCallb0cb0222010-03-27 05:57:59 +00001377 if (!FunctionTemplate || TemplateParams || isFriend) {
1378 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregordec06662009-08-21 18:42:58 +00001380 // In C++, the previous declaration we find might be a tag type
1381 // (class or enum). In this case, the new declaration will hide the
1382 // tag type. Note that this does does not apply if we're declaring a
1383 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001384 if (Previous.isSingleTagDecl())
1385 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001386 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001387
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001388 bool Redeclaration = false;
1389 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001390 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001391 /*FIXME:*/OverloadableAttrRequired);
1392
Douglas Gregor4ba31362009-12-01 17:24:26 +00001393 if (D->isPure())
1394 SemaRef.CheckPureMethod(Method, SourceRange());
1395
Anders Carlsson9eefa222011-01-20 06:52:44 +00001396 Method->setIsMarkedOverride(D->isMarkedOverride());
1397 Method->setIsMarkedFinal(D->isMarkedFinal());
John McCall46460a62010-01-20 21:53:11 +00001398 Method->setAccess(D->getAccess());
1399
Anders Carlsson9eefa222011-01-20 06:52:44 +00001400 SemaRef.CheckOverrideControl(Method);
1401
John McCallb0cb0222010-03-27 05:57:59 +00001402 if (FunctionTemplate) {
1403 // If there's a function template, let our caller handle it.
1404 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1405 // Don't hide a (potentially) valid declaration with an invalid one.
1406 } else {
1407 NamedDecl *DeclToAdd = (TemplateParams
1408 ? cast<NamedDecl>(FunctionTemplate)
1409 : Method);
1410 if (isFriend)
1411 Record->makeDeclVisibleInContext(DeclToAdd);
1412 else
1413 Owner->addDecl(DeclToAdd);
1414 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001415
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001416 return Method;
1417}
1418
Douglas Gregor615c5d42009-03-24 16:43:20 +00001419Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001420 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001421}
1422
Douglas Gregor03b2b072009-03-24 00:15:49 +00001423Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001424 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001425}
1426
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001427Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001428 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001429}
1430
Douglas Gregor6477b692009-03-25 15:04:13 +00001431ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001432 return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001433}
1434
John McCalle29ba202009-08-20 01:44:21 +00001435Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1436 TemplateTypeParmDecl *D) {
1437 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001438 const Type* T = D->getTypeForDecl();
1439 assert(T->isTemplateTypeParmType());
1440 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001441
John McCalle29ba202009-08-20 01:44:21 +00001442 TemplateTypeParmDecl *Inst =
1443 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001444 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001445 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001446 D->wasDeclaredWithTypename(),
1447 D->isParameterPack());
1448
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001449 if (D->hasDefaultArgument())
1450 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001451
Douglas Gregor550d9b22009-10-31 17:21:17 +00001452 // Introduce this template parameter's instantiation into the instantiation
1453 // scope.
1454 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1455
John McCalle29ba202009-08-20 01:44:21 +00001456 return Inst;
1457}
1458
Douglas Gregor33642df2009-10-23 23:25:44 +00001459Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1460 NonTypeTemplateParmDecl *D) {
1461 // Substitute into the type of the non-type template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001462 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1463 llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1464 llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
1465 bool IsExpandedParameterPack = false;
1466 TypeSourceInfo *DI;
Douglas Gregor33642df2009-10-23 23:25:44 +00001467 QualType T;
Douglas Gregor33642df2009-10-23 23:25:44 +00001468 bool Invalid = false;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001469
1470 if (D->isExpandedParameterPack()) {
1471 // The non-type template parameter pack is an already-expanded pack
1472 // expansion of types. Substitute into each of the expanded types.
1473 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1474 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1475 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1476 TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1477 TemplateArgs,
1478 D->getLocation(),
1479 D->getDeclName());
1480 if (!NewDI)
1481 return 0;
1482
1483 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1484 QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1485 D->getLocation());
1486 if (NewT.isNull())
1487 return 0;
1488 ExpandedParameterPackTypes.push_back(NewT);
1489 }
1490
1491 IsExpandedParameterPack = true;
1492 DI = D->getTypeSourceInfo();
1493 T = DI->getType();
1494 } else if (isa<PackExpansionTypeLoc>(TL)) {
1495 // The non-type template parameter pack's type is a pack expansion of types.
1496 // Determine whether we need to expand this parameter pack into separate
1497 // types.
1498 PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
1499 TypeLoc Pattern = Expansion.getPatternLoc();
1500 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1501 SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1502
1503 // Determine whether the set of unexpanded parameter packs can and should
1504 // be expanded.
1505 bool Expand = true;
1506 bool RetainExpansion = false;
1507 llvm::Optional<unsigned> OrigNumExpansions
1508 = Expansion.getTypePtr()->getNumExpansions();
1509 llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
1510 if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1511 Pattern.getSourceRange(),
1512 Unexpanded.data(),
1513 Unexpanded.size(),
1514 TemplateArgs,
1515 Expand, RetainExpansion,
1516 NumExpansions))
1517 return 0;
1518
1519 if (Expand) {
1520 for (unsigned I = 0; I != *NumExpansions; ++I) {
1521 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1522 TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1523 D->getLocation(),
1524 D->getDeclName());
1525 if (!NewDI)
1526 return 0;
1527
1528 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1529 QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1530 NewDI->getType(),
1531 D->getLocation());
1532 if (NewT.isNull())
1533 return 0;
1534 ExpandedParameterPackTypes.push_back(NewT);
1535 }
1536
1537 // Note that we have an expanded parameter pack. The "type" of this
1538 // expanded parameter pack is the original expansion type, but callers
1539 // will end up using the expanded parameter pack types for type-checking.
1540 IsExpandedParameterPack = true;
1541 DI = D->getTypeSourceInfo();
1542 T = DI->getType();
1543 } else {
1544 // We cannot fully expand the pack expansion now, so substitute into the
1545 // pattern and create a new pack expansion type.
1546 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1547 TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1548 D->getLocation(),
1549 D->getDeclName());
1550 if (!NewPattern)
1551 return 0;
1552
1553 DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1554 NumExpansions);
1555 if (!DI)
1556 return 0;
1557
1558 T = DI->getType();
1559 }
1560 } else {
1561 // Simple case: substitution into a parameter that is not a parameter pack.
1562 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1563 D->getLocation(), D->getDeclName());
1564 if (!DI)
1565 return 0;
1566
1567 // Check that this type is acceptable for a non-type template parameter.
1568 bool Invalid = false;
1569 T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
1570 D->getLocation());
1571 if (T.isNull()) {
1572 T = SemaRef.Context.IntTy;
1573 Invalid = true;
1574 }
Douglas Gregor33642df2009-10-23 23:25:44 +00001575 }
1576
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001577 NonTypeTemplateParmDecl *Param;
1578 if (IsExpandedParameterPack)
1579 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1580 D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001581 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001582 D->getPosition(),
1583 D->getIdentifier(), T,
1584 DI,
1585 ExpandedParameterPackTypes.data(),
1586 ExpandedParameterPackTypes.size(),
1587 ExpandedParameterPackTypesAsWritten.data());
1588 else
1589 Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1590 D->getLocation(),
1591 D->getDepth() - TemplateArgs.getNumLevels(),
1592 D->getPosition(),
1593 D->getIdentifier(), T,
1594 D->isParameterPack(), DI);
1595
Douglas Gregor33642df2009-10-23 23:25:44 +00001596 if (Invalid)
1597 Param->setInvalidDecl();
1598
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001599 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001600
1601 // Introduce this template parameter's instantiation into the instantiation
1602 // scope.
1603 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001604 return Param;
1605}
1606
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001607Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001608TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1609 TemplateTemplateParmDecl *D) {
1610 // Instantiate the template parameter list of the template template parameter.
1611 TemplateParameterList *TempParams = D->getTemplateParameters();
1612 TemplateParameterList *InstParams;
1613 {
1614 // Perform the actual substitution of template parameters within a new,
1615 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001616 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001617 InstParams = SubstTemplateParams(TempParams);
1618 if (!InstParams)
1619 return NULL;
1620 }
1621
1622 // Build the template template parameter.
1623 TemplateTemplateParmDecl *Param
1624 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001625 D->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00001626 D->getPosition(), D->isParameterPack(),
1627 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001628 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001629
Douglas Gregor9106ef72009-11-11 16:58:32 +00001630 // Introduce this template parameter's instantiation into the instantiation
1631 // scope.
1632 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1633
1634 return Param;
1635}
1636
Douglas Gregor48c32a72009-11-17 06:07:40 +00001637Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1638 // Using directives are never dependent, so they require no explicit
1639
1640 UsingDirectiveDecl *Inst
1641 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1642 D->getNamespaceKeyLocation(),
1643 D->getQualifierRange(), D->getQualifier(),
1644 D->getIdentLocation(),
1645 D->getNominatedNamespace(),
1646 D->getCommonAncestor());
1647 Owner->addDecl(Inst);
1648 return Inst;
1649}
1650
John McCalled976492009-12-04 22:46:56 +00001651Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001652
1653 // The nested name specifier may be dependent, for example
1654 // template <typename T> struct t {
1655 // struct s1 { T f1(); };
1656 // struct s2 : s1 { using s1::f1; };
1657 // };
1658 // template struct t<int>;
1659 // Here, in using s1::f1, s1 refers to t<T>::s1;
1660 // we need to substitute for t<int>::s1.
1661 NestedNameSpecifier *NNS =
1662 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
1663 D->getNestedNameRange(),
1664 TemplateArgs);
1665 if (!NNS)
1666 return 0;
1667
1668 // The name info is non-dependent, so no transformation
1669 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001670 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001671
John McCall9f54ad42009-12-10 09:41:52 +00001672 // We only need to do redeclaration lookups if we're in a class
1673 // scope (in fact, it's not really even possible in non-class
1674 // scopes).
1675 bool CheckRedeclaration = Owner->isRecord();
1676
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001677 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1678 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001679
John McCalled976492009-12-04 22:46:56 +00001680 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001681 D->getNestedNameRange(),
1682 D->getUsingLocation(),
Douglas Gregor1b398202010-09-29 17:58:28 +00001683 NNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001684 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001685 D->isTypeName());
1686
1687 CXXScopeSpec SS;
Douglas Gregor1b398202010-09-29 17:58:28 +00001688 SS.setScopeRep(NNS);
John McCalled976492009-12-04 22:46:56 +00001689 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001690
1691 if (CheckRedeclaration) {
1692 Prev.setHideTags(false);
1693 SemaRef.LookupQualifiedName(Prev, Owner);
1694
1695 // Check for invalid redeclarations.
1696 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1697 D->isTypeName(), SS,
1698 D->getLocation(), Prev))
1699 NewUD->setInvalidDecl();
1700
1701 }
1702
1703 if (!NewUD->isInvalidDecl() &&
1704 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001705 D->getLocation()))
1706 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001707
John McCalled976492009-12-04 22:46:56 +00001708 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1709 NewUD->setAccess(D->getAccess());
1710 Owner->addDecl(NewUD);
1711
John McCall9f54ad42009-12-10 09:41:52 +00001712 // Don't process the shadow decls for an invalid decl.
1713 if (NewUD->isInvalidDecl())
1714 return NewUD;
1715
John McCall323c3102009-12-22 22:26:37 +00001716 bool isFunctionScope = Owner->isFunctionOrMethod();
1717
John McCall9f54ad42009-12-10 09:41:52 +00001718 // Process the shadow decls.
1719 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1720 I != E; ++I) {
1721 UsingShadowDecl *Shadow = *I;
1722 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001723 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1724 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001725 TemplateArgs));
1726
1727 if (CheckRedeclaration &&
1728 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1729 continue;
1730
1731 UsingShadowDecl *InstShadow
1732 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1733 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001734
1735 if (isFunctionScope)
1736 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001737 }
John McCalled976492009-12-04 22:46:56 +00001738
1739 return NewUD;
1740}
1741
1742Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001743 // Ignore these; we handle them in bulk when processing the UsingDecl.
1744 return 0;
John McCalled976492009-12-04 22:46:56 +00001745}
1746
John McCall7ba107a2009-11-18 02:36:19 +00001747Decl * TemplateDeclInstantiator
1748 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001749 NestedNameSpecifier *NNS =
1750 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1751 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001752 TemplateArgs);
1753 if (!NNS)
1754 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001756 CXXScopeSpec SS;
1757 SS.setRange(D->getTargetNestedNameRange());
1758 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001760 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1761 // Hence, no tranformation is required for it.
1762 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001763 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001764 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001765 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001766 /*instantiation*/ true,
1767 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001768 if (UD)
John McCalled976492009-12-04 22:46:56 +00001769 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1770
John McCall7ba107a2009-11-18 02:36:19 +00001771 return UD;
1772}
1773
1774Decl * TemplateDeclInstantiator
1775 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1776 NestedNameSpecifier *NNS =
1777 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1778 D->getTargetNestedNameRange(),
1779 TemplateArgs);
1780 if (!NNS)
1781 return 0;
1782
1783 CXXScopeSpec SS;
1784 SS.setRange(D->getTargetNestedNameRange());
1785 SS.setScopeRep(NNS);
1786
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001787 DeclarationNameInfo NameInfo
1788 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1789
John McCall7ba107a2009-11-18 02:36:19 +00001790 NamedDecl *UD =
1791 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001792 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001793 /*instantiation*/ true,
1794 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001795 if (UD)
John McCalled976492009-12-04 22:46:56 +00001796 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1797
Anders Carlsson0d8df782009-08-29 19:37:28 +00001798 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001799}
1800
John McCallce3ff2b2009-08-25 22:02:44 +00001801Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001802 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001803 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001804 if (D->isInvalidDecl())
1805 return 0;
1806
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001807 return Instantiator.Visit(D);
1808}
1809
John McCalle29ba202009-08-20 01:44:21 +00001810/// \brief Instantiates a nested template parameter list in the current
1811/// instantiation context.
1812///
1813/// \param L The parameter list to instantiate
1814///
1815/// \returns NULL if there was an error
1816TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001817TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001818 // Get errors for all the parameters before bailing out.
1819 bool Invalid = false;
1820
1821 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001822 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001823 ParamVector Params;
1824 Params.reserve(N);
1825 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1826 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001827 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001828 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001829 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001830 }
1831
1832 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001833 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001834 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001835
1836 TemplateParameterList *InstL
1837 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1838 L->getLAngleLoc(), &Params.front(), N,
1839 L->getRAngleLoc());
1840 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001841}
John McCalle29ba202009-08-20 01:44:21 +00001842
Douglas Gregored9c0f92009-10-29 00:04:11 +00001843/// \brief Instantiate the declaration of a class template partial
1844/// specialization.
1845///
1846/// \param ClassTemplate the (instantiated) class template that is partially
1847// specialized by the instantiation of \p PartialSpec.
1848///
1849/// \param PartialSpec the (uninstantiated) class template partial
1850/// specialization that we are instantiating.
1851///
Douglas Gregord65587f2010-11-10 19:44:59 +00001852/// \returns The instantiated partial specialization, if successful; otherwise,
1853/// NULL to indicate an error.
1854ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001855TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1856 ClassTemplateDecl *ClassTemplate,
1857 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001858 // Create a local instantiation scope for this class template partial
1859 // specialization, which will contain the instantiations of the template
1860 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001861 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001862
Douglas Gregored9c0f92009-10-29 00:04:11 +00001863 // Substitute into the template parameters of the class template partial
1864 // specialization.
1865 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1866 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1867 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001868 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001869
1870 // Substitute into the template arguments of the class template partial
1871 // specialization.
John McCalld5532b62009-11-23 01:53:49 +00001872 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
Douglas Gregore02e2622010-12-22 21:19:48 +00001873 if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1874 PartialSpec->getNumTemplateArgsAsWritten(),
1875 InstTemplateArgs, TemplateArgs))
1876 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001877
Douglas Gregored9c0f92009-10-29 00:04:11 +00001878 // Check that the template argument list is well-formed for this
1879 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001880 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001881 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1882 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001883 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001884 false,
1885 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001886 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001887
1888 // Figure out where to insert this class template partial specialization
1889 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001890 void *InsertPos = 0;
1891 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001892 = ClassTemplate->findPartialSpecialization(Converted.data(),
1893 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001894
1895 // Build the canonical type that describes the converted template
1896 // arguments of the class template partial specialization.
1897 QualType CanonType
1898 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001899 Converted.data(),
1900 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001901
1902 // Build the fully-sugared type for this class template
1903 // specialization as the user wrote in the specialization
1904 // itself. This means that we'll pretty-print the type retrieved
1905 // from the specialization's declaration the way that the user
1906 // actually wrote the specialization, rather than formatting the
1907 // name based on the "canonical" representation used to store the
1908 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001909 TypeSourceInfo *WrittenTy
1910 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1911 TemplateName(ClassTemplate),
1912 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001913 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001914 CanonType);
1915
1916 if (PrevDecl) {
1917 // We've already seen a partial specialization with the same template
1918 // parameters and template arguments. This can happen, for example, when
1919 // substituting the outer template arguments ends up causing two
1920 // class template partial specializations of a member class template
1921 // to have identical forms, e.g.,
1922 //
1923 // template<typename T, typename U>
1924 // struct Outer {
1925 // template<typename X, typename Y> struct Inner;
1926 // template<typename Y> struct Inner<T, Y>;
1927 // template<typename Y> struct Inner<U, Y>;
1928 // };
1929 //
1930 // Outer<int, int> outer; // error: the partial specializations of Inner
1931 // // have the same signature.
1932 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001933 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001934 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1935 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001936 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001937 }
1938
1939
1940 // Create the class template partial specialization declaration.
1941 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001942 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1943 PartialSpec->getTagKind(),
1944 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001945 PartialSpec->getLocation(),
1946 InstParams,
1947 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001948 Converted.data(),
1949 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001950 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001951 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001952 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001953 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001954 // Substitute the nested name specifier, if any.
1955 if (SubstQualifier(PartialSpec, InstPartialSpec))
1956 return 0;
1957
Douglas Gregored9c0f92009-10-29 00:04:11 +00001958 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001959 InstPartialSpec->setTypeAsWritten(WrittenTy);
1960
Douglas Gregored9c0f92009-10-29 00:04:11 +00001961 // Add this partial specialization to the set of class template partial
1962 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001963 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001964 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001965}
1966
John McCall21ef0fa2010-03-11 09:03:00 +00001967TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001968TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001969 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001970 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1971 assert(OldTInfo && "substituting function without type source info");
1972 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001973 TypeSourceInfo *NewTInfo
1974 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1975 D->getTypeSpecStartLoc(),
1976 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001977 if (!NewTInfo)
1978 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001979
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001980 if (NewTInfo != OldTInfo) {
1981 // Get parameters from the new type info.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001982 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001983 if (FunctionProtoTypeLoc *OldProtoLoc
1984 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00001985 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001986 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1987 assert(NewProtoLoc && "Missing prototype?");
Douglas Gregor12c9c002011-01-07 16:43:16 +00001988 unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
1989 for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
1990 OldIdx != NumOldParams; ++OldIdx) {
1991 ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
1992 if (!OldParam->isParameterPack() ||
1993 (NewIdx < NumNewParams &&
1994 NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
1995 // Simple case: normal parameter, or a parameter pack that's
1996 // instantiated to a (still-dependent) parameter pack.
1997 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
1998 Params.push_back(NewParam);
1999 SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
2000 NewParam);
2001 continue;
2002 }
2003
2004 // Parameter pack: make the instantiation an argument pack.
2005 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
2006 OldParam);
Douglas Gregor21371ea2011-01-11 03:14:20 +00002007 unsigned NumArgumentsInExpansion
2008 = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2009 TemplateArgs);
2010 while (NumArgumentsInExpansion--) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002011 ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
2012 Params.push_back(NewParam);
2013 SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
2014 NewParam);
2015 }
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002016 }
Douglas Gregor895162d2010-04-30 18:55:50 +00002017 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002018 } else {
2019 // The function type itself was not dependent and therefore no
2020 // substitution occurred. However, we still need to instantiate
2021 // the function parameters themselves.
Abramo Bagnara140a2bd2010-12-13 22:27:55 +00002022 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00002023 if (FunctionProtoTypeLoc *OldProtoLoc
2024 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2025 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
2026 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
2027 if (!Parm)
2028 return 0;
2029 Params.push_back(Parm);
2030 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00002031 }
2032 }
John McCall21ef0fa2010-03-11 09:03:00 +00002033 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00002034}
2035
Mike Stump1eb44332009-09-09 15:08:12 +00002036/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00002037/// declaration (New) from the corresponding fields of its template (Tmpl).
2038///
2039/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002040bool
2041TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00002042 FunctionDecl *Tmpl) {
2043 if (Tmpl->isDeleted())
2044 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Douglas Gregorcca9e962009-07-01 22:01:06 +00002046 // If we are performing substituting explicitly-specified template arguments
2047 // or deduced template arguments into a function template and we reach this
2048 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002049 // to keeping the new function template specialization. We therefore
2050 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002051 // into a template instantiation for this specific function template
2052 // specialization, which is not a SFINAE context, so that we diagnose any
2053 // further errors in the declaration itself.
2054 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2055 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2056 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2057 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002058 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002059 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002060 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002061 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002062 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002063 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2064 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002065 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002066 }
2067 }
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002069 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2070 assert(Proto && "Function template without prototype?");
2071
2072 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2073 Proto->getNoReturnAttr()) {
2074 // The function has an exception specification or a "noreturn"
2075 // attribute. Substitute into each of the exception types.
2076 llvm::SmallVector<QualType, 4> Exceptions;
2077 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2078 // FIXME: Poor location information!
Douglas Gregorb99268b2010-12-21 00:52:54 +00002079 if (const PackExpansionType *PackExpansion
2080 = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2081 // We have a pack expansion. Instantiate it.
2082 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2083 SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2084 Unexpanded);
2085 assert(!Unexpanded.empty() &&
2086 "Pack expansion without parameter packs?");
2087
2088 bool Expand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002089 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002090 llvm::Optional<unsigned> NumExpansions
2091 = PackExpansion->getNumExpansions();
Douglas Gregorb99268b2010-12-21 00:52:54 +00002092 if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2093 SourceRange(),
2094 Unexpanded.data(),
2095 Unexpanded.size(),
2096 TemplateArgs,
Douglas Gregord3731192011-01-10 07:32:04 +00002097 Expand,
2098 RetainExpansion,
2099 NumExpansions))
Douglas Gregorb99268b2010-12-21 00:52:54 +00002100 break;
2101
2102 if (!Expand) {
2103 // We can't expand this pack expansion into separate arguments yet;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002104 // just substitute into the pattern and create a new pack expansion
2105 // type.
Douglas Gregorb99268b2010-12-21 00:52:54 +00002106 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2107 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2108 TemplateArgs,
2109 New->getLocation(), New->getDeclName());
2110 if (T.isNull())
2111 break;
2112
Douglas Gregorcded4f62011-01-14 17:04:44 +00002113 T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
Douglas Gregorb99268b2010-12-21 00:52:54 +00002114 Exceptions.push_back(T);
2115 continue;
2116 }
2117
2118 // Substitute into the pack expansion pattern for each template
2119 bool Invalid = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002120 for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
Douglas Gregorb99268b2010-12-21 00:52:54 +00002121 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2122
2123 QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2124 TemplateArgs,
2125 New->getLocation(), New->getDeclName());
2126 if (T.isNull()) {
2127 Invalid = true;
2128 break;
2129 }
2130
2131 Exceptions.push_back(T);
2132 }
2133
2134 if (Invalid)
2135 break;
2136
2137 continue;
2138 }
2139
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002140 QualType T
2141 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2142 New->getLocation(), New->getDeclName());
2143 if (T.isNull() ||
2144 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2145 continue;
2146
2147 Exceptions.push_back(T);
2148 }
2149
2150 // Rebuild the function type
2151
John McCalle23cf432010-12-14 08:05:40 +00002152 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2153 EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2154 EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2155 EPI.NumExceptions = Exceptions.size();
2156 EPI.Exceptions = Exceptions.data();
2157 EPI.ExtInfo = Proto->getExtInfo();
2158
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002159 const FunctionProtoType *NewProto
2160 = New->getType()->getAs<FunctionProtoType>();
2161 assert(NewProto && "Template instantiation without function prototype?");
2162 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2163 NewProto->arg_type_begin(),
2164 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +00002165 EPI));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002166 }
2167
John McCall1d8d1cc2010-08-01 02:01:53 +00002168 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002169
Douglas Gregore53060f2009-06-25 22:08:12 +00002170 return false;
2171}
2172
Douglas Gregor5545e162009-03-24 00:38:23 +00002173/// \brief Initializes common fields of an instantiated method
2174/// declaration (New) from the corresponding fields of its template
2175/// (Tmpl).
2176///
2177/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002178bool
2179TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002180 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002181 if (InitFunctionInstantiation(New, Tmpl))
2182 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002183
Douglas Gregor5545e162009-03-24 00:38:23 +00002184 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002185 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002186 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002187
2188 // FIXME: attributes
2189 // FIXME: New needs a pointer to Tmpl
2190 return false;
2191}
Douglas Gregora58861f2009-05-13 20:28:22 +00002192
2193/// \brief Instantiate the definition of the given function from its
2194/// template.
2195///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002196/// \param PointOfInstantiation the point at which the instantiation was
2197/// required. Note that this is not precisely a "point of instantiation"
2198/// for the function, but it's close.
2199///
Douglas Gregora58861f2009-05-13 20:28:22 +00002200/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002201/// function template specialization or member function of a class template
2202/// specialization.
2203///
2204/// \param Recursive if true, recursively instantiates any functions that
2205/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002206///
2207/// \param DefinitionRequired if true, then we are performing an explicit
2208/// instantiation where the body of the function is required. Complain if
2209/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002210void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002211 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002212 bool Recursive,
2213 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002214 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002215 return;
2216
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002217 // Never instantiate an explicit specialization.
2218 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2219 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002220
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002221 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002222 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002223 Stmt *Pattern = 0;
2224 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002225 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002226
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002227 if (!Pattern) {
2228 if (DefinitionRequired) {
2229 if (Function->getPrimaryTemplate())
2230 Diag(PointOfInstantiation,
2231 diag::err_explicit_instantiation_undefined_func_template)
2232 << Function->getPrimaryTemplate();
2233 else
2234 Diag(PointOfInstantiation,
2235 diag::err_explicit_instantiation_undefined_member)
2236 << 1 << Function->getDeclName() << Function->getDeclContext();
2237
2238 if (PatternDecl)
2239 Diag(PatternDecl->getLocation(),
2240 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002241 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002242 } else if (Function->getTemplateSpecializationKind()
2243 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002244 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002245 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002246 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002247
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002248 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002249 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002250
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002251 // C++0x [temp.explicit]p9:
2252 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002253 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002254 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002255 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002256 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002257 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002258 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002260 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2261 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002262 return;
2263
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002264 // If we're performing recursive template instantiation, create our own
2265 // queue of pending implicit instantiations that we will instantiate later,
2266 // while we're still within our own instantiation context.
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002267 llvm::SmallVector<VTableUse, 16> SavedVTableUses;
Chandler Carruth62c78d52010-08-25 08:44:16 +00002268 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002269 if (Recursive) {
2270 VTableUses.swap(SavedVTableUses);
Chandler Carruth62c78d52010-08-25 08:44:16 +00002271 PendingInstantiations.swap(SavedPendingInstantiations);
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002272 }
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Douglas Gregor9679caf2010-05-12 17:27:19 +00002274 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002275 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002276 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002277
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002278 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002279 // recorded, unless we're actually a member function within a local
2280 // class, in which case we need to merge our results with the parent
2281 // scope (of the enclosing function).
2282 bool MergeWithParentScope = false;
2283 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2284 MergeWithParentScope = Rec->isLocalClass();
2285
2286 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002288 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002289 // instantiation scope, and set the parameter names to those used
2290 // in the template.
Douglas Gregor12c9c002011-01-07 16:43:16 +00002291 unsigned FParamIdx = 0;
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002292 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2293 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
Douglas Gregor12c9c002011-01-07 16:43:16 +00002294 if (!PatternParam->isParameterPack()) {
2295 // Simple case: not a parameter pack.
2296 assert(FParamIdx < Function->getNumParams());
2297 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2298 FunctionParam->setDeclName(PatternParam->getDeclName());
2299 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2300 ++FParamIdx;
2301 continue;
2302 }
2303
2304 // Expand the parameter pack.
2305 Scope.MakeInstantiatedLocalArgPack(PatternParam);
2306 for (unsigned NumFParams = Function->getNumParams();
2307 FParamIdx < NumFParams;
2308 ++FParamIdx) {
2309 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2310 FunctionParam->setDeclName(PatternParam->getDeclName());
2311 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2312 }
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002313 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002314
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002315 // Enter the scope of this instantiation. We don't use
2316 // PushDeclContext because we don't have a scope.
2317 DeclContext *PreviousContext = CurContext;
2318 CurContext = Function;
2319
Mike Stump1eb44332009-09-09 15:08:12 +00002320 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002321 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002322
2323 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002324 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002325 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2326 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2327 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002328 }
2329
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002330 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002331 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002332
Douglas Gregor52604ab2009-09-11 21:19:12 +00002333 if (Body.isInvalid())
2334 Function->setInvalidDecl();
2335
John McCall9ae2f072010-08-23 23:25:46 +00002336 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002337 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002338
John McCall0c01d182010-03-24 05:22:00 +00002339 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2340
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002341 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002342
2343 DeclGroupRef DG(Function);
2344 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002345
Douglas Gregor60406be2010-01-16 22:29:39 +00002346 // This class may have local implicit instantiations that need to be
2347 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002348 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002349 Scope.Exit();
2350
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002351 if (Recursive) {
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002352 // Define any pending vtables.
2353 DefineUsedVTables();
2354
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002355 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002356 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002357 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Nick Lewycky2a5f99e2010-11-25 00:35:20 +00002359 // Restore the set of pending vtables.
2360 VTableUses.swap(SavedVTableUses);
2361
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002362 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002363 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002364 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002365}
2366
2367/// \brief Instantiate the definition of the given variable from its
2368/// template.
2369///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002370/// \param PointOfInstantiation the point at which the instantiation was
2371/// required. Note that this is not precisely a "point of instantiation"
2372/// for the function, but it's close.
2373///
2374/// \param Var the already-instantiated declaration of a static member
2375/// variable of a class template specialization.
2376///
2377/// \param Recursive if true, recursively instantiates any functions that
2378/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002379///
2380/// \param DefinitionRequired if true, then we are performing an explicit
2381/// instantiation where an out-of-line definition of the member variable
2382/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002383void Sema::InstantiateStaticDataMemberDefinition(
2384 SourceLocation PointOfInstantiation,
2385 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002386 bool Recursive,
2387 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002388 if (Var->isInvalidDecl())
2389 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Douglas Gregor7caa6822009-07-24 20:34:43 +00002391 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002392 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002393 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002394 assert(Def->isStaticDataMember() && "Not a static data member?");
2395 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Douglas Gregor0d035142009-10-27 18:42:08 +00002397 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002398 // We did not find an out-of-line definition of this static data member,
2399 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002400 // instantiate this definition (or provide a specialization for it) in
2401 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002402 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002403 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002404 Diag(PointOfInstantiation,
2405 diag::err_explicit_instantiation_undefined_member)
2406 << 2 << Var->getDeclName() << Var->getDeclContext();
2407 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002408 } else if (Var->getTemplateSpecializationKind()
2409 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002410 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002411 std::make_pair(Var, PointOfInstantiation));
2412 }
2413
Douglas Gregor7caa6822009-07-24 20:34:43 +00002414 return;
2415 }
2416
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002417 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002418 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002419 return;
2420
2421 // C++0x [temp.explicit]p9:
2422 // Except for inline functions, other explicit instantiation declarations
2423 // have the effect of suppressing the implicit instantiation of the entity
2424 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002425 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002426 == TSK_ExplicitInstantiationDeclaration)
2427 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Douglas Gregor7caa6822009-07-24 20:34:43 +00002429 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2430 if (Inst)
2431 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Douglas Gregor7caa6822009-07-24 20:34:43 +00002433 // If we're performing recursive template instantiation, create our own
2434 // queue of pending implicit instantiations that we will instantiate later,
2435 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002436 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002437 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002438 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Douglas Gregor7caa6822009-07-24 20:34:43 +00002440 // Enter the scope of this instantiation. We don't use
2441 // PushDeclContext because we don't have a scope.
2442 DeclContext *PreviousContext = CurContext;
2443 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002445 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002446 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Nico Weber6bb4dcb2010-11-28 22:53:37 +00002447 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002448 CurContext = PreviousContext;
2449
2450 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002451 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2452 assert(MSInfo && "Missing member specialization information?");
2453 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2454 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002455 DeclGroupRef DG(Var);
2456 Consumer.HandleTopLevelDecl(DG);
2457 }
Mike Stump1eb44332009-09-09 15:08:12 +00002458
Douglas Gregor7caa6822009-07-24 20:34:43 +00002459 if (Recursive) {
2460 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002461 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002462 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002463
Douglas Gregor7caa6822009-07-24 20:34:43 +00002464 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002465 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002466 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002467}
Douglas Gregor815215d2009-05-27 05:35:12 +00002468
Anders Carlsson09025312009-08-29 05:16:22 +00002469void
2470Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2471 const CXXConstructorDecl *Tmpl,
2472 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Anders Carlsson09025312009-08-29 05:16:22 +00002474 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002475 bool AnyErrors = false;
2476
Anders Carlsson09025312009-08-29 05:16:22 +00002477 // Instantiate all the initializers.
2478 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002479 InitsEnd = Tmpl->init_end();
2480 Inits != InitsEnd; ++Inits) {
Sean Huntcbb67482011-01-08 20:30:50 +00002481 CXXCtorInitializer *Init = *Inits;
Anders Carlsson09025312009-08-29 05:16:22 +00002482
Chandler Carruth030ef472010-09-03 21:54:20 +00002483 // Only instantiate written initializers, let Sema re-construct implicit
2484 // ones.
2485 if (!Init->isWritten())
2486 continue;
2487
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002488 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002489 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002491 SourceLocation EllipsisLoc;
2492
2493 if (Init->isPackExpansion()) {
2494 // This is a pack expansion. We should expand it now.
2495 TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
2496 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2497 collectUnexpandedParameterPacks(BaseTL, Unexpanded);
2498 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00002499 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002500 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002501 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
2502 BaseTL.getSourceRange(),
2503 Unexpanded.data(),
2504 Unexpanded.size(),
2505 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00002506 RetainExpansion,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002507 NumExpansions)) {
2508 AnyErrors = true;
2509 New->setInvalidDecl();
2510 continue;
2511 }
2512 assert(ShouldExpand && "Partial instantiation of base initializer?");
2513
2514 // Loop over all of the arguments in the argument pack(s),
Douglas Gregorcded4f62011-01-14 17:04:44 +00002515 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002516 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
2517
2518 // Instantiate the initializer.
2519 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2520 LParenLoc, NewArgs, RParenLoc)) {
2521 AnyErrors = true;
2522 break;
2523 }
2524
2525 // Instantiate the base type.
2526 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2527 TemplateArgs,
2528 Init->getSourceLocation(),
2529 New->getDeclName());
2530 if (!BaseTInfo) {
2531 AnyErrors = true;
2532 break;
2533 }
2534
2535 // Build the initializer.
2536 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
2537 BaseTInfo,
2538 (Expr **)NewArgs.data(),
2539 NewArgs.size(),
2540 Init->getLParenLoc(),
2541 Init->getRParenLoc(),
2542 New->getParent(),
2543 SourceLocation());
2544 if (NewInit.isInvalid()) {
2545 AnyErrors = true;
2546 break;
2547 }
2548
2549 NewInits.push_back(NewInit.get());
2550 NewArgs.clear();
2551 }
2552
2553 continue;
2554 }
2555
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002556 // Instantiate the initializer.
2557 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002558 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002559 AnyErrors = true;
2560 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002561 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002562
Anders Carlsson09025312009-08-29 05:16:22 +00002563 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002564 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002565 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002566 TemplateArgs,
2567 Init->getSourceLocation(),
2568 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002569 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002570 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002571 New->setInvalidDecl();
2572 continue;
2573 }
2574
John McCalla93c9342009-12-07 02:54:59 +00002575 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002576 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002577 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002578 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002579 Init->getRParenLoc(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002580 New->getParent(),
2581 EllipsisLoc);
Anders Carlsson09025312009-08-29 05:16:22 +00002582 } else if (Init->isMemberInitializer()) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00002583 FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
2584 Init->getMemberLocation(),
2585 Init->getMember(),
2586 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002587
2588 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002589 NewArgs.size(),
2590 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002591 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002592 Init->getRParenLoc());
Francois Pichet00eb3f92010-12-04 09:14:42 +00002593 } else if (Init->isIndirectMemberInitializer()) {
2594 IndirectFieldDecl *IndirectMember =
2595 cast<IndirectFieldDecl>(FindInstantiatedDecl(
2596 Init->getMemberLocation(),
2597 Init->getIndirectMember(), TemplateArgs));
2598
2599 NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
2600 NewArgs.size(),
2601 Init->getSourceLocation(),
2602 Init->getLParenLoc(),
2603 Init->getRParenLoc());
Anders Carlsson09025312009-08-29 05:16:22 +00002604 }
2605
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002606 if (NewInit.isInvalid()) {
2607 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002608 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002609 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002610 // FIXME: It would be nice if ASTOwningVector had a release function.
2611 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002612
Anders Carlsson09025312009-08-29 05:16:22 +00002613 NewInits.push_back((MemInitTy *)NewInit.get());
2614 }
2615 }
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Anders Carlsson09025312009-08-29 05:16:22 +00002617 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002618 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002619 /*FIXME: ColonLoc */
2620 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002621 NewInits.data(), NewInits.size(),
2622 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002623}
2624
John McCall52a575a2009-08-29 08:11:13 +00002625// TODO: this could be templated if the various decl types used the
2626// same method name.
2627static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2628 ClassTemplateDecl *Instance) {
2629 Pattern = Pattern->getCanonicalDecl();
2630
2631 do {
2632 Instance = Instance->getCanonicalDecl();
2633 if (Pattern == Instance) return true;
2634 Instance = Instance->getInstantiatedFromMemberTemplate();
2635 } while (Instance);
2636
2637 return false;
2638}
2639
Douglas Gregor0d696532009-09-28 06:34:35 +00002640static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2641 FunctionTemplateDecl *Instance) {
2642 Pattern = Pattern->getCanonicalDecl();
2643
2644 do {
2645 Instance = Instance->getCanonicalDecl();
2646 if (Pattern == Instance) return true;
2647 Instance = Instance->getInstantiatedFromMemberTemplate();
2648 } while (Instance);
2649
2650 return false;
2651}
2652
Douglas Gregored9c0f92009-10-29 00:04:11 +00002653static bool
2654isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2655 ClassTemplatePartialSpecializationDecl *Instance) {
2656 Pattern
2657 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2658 do {
2659 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2660 Instance->getCanonicalDecl());
2661 if (Pattern == Instance)
2662 return true;
2663 Instance = Instance->getInstantiatedFromMember();
2664 } while (Instance);
2665
2666 return false;
2667}
2668
John McCall52a575a2009-08-29 08:11:13 +00002669static bool isInstantiationOf(CXXRecordDecl *Pattern,
2670 CXXRecordDecl *Instance) {
2671 Pattern = Pattern->getCanonicalDecl();
2672
2673 do {
2674 Instance = Instance->getCanonicalDecl();
2675 if (Pattern == Instance) return true;
2676 Instance = Instance->getInstantiatedFromMemberClass();
2677 } while (Instance);
2678
2679 return false;
2680}
2681
2682static bool isInstantiationOf(FunctionDecl *Pattern,
2683 FunctionDecl *Instance) {
2684 Pattern = Pattern->getCanonicalDecl();
2685
2686 do {
2687 Instance = Instance->getCanonicalDecl();
2688 if (Pattern == Instance) return true;
2689 Instance = Instance->getInstantiatedFromMemberFunction();
2690 } while (Instance);
2691
2692 return false;
2693}
2694
2695static bool isInstantiationOf(EnumDecl *Pattern,
2696 EnumDecl *Instance) {
2697 Pattern = Pattern->getCanonicalDecl();
2698
2699 do {
2700 Instance = Instance->getCanonicalDecl();
2701 if (Pattern == Instance) return true;
2702 Instance = Instance->getInstantiatedFromMemberEnum();
2703 } while (Instance);
2704
2705 return false;
2706}
2707
John McCalled976492009-12-04 22:46:56 +00002708static bool isInstantiationOf(UsingShadowDecl *Pattern,
2709 UsingShadowDecl *Instance,
2710 ASTContext &C) {
2711 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2712}
2713
2714static bool isInstantiationOf(UsingDecl *Pattern,
2715 UsingDecl *Instance,
2716 ASTContext &C) {
2717 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2718}
2719
John McCall7ba107a2009-11-18 02:36:19 +00002720static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2721 UsingDecl *Instance,
2722 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002723 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002724}
2725
2726static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002727 UsingDecl *Instance,
2728 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002729 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002730}
2731
John McCall52a575a2009-08-29 08:11:13 +00002732static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2733 VarDecl *Instance) {
2734 assert(Instance->isStaticDataMember());
2735
2736 Pattern = Pattern->getCanonicalDecl();
2737
2738 do {
2739 Instance = Instance->getCanonicalDecl();
2740 if (Pattern == Instance) return true;
2741 Instance = Instance->getInstantiatedFromStaticDataMember();
2742 } while (Instance);
2743
2744 return false;
2745}
2746
John McCalled976492009-12-04 22:46:56 +00002747// Other is the prospective instantiation
2748// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002749static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002750 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002751 if (UnresolvedUsingTypenameDecl *UUD
2752 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2753 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2754 return isInstantiationOf(UUD, UD, Ctx);
2755 }
2756 }
2757
2758 if (UnresolvedUsingValueDecl *UUD
2759 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002760 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2761 return isInstantiationOf(UUD, UD, Ctx);
2762 }
2763 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002764
Anders Carlsson0d8df782009-08-29 19:37:28 +00002765 return false;
2766 }
Mike Stump1eb44332009-09-09 15:08:12 +00002767
John McCall52a575a2009-08-29 08:11:13 +00002768 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2769 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002770
John McCall52a575a2009-08-29 08:11:13 +00002771 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2772 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002773
John McCall52a575a2009-08-29 08:11:13 +00002774 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2775 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002776
Douglas Gregor7caa6822009-07-24 20:34:43 +00002777 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002778 if (Var->isStaticDataMember())
2779 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2780
2781 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2782 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002783
Douglas Gregor0d696532009-09-28 06:34:35 +00002784 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2785 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2786
Douglas Gregored9c0f92009-10-29 00:04:11 +00002787 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2788 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2789 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2790 PartialSpec);
2791
Anders Carlssond8b285f2009-09-01 04:26:58 +00002792 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2793 if (!Field->getDeclName()) {
2794 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002795 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002796 cast<FieldDecl>(D);
2797 }
2798 }
Mike Stump1eb44332009-09-09 15:08:12 +00002799
John McCalled976492009-12-04 22:46:56 +00002800 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2801 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2802
2803 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2804 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2805
Douglas Gregor815215d2009-05-27 05:35:12 +00002806 return D->getDeclName() && isa<NamedDecl>(Other) &&
2807 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2808}
2809
2810template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002811static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002812 NamedDecl *D,
2813 ForwardIterator first,
2814 ForwardIterator last) {
2815 for (; first != last; ++first)
2816 if (isInstantiationOf(Ctx, D, *first))
2817 return cast<NamedDecl>(*first);
2818
2819 return 0;
2820}
2821
John McCall02cace72009-08-28 07:59:38 +00002822/// \brief Finds the instantiation of the given declaration context
2823/// within the current instantiation.
2824///
2825/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002826DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002827 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002828 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002829 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002830 return cast_or_null<DeclContext>(ID);
2831 } else return DC;
2832}
2833
Douglas Gregored961e72009-05-27 17:54:46 +00002834/// \brief Find the instantiation of the given declaration within the
2835/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002836///
2837/// This routine is intended to be used when \p D is a declaration
2838/// referenced from within a template, that needs to mapped into the
2839/// corresponding declaration within an instantiation. For example,
2840/// given:
2841///
2842/// \code
2843/// template<typename T>
2844/// struct X {
2845/// enum Kind {
2846/// KnownValue = sizeof(T)
2847/// };
2848///
2849/// bool getKind() const { return KnownValue; }
2850/// };
2851///
2852/// template struct X<int>;
2853/// \endcode
2854///
2855/// In the instantiation of X<int>::getKind(), we need to map the
2856/// EnumConstantDecl for KnownValue (which refers to
2857/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002858/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2859/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002860NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002861 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002862 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002863 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002864 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002865 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002866 // D is a local of some kind. Look into the map of local
2867 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002868 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002869 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002870
Douglas Gregore95b4092009-09-16 18:34:49 +00002871 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2872 if (!Record->isDependentContext())
2873 return D;
2874
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002875 // If the RecordDecl is actually the injected-class-name or a
2876 // "templated" declaration for a class template, class template
2877 // partial specialization, or a member class of a class template,
2878 // substitute into the injected-class-name of the class template
2879 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002880 QualType T;
2881 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2882
2883 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002884 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002885 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2886 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002887 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002888
2889 // If we call SubstType with an InjectedClassNameType here we
2890 // can end up in an infinite loop.
2891 T = Context.getTypeDeclType(Record);
2892 assert(isa<InjectedClassNameType>(T) &&
2893 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002894 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002895 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002896
2897 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002898 // Substitute into the injected-class-name to get the type
2899 // corresponding to the instantiation we want, which may also be
2900 // the current instantiation (if we're in a template
2901 // definition). This substitution should never fail, since we
2902 // know we can instantiate the injected-class-name or we
2903 // wouldn't have gotten to the injected-class-name!
2904
2905 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2906 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002907 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2908 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2909
2910 if (!T->isDependentType()) {
2911 assert(T->isRecordType() && "Instantiation must produce a record type");
2912 return T->getAs<RecordType>()->getDecl();
2913 }
2914
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002915 // We are performing "partial" template instantiation to create
2916 // the member declarations for the members of a class template
2917 // specialization. Therefore, D is actually referring to something
2918 // in the current instantiation. Look through the current
2919 // context, which contains actual instantiations, to find the
2920 // instantiation of the "current instantiation" that D refers
2921 // to.
2922 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002923 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002924 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002925 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002926 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002927 if (isInstantiationOf(ClassTemplate,
2928 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002929 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002930
2931 if (!DC->isDependentContext())
2932 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002933 }
2934
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002935 // We're performing "instantiation" of a member of the current
2936 // instantiation while we are type-checking the
2937 // definition. Compute the declaration context and return that.
2938 assert(!SawNonDependentContext &&
2939 "No dependent context while instantiating record");
2940 DeclContext *DC = computeDeclContext(T);
2941 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002942 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002943 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002944 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002945
Douglas Gregore95b4092009-09-16 18:34:49 +00002946 // Fall through to deal with other dependent record types (e.g.,
2947 // anonymous unions in class templates).
2948 }
John McCall52a575a2009-08-29 08:11:13 +00002949
Douglas Gregore95b4092009-09-16 18:34:49 +00002950 if (!ParentDC->isDependentContext())
2951 return D;
2952
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002953 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002954 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002955 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Douglas Gregor815215d2009-05-27 05:35:12 +00002957 if (ParentDC != D->getDeclContext()) {
2958 // We performed some kind of instantiation in the parent context,
2959 // so now we need to look into the instantiated parent context to
2960 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002961
John McCall3cb0ebd2010-03-10 03:28:59 +00002962 // If our context used to be dependent, we may need to instantiate
2963 // it before performing lookup into that context.
2964 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002965 if (!Spec->isDependentContext()) {
2966 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002967 const RecordType *Tag = T->getAs<RecordType>();
2968 assert(Tag && "type of non-dependent record is not a RecordType");
2969 if (!Tag->isBeingDefined() &&
2970 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2971 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002972
2973 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002974 }
2975 }
2976
Douglas Gregor815215d2009-05-27 05:35:12 +00002977 NamedDecl *Result = 0;
2978 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002979 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002980 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2981 } else {
2982 // Since we don't have a name for the entity we're looking for,
2983 // our only option is to walk through all of the declarations to
2984 // find that name. This will occur in a few cases:
2985 //
2986 // - anonymous struct/union within a template
2987 // - unnamed class/struct/union/enum within a template
2988 //
2989 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002990 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002991 ParentDC->decls_begin(),
2992 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002993 }
Mike Stump1eb44332009-09-09 15:08:12 +00002994
John McCall9f54ad42009-12-10 09:41:52 +00002995 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002996 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2997 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002998 && "Unable to find instantiation of declaration!");
2999
Douglas Gregor815215d2009-05-27 05:35:12 +00003000 D = Result;
3001 }
3002
Douglas Gregor815215d2009-05-27 05:35:12 +00003003 return D;
3004}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003005
Mike Stump1eb44332009-09-09 15:08:12 +00003006/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003007/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003008void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003009 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00003010 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00003011 PendingImplicitInstantiation Inst;
3012
3013 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003014 Inst = PendingInstantiations.front();
3015 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00003016 } else {
3017 Inst = PendingLocalImplicitInstantiations.front();
3018 PendingLocalImplicitInstantiations.pop_front();
3019 }
Mike Stump1eb44332009-09-09 15:08:12 +00003020
Douglas Gregor7caa6822009-07-24 20:34:43 +00003021 // Instantiate function definitions
3022 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00003023 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3024 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00003025 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
3026 TSK_ExplicitInstantiationDefinition;
3027 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
3028 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00003029 continue;
3030 }
Mike Stump1eb44332009-09-09 15:08:12 +00003031
Douglas Gregor7caa6822009-07-24 20:34:43 +00003032 // Instantiate static data member definitions.
3033 VarDecl *Var = cast<VarDecl>(Inst.first);
3034 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00003035
Chandler Carruth291b4412010-02-13 10:17:50 +00003036 // Don't try to instantiate declarations if the most recent redeclaration
3037 // is invalid.
3038 if (Var->getMostRecentDeclaration()->isInvalidDecl())
3039 continue;
3040
3041 // Check if the most recent declaration has changed the specialization kind
3042 // and removed the need for implicit instantiation.
3043 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3044 case TSK_Undeclared:
3045 assert(false && "Cannot instantitiate an undeclared specialization.");
3046 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00003047 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00003048 continue; // No longer need to instantiate this type.
3049 case TSK_ExplicitInstantiationDefinition:
3050 // We only need an instantiation if the pending instantiation *is* the
3051 // explicit instantiation.
3052 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00003053 case TSK_ImplicitInstantiation:
3054 break;
3055 }
3056
John McCallf312b1e2010-08-26 23:41:50 +00003057 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3058 "instantiating static data member "
3059 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00003060
Chandler Carruth58e390e2010-08-25 08:27:02 +00003061 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
3062 TSK_ExplicitInstantiationDefinition;
3063 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
3064 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00003065 }
3066}
John McCall0c01d182010-03-24 05:22:00 +00003067
3068void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
3069 const MultiLevelTemplateArgumentList &TemplateArgs) {
3070 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
3071 E = Pattern->ddiag_end(); I != E; ++I) {
3072 DependentDiagnostic *DD = *I;
3073
3074 switch (DD->getKind()) {
3075 case DependentDiagnostic::Access:
3076 HandleDependentAccessCheck(*DD, TemplateArgs);
3077 break;
3078 }
3079 }
3080}