blob: 5b5e1babf532d55bfd354c71b39f6fc424b01867 [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(),
Sean Huntcf807c42010-08-18 23:23:40 +000075 TemplateArgs);
76 if (!Result.isInvalid())
77 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
78 }
79 else {
80 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
81 TemplateArgs,
82 Aligned->getLocation(),
83 DeclarationName());
84 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 Gregor6eef5192009-12-14 19:27:10 +0000169/// \brief Instantiate the arguments provided as part of initialization.
170///
171/// \returns true if an error occurred, false otherwise.
172static bool InstantiateInitializationArguments(Sema &SemaRef,
173 Expr **Args, unsigned NumArgs,
174 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallca0408f2010-08-23 06:44:23 +0000175 ASTOwningVector<Expr*> &InitArgs) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000176 for (unsigned I = 0; I != NumArgs; ++I) {
177 // When we hit the first defaulted argument, break out of the loop:
178 // we don't pass those default arguments on.
179 if (Args[I]->isDefaultArgument())
180 break;
181
John McCall60d7b3a2010-08-24 06:29:42 +0000182 ExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000183 if (Arg.isInvalid())
184 return true;
185
Douglas Gregor6eef5192009-12-14 19:27:10 +0000186 InitArgs.push_back(Arg.release());
Douglas Gregor6eef5192009-12-14 19:27:10 +0000187 }
188
189 return false;
190}
191
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000192/// \brief Instantiate an initializer, breaking it into separate
193/// initialization arguments.
194///
195/// \param S The semantic analysis object.
196///
197/// \param Init The initializer to instantiate.
198///
199/// \param TemplateArgs Template arguments to be substituted into the
200/// initializer.
201///
202/// \param NewArgs Will be filled in with the instantiation arguments.
203///
204/// \returns true if an error occurred, false otherwise
205static bool InstantiateInitializer(Sema &S, Expr *Init,
206 const MultiLevelTemplateArgumentList &TemplateArgs,
207 SourceLocation &LParenLoc,
John McCallca0408f2010-08-23 06:44:23 +0000208 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000209 SourceLocation &RParenLoc) {
210 NewArgs.clear();
211 LParenLoc = SourceLocation();
212 RParenLoc = SourceLocation();
213
214 if (!Init)
215 return false;
216
217 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
218 Init = ExprTemp->getSubExpr();
219
220 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
221 Init = Binder->getSubExpr();
222
223 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
224 Init = ICE->getSubExprAsWritten();
225
226 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
227 LParenLoc = ParenList->getLParenLoc();
228 RParenLoc = ParenList->getRParenLoc();
229 return InstantiateInitializationArguments(S, ParenList->getExprs(),
230 ParenList->getNumExprs(),
Douglas Gregora1a04782010-09-09 16:33:13 +0000231 TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000232 }
233
234 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000235 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
236 if (InstantiateInitializationArguments(S,
237 Construct->getArgs(),
238 Construct->getNumArgs(),
Douglas Gregora1a04782010-09-09 16:33:13 +0000239 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000240 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000241
Douglas Gregor28329e52010-03-24 21:22:47 +0000242 // FIXME: Fake locations!
243 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000244 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000245 return false;
246 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000247 }
248
John McCall60d7b3a2010-08-24 06:29:42 +0000249 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000250 if (Result.isInvalid())
251 return true;
252
253 NewArgs.push_back(Result.takeAs<Expr>());
254 return false;
255}
256
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000257Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000258 // If this is the variable for an anonymous struct or union,
259 // instantiate the anonymous struct/union type first.
260 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
261 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
262 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
263 return 0;
264
John McCallce3ff2b2009-08-25 22:02:44 +0000265 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000266 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000267 TemplateArgs,
268 D->getTypeSpecStartLoc(),
269 D->getDeclName());
270 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000271 return 0;
272
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000273 if (DI->getType()->isFunctionType()) {
274 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
275 << D->isStaticDataMember() << DI->getType();
276 return 0;
277 }
278
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000279 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000280 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
281 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000282 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000283 D->getStorageClass(),
284 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000285 Var->setThreadSpecified(D->isThreadSpecified());
286 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000287
John McCallb6217662010-03-15 10:12:16 +0000288 // Substitute the nested name specifier, if any.
289 if (SubstQualifier(D, Var))
290 return 0;
291
Mike Stump1eb44332009-09-09 15:08:12 +0000292 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000293 // out-of-line, the instantiation will have the same lexical
294 // context (which will be a namespace scope) as the template.
295 if (D->isOutOfLine())
296 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000297
John McCall46460a62010-01-20 21:53:11 +0000298 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000299
300 if (!D->isStaticDataMember())
301 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000302
Mike Stump390b4cc2009-05-16 07:39:55 +0000303 // FIXME: In theory, we could have a previous declaration for variables that
304 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000305 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000306 // FIXME: having to fake up a LookupResult is dumb.
307 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000308 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000309 if (D->isStaticDataMember())
310 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000311 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Douglas Gregor7caa6822009-07-24 20:34:43 +0000313 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000314 if (!D->isStaticDataMember())
315 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000316 Owner->makeDeclVisibleInContext(Var);
317 } else {
318 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000319 if (Owner->isFunctionOrMethod())
320 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000321 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000322 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000323
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000324 // Link instantiations of static data members back to the template from
325 // which they were instantiated.
326 if (Var->isStaticDataMember())
327 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000328 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000329
Douglas Gregor60c93c92010-02-09 07:26:29 +0000330 if (Var->getAnyInitializer()) {
331 // We already have an initializer in the class.
332 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000333 if (Var->isStaticDataMember() && !D->isOutOfLine())
334 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
335 else
336 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
337
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000338 // Instantiate the initializer.
339 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000340 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000341 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000342 InitArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000343 // Attach the initializer to the declaration.
344 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000345 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000346 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000347 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000348 move_arg(InitArgs),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000349 RParenLoc);
350 } else if (InitArgs.size() == 1) {
John McCall9ae2f072010-08-23 23:25:46 +0000351 Expr *Init = InitArgs.take()[0];
352 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000353 } else {
354 assert(InitArgs.size() == 0);
John McCalld226f652010-08-21 09:40:31 +0000355 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000356 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000357 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000358 // FIXME: Not too happy about invalidating the declaration
359 // because of a bogus initializer.
360 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000361 }
362
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000363 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000364 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000365 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000366
Douglas Gregor5764f612010-05-08 23:05:03 +0000367 // Diagnose unused local variables.
368 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
369 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000370
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000371 return Var;
372}
373
Abramo Bagnara6206d532010-06-05 05:09:32 +0000374Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
375 AccessSpecDecl* AD
376 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
377 D->getAccessSpecifierLoc(), D->getColonLoc());
378 Owner->addHiddenDecl(AD);
379 return AD;
380}
381
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000382Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
383 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000384 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000385 if (DI->getType()->isDependentType() ||
386 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000387 DI = SemaRef.SubstType(DI, TemplateArgs,
388 D->getLocation(), D->getDeclName());
389 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000390 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000391 Invalid = true;
392 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000393 // C++ [temp.arg.type]p3:
394 // If a declaration acquires a function type through a type
395 // dependent on a template-parameter and this causes a
396 // declaration that does not use the syntactic form of a
397 // function declarator to have function type, the program is
398 // ill-formed.
399 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000400 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000401 Invalid = true;
402 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000403 } else {
404 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000405 }
406
407 Expr *BitWidth = D->getBitWidth();
408 if (Invalid)
409 BitWidth = 0;
410 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000411 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000412 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000413
John McCall60d7b3a2010-08-24 06:29:42 +0000414 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000415 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000416 if (InstantiatedBitWidth.isInvalid()) {
417 Invalid = true;
418 BitWidth = 0;
419 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000420 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000421 }
422
John McCall07fb6be2009-10-22 23:33:21 +0000423 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
424 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000425 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000426 D->getLocation(),
427 D->isMutable(),
428 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000429 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000430 D->getAccess(),
431 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000432 if (!Field) {
433 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000434 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
John McCall1d8d1cc2010-08-01 02:01:53 +0000437 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000438
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000439 if (Invalid)
440 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000442 if (!Field->getDeclName()) {
443 // Keep track of where this decl came from.
444 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000445 }
446 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
447 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000448 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000449 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000452 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000453 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000454 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000455
456 return Field;
457}
458
John McCall02cace72009-08-28 07:59:38 +0000459Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000460 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000461 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000462 if (TypeSourceInfo *Ty = D->getFriendType()) {
463 TypeSourceInfo *InstTy =
464 SemaRef.SubstType(Ty, TemplateArgs,
465 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000466 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000467 return 0;
John McCall02cace72009-08-28 07:59:38 +0000468
Douglas Gregor06245bf2010-04-07 17:57:12 +0000469 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
470 if (!FD)
471 return 0;
472
473 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000474 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000475 Owner->addDecl(FD);
476 return FD;
477 }
478
479 NamedDecl *ND = D->getFriendDecl();
480 assert(ND && "friend decl must be a decl or a type!");
481
John McCallaf2094e2010-04-08 09:05:18 +0000482 // All of the Visit implementations for the various potential friend
483 // declarations have to be carefully written to work for friend
484 // objects, with the most important detail being that the target
485 // decl should almost certainly not be placed in Owner.
486 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000487 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
John McCall02cace72009-08-28 07:59:38 +0000489 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000490 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
491 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000492 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000493 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000494 Owner->addDecl(FD);
495 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000496}
497
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000498Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
499 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregorac7610d2009-06-22 20:57:11 +0000501 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000502 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
John McCall60d7b3a2010-08-24 06:29:42 +0000504 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000505 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000506 if (InstantiatedAssertExpr.isInvalid())
507 return 0;
508
John McCall60d7b3a2010-08-24 06:29:42 +0000509 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000510 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000511 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000512 InstantiatedAssertExpr.get(),
513 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000514}
515
516Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000517 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000518 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000519 D->getTagKeywordLoc(),
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000520 /*PrevDecl=*/0,
521 D->isScoped(), D->isFixed());
522 if (D->isFixed()) {
523 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
524 // If we have type source information for the underlying type, it means it
525 // has been explicitly set by the user. Perform substitution on it before
526 // moving on.
527 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
528 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
529 TemplateArgs,
530 UnderlyingLoc,
531 DeclarationName()));
532
533 if (!Enum->getIntegerTypeSourceInfo())
534 Enum->setIntegerType(SemaRef.Context.IntTy);
535 }
536 else {
537 assert(!D->getIntegerType()->isDependentType()
538 && "Dependent type without type source info");
539 Enum->setIntegerType(D->getIntegerType());
540 }
541 }
542
John McCall5b629aa2010-10-22 23:36:17 +0000543 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
544
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000545 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000546 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000547 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000548 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000549 Enum->startDefinition();
550
Douglas Gregor96084f12010-03-01 19:00:07 +0000551 if (D->getDeclContext()->isFunctionOrMethod())
552 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
553
John McCalld226f652010-08-21 09:40:31 +0000554 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000555
556 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000557 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
558 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559 EC != ECEnd; ++EC) {
560 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000561 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000562 if (Expr *UninstValue = EC->getInitExpr()) {
563 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000564 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000565 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
John McCallce3ff2b2009-08-25 22:02:44 +0000567 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000568 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000569
570 // Drop the initial value and continue.
571 bool isInvalid = false;
572 if (Value.isInvalid()) {
573 Value = SemaRef.Owned((Expr *)0);
574 isInvalid = true;
575 }
576
Mike Stump1eb44332009-09-09 15:08:12 +0000577 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000578 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
579 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000580 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000581
582 if (isInvalid) {
583 if (EnumConst)
584 EnumConst->setInvalidDecl();
585 Enum->setInvalidDecl();
586 }
587
588 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000589 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
590
John McCall3b85ecf2010-01-23 22:37:59 +0000591 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000592 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000593 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000594 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000595
596 if (D->getDeclContext()->isFunctionOrMethod()) {
597 // If the enumeration is within a function or method, record the enum
598 // constant as a local.
599 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
600 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000601 }
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000604 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000605 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000606 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000607 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000608 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000609 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000610
611 return Enum;
612}
613
Douglas Gregor6477b692009-03-25 15:04:13 +0000614Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
615 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
616 return 0;
617}
618
John McCalle29ba202009-08-20 01:44:21 +0000619Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000620 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
621
Douglas Gregor550d9b22009-10-31 17:21:17 +0000622 // Create a local instantiation scope for this class template, which
623 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000624 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000625 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000626 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000627 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000628 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000629
630 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000631
632 // Instantiate the qualifier. We have to do this first in case
633 // we're a friend declaration, because if we are then we need to put
634 // the new declaration in the appropriate context.
635 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
636 if (Qualifier) {
637 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
638 Pattern->getQualifierRange(),
639 TemplateArgs);
640 if (!Qualifier) return 0;
641 }
642
643 CXXRecordDecl *PrevDecl = 0;
644 ClassTemplateDecl *PrevClassTemplate = 0;
645
Nick Lewycky37574f52010-11-08 23:29:42 +0000646 if (!isFriend && Pattern->getPreviousDeclaration()) {
647 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
648 if (Found.first != Found.second) {
649 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
650 if (PrevClassTemplate)
651 PrevDecl = PrevClassTemplate->getTemplatedDecl();
652 }
653 }
654
John McCall93ba8572010-03-25 06:39:04 +0000655 // If this isn't a friend, then it's a member template, in which
656 // case we just want to build the instantiation in the
657 // specialization. If it is a friend, we want to build it in
658 // the appropriate context.
659 DeclContext *DC = Owner;
660 if (isFriend) {
661 if (Qualifier) {
662 CXXScopeSpec SS;
663 SS.setScopeRep(Qualifier);
664 SS.setRange(Pattern->getQualifierRange());
665 DC = SemaRef.computeDeclContext(SS);
666 if (!DC) return 0;
667 } else {
668 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
669 Pattern->getDeclContext(),
670 TemplateArgs);
671 }
672
673 // Look for a previous declaration of the template in the owning
674 // context.
675 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
676 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
677 SemaRef.LookupQualifiedName(R, DC);
678
679 if (R.isSingleResult()) {
680 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
681 if (PrevClassTemplate)
682 PrevDecl = PrevClassTemplate->getTemplatedDecl();
683 }
684
685 if (!PrevClassTemplate && Qualifier) {
686 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000687 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
688 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000689 return 0;
690 }
691
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000692 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000693 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000694 bool Complain = true;
695
696 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
697 // template for struct std::tr1::__detail::_Map_base, where the
698 // template parameters of the friend declaration don't match the
699 // template parameters of the original declaration. In this one
700 // case, we don't complain about the ill-formed friend
701 // declaration.
702 if (isFriend && Pattern->getIdentifier() &&
703 Pattern->getIdentifier()->isStr("_Map_base") &&
704 DC->isNamespace() &&
705 cast<NamespaceDecl>(DC)->getIdentifier() &&
706 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
707 DeclContext *DCParent = DC->getParent();
708 if (DCParent->isNamespace() &&
709 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
710 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
711 DeclContext *DCParent2 = DCParent->getParent();
712 if (DCParent2->isNamespace() &&
713 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
714 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
715 DCParent2->getParent()->isTranslationUnit())
716 Complain = false;
717 }
718 }
719
John McCall93ba8572010-03-25 06:39:04 +0000720 TemplateParameterList *PrevParams
721 = PrevClassTemplate->getTemplateParameters();
722
723 // Make sure the parameter lists match.
724 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000725 Complain,
726 Sema::TPL_TemplateMatch)) {
727 if (Complain)
728 return 0;
729
730 AdoptedPreviousTemplateParams = true;
731 InstParams = PrevParams;
732 }
John McCall93ba8572010-03-25 06:39:04 +0000733
734 // Do some additional validation, then merge default arguments
735 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000736 if (!AdoptedPreviousTemplateParams &&
737 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000738 Sema::TPC_ClassTemplate))
739 return 0;
740 }
741 }
742
John McCalle29ba202009-08-20 01:44:21 +0000743 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000744 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000745 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000746 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000747 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000748
John McCall93ba8572010-03-25 06:39:04 +0000749 if (Qualifier)
750 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000751
John McCalle29ba202009-08-20 01:44:21 +0000752 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000753 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
754 D->getIdentifier(), InstParams, RecordInst,
755 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000756 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000757
John McCall93ba8572010-03-25 06:39:04 +0000758 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000759 if (PrevClassTemplate)
760 Inst->setAccess(PrevClassTemplate->getAccess());
761 else
762 Inst->setAccess(D->getAccess());
763
John McCall93ba8572010-03-25 06:39:04 +0000764 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
765 // TODO: do we want to track the instantiation progeny of this
766 // friend target decl?
767 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000768 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000769 if (!PrevClassTemplate)
770 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000771 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000772
773 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000774 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000775 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000776
Douglas Gregor259571e2009-10-30 22:42:42 +0000777 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000778 if (isFriend) {
779 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000780 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000781 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000782
John McCalle29ba202009-08-20 01:44:21 +0000783 Owner->addDecl(Inst);
Douglas Gregord65587f2010-11-10 19:44:59 +0000784
785 if (!PrevClassTemplate) {
786 // Queue up any out-of-line partial specializations of this member
787 // class template; the client will force their instantiation once
788 // the enclosing class has been instantiated.
789 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
790 D->getPartialSpecializations(PartialSpecs);
791 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
792 if (PartialSpecs[I]->isOutOfLine())
793 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
794 }
795
John McCalle29ba202009-08-20 01:44:21 +0000796 return Inst;
797}
798
Douglas Gregord60e1052009-08-27 16:57:43 +0000799Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000800TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
801 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000802 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
803
804 // Lookup the already-instantiated declaration in the instantiation
805 // of the class template and return that.
806 DeclContext::lookup_result Found
807 = Owner->lookup(ClassTemplate->getDeclName());
808 if (Found.first == Found.second)
809 return 0;
810
811 ClassTemplateDecl *InstClassTemplate
812 = dyn_cast<ClassTemplateDecl>(*Found.first);
813 if (!InstClassTemplate)
814 return 0;
815
Douglas Gregord65587f2010-11-10 19:44:59 +0000816 if (ClassTemplatePartialSpecializationDecl *Result
817 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
818 return Result;
819
820 return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000821}
822
823Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000824TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000825 // Create a local instantiation scope for this function template, which
826 // will contain the instantiations of the template parameters and then get
827 // merged with the local instantiation scope for the function template
828 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000829 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000830
Douglas Gregord60e1052009-08-27 16:57:43 +0000831 TemplateParameterList *TempParams = D->getTemplateParameters();
832 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000833 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000834 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000835
Douglas Gregora735b202009-10-13 14:39:41 +0000836 FunctionDecl *Instantiated = 0;
837 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
838 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
839 InstParams));
840 else
841 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
842 D->getTemplatedDecl(),
843 InstParams));
844
845 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000846 return 0;
847
John McCall46460a62010-01-20 21:53:11 +0000848 Instantiated->setAccess(D->getAccess());
849
Mike Stump1eb44332009-09-09 15:08:12 +0000850 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000851 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000852 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000853 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000854 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000855 assert(InstTemplate &&
856 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000857
John McCallb1a56e72010-03-26 23:10:15 +0000858 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
859
John McCalle976ffe2009-12-14 23:19:40 +0000860 // Link the instantiation back to the pattern *unless* this is a
861 // non-definition friend declaration.
862 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000863 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000864 InstTemplate->setInstantiatedFromMemberTemplate(D);
865
John McCallb1a56e72010-03-26 23:10:15 +0000866 // Make declarations visible in the appropriate context.
867 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000868 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000869
Douglas Gregord60e1052009-08-27 16:57:43 +0000870 return InstTemplate;
871}
872
Douglas Gregord475b8d2009-03-25 21:17:03 +0000873Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
874 CXXRecordDecl *PrevDecl = 0;
875 if (D->isInjectedClassName())
876 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000877 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000878 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
879 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000880 TemplateArgs);
881 if (!Prev) return 0;
882 PrevDecl = cast<CXXRecordDecl>(Prev);
883 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000884
885 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000886 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000887 D->getLocation(), D->getIdentifier(),
888 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000889
890 // Substitute the nested name specifier, if any.
891 if (SubstQualifier(D, Record))
892 return 0;
893
Douglas Gregord475b8d2009-03-25 21:17:03 +0000894 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000895 // FIXME: Check against AS_none is an ugly hack to work around the issue that
896 // the tag decls introduced by friend class declarations don't have an access
897 // specifier. Remove once this area of the code gets sorted out.
898 if (D->getAccess() != AS_none)
899 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000900 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000901 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902
John McCall02cace72009-08-28 07:59:38 +0000903 // If the original function was part of a friend declaration,
904 // inherit its namespace state.
905 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
906 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
907
Douglas Gregor9901c572010-05-21 00:31:19 +0000908 // Make sure that anonymous structs and unions are recorded.
909 if (D->isAnonymousStructOrUnion()) {
910 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000911 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000912 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
913 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000914
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000915 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000916 return Record;
917}
918
John McCall02cace72009-08-28 07:59:38 +0000919/// Normal class members are of more specific types and therefore
920/// don't make it here. This function serves two purposes:
921/// 1) instantiating function templates
922/// 2) substituting friend declarations
923/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000924Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000925 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000926 // Check whether there is already a function template specialization for
927 // this declaration.
928 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
929 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000930 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000931 std::pair<const TemplateArgument *, unsigned> Innermost
932 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000934 FunctionDecl *SpecFunc
935 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
936 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Douglas Gregor127102b2009-06-29 20:59:39 +0000938 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000939 if (SpecFunc)
940 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000941 }
Mike Stump1eb44332009-09-09 15:08:12 +0000942
John McCallb0cb0222010-03-27 05:57:59 +0000943 bool isFriend;
944 if (FunctionTemplate)
945 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
946 else
947 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
948
Douglas Gregor79c22782010-01-16 20:21:20 +0000949 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000950 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000951 !(isa<Decl>(Owner) &&
952 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000953 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregore53060f2009-06-25 22:08:12 +0000955 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000956 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
957 TInfo = SubstFunctionType(D, Params);
958 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000959 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000960 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000961
John McCalld325daa2010-03-26 04:53:08 +0000962 NestedNameSpecifier *Qualifier = D->getQualifier();
963 if (Qualifier) {
964 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
965 D->getQualifierRange(),
966 TemplateArgs);
967 if (!Qualifier) return 0;
968 }
969
John McCall68b6b872010-02-06 01:50:47 +0000970 // If we're instantiating a local function declaration, put the result
971 // in the owner; otherwise we need to find the instantiated context.
972 DeclContext *DC;
973 if (D->getDeclContext()->isFunctionOrMethod())
974 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000975 else if (isFriend && Qualifier) {
976 CXXScopeSpec SS;
977 SS.setScopeRep(Qualifier);
978 SS.setRange(D->getQualifierRange());
979 DC = SemaRef.computeDeclContext(SS);
980 if (!DC) return 0;
981 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000982 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
983 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000984 }
John McCall68b6b872010-02-06 01:50:47 +0000985
John McCall02cace72009-08-28 07:59:38 +0000986 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000987 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000988 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000989 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000990 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000991
John McCalld325daa2010-03-26 04:53:08 +0000992 if (Qualifier)
993 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000994
John McCallb1a56e72010-03-26 23:10:15 +0000995 DeclContext *LexicalDC = Owner;
996 if (!isFriend && D->isOutOfLine()) {
997 assert(D->getDeclContext()->isFileContext());
998 LexicalDC = D->getDeclContext();
999 }
1000
1001 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Douglas Gregore53060f2009-06-25 22:08:12 +00001003 // Attach the parameters
1004 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001005 if (Params[P])
1006 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001007 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001008
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001009 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001010 if (TemplateParams) {
1011 // Our resulting instantiation is actually a function template, since we
1012 // are substituting only the outer template parameters. For example, given
1013 //
1014 // template<typename T>
1015 // struct X {
1016 // template<typename U> friend void f(T, U);
1017 // };
1018 //
1019 // X<int> x;
1020 //
1021 // We are instantiating the friend function template "f" within X<int>,
1022 // which means substituting int for T, but leaving "f" as a friend function
1023 // template.
1024 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001025 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001026 Function->getLocation(),
1027 Function->getDeclName(),
1028 TemplateParams, Function);
1029 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001030
1031 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001032
1033 if (isFriend && D->isThisDeclarationADefinition()) {
1034 // TODO: should we remember this connection regardless of whether
1035 // the friend declaration provided a body?
1036 FunctionTemplate->setInstantiatedFromMemberTemplate(
1037 D->getDescribedFunctionTemplate());
1038 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001039 } else if (FunctionTemplate) {
1040 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001041 std::pair<const TemplateArgument *, unsigned> Innermost
1042 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001043 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001044 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001045 Innermost.first,
1046 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001047 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001048 } else if (isFriend && D->isThisDeclarationADefinition()) {
1049 // TODO: should we remember this connection regardless of whether
1050 // the friend declaration provided a body?
1051 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001052 }
Douglas Gregora735b202009-10-13 14:39:41 +00001053
Douglas Gregore53060f2009-06-25 22:08:12 +00001054 if (InitFunctionInstantiation(Function, D))
1055 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Douglas Gregore53060f2009-06-25 22:08:12 +00001057 bool Redeclaration = false;
1058 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001059 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001060
John McCall68263142009-11-18 22:49:29 +00001061 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1062 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1063
John McCallaf2094e2010-04-08 09:05:18 +00001064 if (DependentFunctionTemplateSpecializationInfo *Info
1065 = D->getDependentSpecializationInfo()) {
1066 assert(isFriend && "non-friend has dependent specialization info?");
1067
1068 // This needs to be set now for future sanity.
1069 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1070
1071 // Instantiate the explicit template arguments.
1072 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1073 Info->getRAngleLoc());
1074 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1075 TemplateArgumentLoc Loc;
1076 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1077 return 0;
1078
1079 ExplicitArgs.addArgument(Loc);
1080 }
1081
1082 // Map the candidate templates to their instantiations.
1083 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1084 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1085 Info->getTemplate(I),
1086 TemplateArgs);
1087 if (!Temp) return 0;
1088
1089 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1090 }
1091
1092 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1093 &ExplicitArgs,
1094 Previous))
1095 Function->setInvalidDecl();
1096
1097 isExplicitSpecialization = true;
1098
1099 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001100 // Look only into the namespace where the friend would be declared to
1101 // find a previous declaration. This is the innermost enclosing namespace,
1102 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001103 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001104
Douglas Gregora735b202009-10-13 14:39:41 +00001105 // In C++, the previous declaration we find might be a tag type
1106 // (class or enum). In this case, the new declaration will hide the
1107 // tag type. Note that this does does not apply if we're declaring a
1108 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001109 if (Previous.isSingleTagDecl())
1110 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001111 }
1112
John McCall9f54ad42009-12-10 09:41:52 +00001113 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001114 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001115 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001116
John McCall76d32642010-04-24 01:30:58 +00001117 NamedDecl *PrincipalDecl = (TemplateParams
1118 ? cast<NamedDecl>(FunctionTemplate)
1119 : Function);
1120
Douglas Gregora735b202009-10-13 14:39:41 +00001121 // If the original function was part of a friend declaration,
1122 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001123 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001124 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001125 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001126 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001127 else
Douglas Gregora735b202009-10-13 14:39:41 +00001128 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001129
1130 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1131 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001132
Gabor Greif77535df2010-08-30 22:25:56 +00001133 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001134
Douglas Gregor238058c2010-05-18 05:45:02 +00001135 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1136 D->isThisDeclarationADefinition()) {
1137 // Check for a function body.
1138 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001139 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001140 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1141 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1142 << Function->getDeclName();
1143 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1144 Function->setInvalidDecl();
1145 }
1146 // Check for redefinitions due to other instantiations of this or
1147 // a similar friend function.
1148 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1149 REnd = Function->redecls_end();
1150 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001151 if (*R == Function)
1152 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001153 switch (R->getFriendObjectKind()) {
1154 case Decl::FOK_None:
1155 if (!queuedInstantiation && R->isUsed(false)) {
1156 if (MemberSpecializationInfo *MSInfo
1157 = Function->getMemberSpecializationInfo()) {
1158 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1159 SourceLocation Loc = R->getLocation(); // FIXME
1160 MSInfo->setPointOfInstantiation(Loc);
1161 SemaRef.PendingLocalImplicitInstantiations.push_back(
1162 std::make_pair(Function, Loc));
1163 queuedInstantiation = true;
1164 }
1165 }
1166 }
1167 break;
1168 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001169 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001170 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001171 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001172 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1173 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001174 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001175 Function->setInvalidDecl();
1176 break;
1177 }
1178 }
1179 }
1180 }
Douglas Gregora735b202009-10-13 14:39:41 +00001181 }
1182
John McCall76d32642010-04-24 01:30:58 +00001183 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1184 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1185 PrincipalDecl->setNonMemberOperator();
1186
Douglas Gregore53060f2009-06-25 22:08:12 +00001187 return Function;
1188}
1189
Douglas Gregord60e1052009-08-27 16:57:43 +00001190Decl *
1191TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1192 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001193 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1194 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001195 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001196 // We are creating a function template specialization from a function
1197 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001198 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001199 std::pair<const TemplateArgument *, unsigned> Innermost
1200 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001202 FunctionDecl *SpecFunc
1203 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1204 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Douglas Gregor6b906862009-08-21 00:16:32 +00001206 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001207 if (SpecFunc)
1208 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001209 }
1210
John McCallb0cb0222010-03-27 05:57:59 +00001211 bool isFriend;
1212 if (FunctionTemplate)
1213 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1214 else
1215 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1216
Douglas Gregor79c22782010-01-16 20:21:20 +00001217 bool MergeWithParentScope = (TemplateParams != 0) ||
1218 !(isa<Decl>(Owner) &&
1219 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001220 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001221
John McCall4eab39f2010-10-19 02:26:41 +00001222 // Instantiate enclosing template arguments for friends.
1223 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1224 unsigned NumTempParamLists = 0;
1225 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1226 TempParamLists.set_size(NumTempParamLists);
1227 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1228 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1229 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1230 if (!InstParams)
1231 return NULL;
1232 TempParamLists[I] = InstParams;
1233 }
1234 }
1235
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001236 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001237 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1238 TInfo = SubstFunctionType(D, Params);
1239 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001240 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001241 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001242
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001243 // \brief If the type of this function is not *directly* a function
1244 // type, then we're instantiating the a function that was declared
1245 // via a typedef, e.g.,
1246 //
1247 // typedef int functype(int, int);
1248 // functype func;
1249 //
1250 // In this case, we'll just go instantiate the ParmVarDecls that we
1251 // synthesized in the method declaration.
1252 if (!isa<FunctionProtoType>(T)) {
1253 assert(!Params.size() && "Instantiating type could not yield parameters");
1254 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1255 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1256 TemplateArgs);
1257 if (!P)
1258 return 0;
1259
1260 Params.push_back(P);
1261 }
1262 }
1263
John McCallb0cb0222010-03-27 05:57:59 +00001264 NestedNameSpecifier *Qualifier = D->getQualifier();
1265 if (Qualifier) {
1266 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1267 D->getQualifierRange(),
1268 TemplateArgs);
1269 if (!Qualifier) return 0;
1270 }
1271
1272 DeclContext *DC = Owner;
1273 if (isFriend) {
1274 if (Qualifier) {
1275 CXXScopeSpec SS;
1276 SS.setScopeRep(Qualifier);
1277 SS.setRange(D->getQualifierRange());
1278 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001279
1280 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1281 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001282 } else {
1283 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1284 D->getDeclContext(),
1285 TemplateArgs);
1286 }
1287 if (!DC) return 0;
1288 }
1289
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001290 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001291 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001292 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Abramo Bagnara25777432010-08-11 22:01:17 +00001294 DeclarationNameInfo NameInfo
1295 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001296 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001297 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001298 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001299 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001300 Constructor->isInlineSpecified(),
1301 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001302 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001303 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001304 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001305 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001306 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001307 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001308 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001309 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001310 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001311 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001312 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001313 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1314 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001315 D->isStatic(),
1316 D->getStorageClassAsWritten(),
1317 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001318 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001319
John McCallb0cb0222010-03-27 05:57:59 +00001320 if (Qualifier)
1321 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001322
Douglas Gregord60e1052009-08-27 16:57:43 +00001323 if (TemplateParams) {
1324 // Our resulting instantiation is actually a function template, since we
1325 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001326 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001327 // template<typename T>
1328 // struct X {
1329 // template<typename U> void f(T, U);
1330 // };
1331 //
1332 // X<int> x;
1333 //
1334 // We are instantiating the member template "f" within X<int>, which means
1335 // substituting int for T, but leaving "f" as a member function template.
1336 // Build the function template itself.
1337 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1338 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001339 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001340 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001341 if (isFriend) {
1342 FunctionTemplate->setLexicalDeclContext(Owner);
1343 FunctionTemplate->setObjectOfFriendDecl(true);
1344 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001345 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001346 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001347 } else if (FunctionTemplate) {
1348 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001349 std::pair<const TemplateArgument *, unsigned> Innermost
1350 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001351 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001352 TemplateArgumentList::CreateCopy(SemaRef.Context,
1353 Innermost.first,
1354 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001355 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001356 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001357 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001358 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001359 }
1360
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001362 // out-of-line, the instantiation will have the same lexical
1363 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001364 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001365 if (NumTempParamLists)
1366 Method->setTemplateParameterListsInfo(SemaRef.Context,
1367 NumTempParamLists,
1368 TempParamLists.data());
1369
John McCallb0cb0222010-03-27 05:57:59 +00001370 Method->setLexicalDeclContext(Owner);
1371 Method->setObjectOfFriendDecl(true);
1372 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001373 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Douglas Gregor5545e162009-03-24 00:38:23 +00001375 // Attach the parameters
1376 for (unsigned P = 0; P < Params.size(); ++P)
1377 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001378 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001379
1380 if (InitMethodInstantiation(Method, D))
1381 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001382
Abramo Bagnara25777432010-08-11 22:01:17 +00001383 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1384 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
John McCallb0cb0222010-03-27 05:57:59 +00001386 if (!FunctionTemplate || TemplateParams || isFriend) {
1387 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Douglas Gregordec06662009-08-21 18:42:58 +00001389 // In C++, the previous declaration we find might be a tag type
1390 // (class or enum). In this case, the new declaration will hide the
1391 // tag type. Note that this does does not apply if we're declaring a
1392 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001393 if (Previous.isSingleTagDecl())
1394 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001395 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001396
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001397 bool Redeclaration = false;
1398 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001399 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001400 /*FIXME:*/OverloadableAttrRequired);
1401
Douglas Gregor4ba31362009-12-01 17:24:26 +00001402 if (D->isPure())
1403 SemaRef.CheckPureMethod(Method, SourceRange());
1404
John McCall46460a62010-01-20 21:53:11 +00001405 Method->setAccess(D->getAccess());
1406
John McCallb0cb0222010-03-27 05:57:59 +00001407 if (FunctionTemplate) {
1408 // If there's a function template, let our caller handle it.
1409 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1410 // Don't hide a (potentially) valid declaration with an invalid one.
1411 } else {
1412 NamedDecl *DeclToAdd = (TemplateParams
1413 ? cast<NamedDecl>(FunctionTemplate)
1414 : Method);
1415 if (isFriend)
1416 Record->makeDeclVisibleInContext(DeclToAdd);
1417 else
1418 Owner->addDecl(DeclToAdd);
1419 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001420
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001421 return Method;
1422}
1423
Douglas Gregor615c5d42009-03-24 16:43:20 +00001424Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001425 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001426}
1427
Douglas Gregor03b2b072009-03-24 00:15:49 +00001428Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001429 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001430}
1431
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001432Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001433 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001434}
1435
Douglas Gregor6477b692009-03-25 15:04:13 +00001436ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001437 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001438}
1439
John McCalle29ba202009-08-20 01:44:21 +00001440Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1441 TemplateTypeParmDecl *D) {
1442 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001443 const Type* T = D->getTypeForDecl();
1444 assert(T->isTemplateTypeParmType());
1445 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001446
John McCalle29ba202009-08-20 01:44:21 +00001447 TemplateTypeParmDecl *Inst =
1448 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001449 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001450 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001451 D->wasDeclaredWithTypename(),
1452 D->isParameterPack());
1453
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001454 if (D->hasDefaultArgument())
1455 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001456
Douglas Gregor550d9b22009-10-31 17:21:17 +00001457 // Introduce this template parameter's instantiation into the instantiation
1458 // scope.
1459 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1460
John McCalle29ba202009-08-20 01:44:21 +00001461 return Inst;
1462}
1463
Douglas Gregor33642df2009-10-23 23:25:44 +00001464Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1465 NonTypeTemplateParmDecl *D) {
1466 // Substitute into the type of the non-type template parameter.
1467 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001468 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001469 if (DI) {
1470 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1471 D->getDeclName());
1472 if (DI) T = DI->getType();
1473 } else {
1474 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1475 D->getDeclName());
1476 DI = 0;
1477 }
1478 if (T.isNull())
1479 return 0;
1480
1481 // Check that this type is acceptable for a non-type template parameter.
1482 bool Invalid = false;
1483 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1484 if (T.isNull()) {
1485 T = SemaRef.Context.IntTy;
1486 Invalid = true;
1487 }
1488
1489 NonTypeTemplateParmDecl *Param
1490 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001491 D->getDepth() - TemplateArgs.getNumLevels(),
1492 D->getPosition(), D->getIdentifier(), T,
1493 DI);
Douglas Gregor33642df2009-10-23 23:25:44 +00001494 if (Invalid)
1495 Param->setInvalidDecl();
1496
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001497 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001498
1499 // Introduce this template parameter's instantiation into the instantiation
1500 // scope.
1501 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001502 return Param;
1503}
1504
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001505Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001506TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1507 TemplateTemplateParmDecl *D) {
1508 // Instantiate the template parameter list of the template template parameter.
1509 TemplateParameterList *TempParams = D->getTemplateParameters();
1510 TemplateParameterList *InstParams;
1511 {
1512 // Perform the actual substitution of template parameters within a new,
1513 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001514 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001515 InstParams = SubstTemplateParams(TempParams);
1516 if (!InstParams)
1517 return NULL;
1518 }
1519
1520 // Build the template template parameter.
1521 TemplateTemplateParmDecl *Param
1522 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001523 D->getDepth() - TemplateArgs.getNumLevels(),
1524 D->getPosition(), D->getIdentifier(),
1525 InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001526 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001527
Douglas Gregor9106ef72009-11-11 16:58:32 +00001528 // Introduce this template parameter's instantiation into the instantiation
1529 // scope.
1530 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1531
1532 return Param;
1533}
1534
Douglas Gregor48c32a72009-11-17 06:07:40 +00001535Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1536 // Using directives are never dependent, so they require no explicit
1537
1538 UsingDirectiveDecl *Inst
1539 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1540 D->getNamespaceKeyLocation(),
1541 D->getQualifierRange(), D->getQualifier(),
1542 D->getIdentLocation(),
1543 D->getNominatedNamespace(),
1544 D->getCommonAncestor());
1545 Owner->addDecl(Inst);
1546 return Inst;
1547}
1548
John McCalled976492009-12-04 22:46:56 +00001549Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001550
1551 // The nested name specifier may be dependent, for example
1552 // template <typename T> struct t {
1553 // struct s1 { T f1(); };
1554 // struct s2 : s1 { using s1::f1; };
1555 // };
1556 // template struct t<int>;
1557 // Here, in using s1::f1, s1 refers to t<T>::s1;
1558 // we need to substitute for t<int>::s1.
1559 NestedNameSpecifier *NNS =
1560 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
1561 D->getNestedNameRange(),
1562 TemplateArgs);
1563 if (!NNS)
1564 return 0;
1565
1566 // The name info is non-dependent, so no transformation
1567 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001568 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001569
John McCall9f54ad42009-12-10 09:41:52 +00001570 // We only need to do redeclaration lookups if we're in a class
1571 // scope (in fact, it's not really even possible in non-class
1572 // scopes).
1573 bool CheckRedeclaration = Owner->isRecord();
1574
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001575 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1576 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001577
John McCalled976492009-12-04 22:46:56 +00001578 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001579 D->getNestedNameRange(),
1580 D->getUsingLocation(),
Douglas Gregor1b398202010-09-29 17:58:28 +00001581 NNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001582 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001583 D->isTypeName());
1584
1585 CXXScopeSpec SS;
Douglas Gregor1b398202010-09-29 17:58:28 +00001586 SS.setScopeRep(NNS);
John McCalled976492009-12-04 22:46:56 +00001587 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001588
1589 if (CheckRedeclaration) {
1590 Prev.setHideTags(false);
1591 SemaRef.LookupQualifiedName(Prev, Owner);
1592
1593 // Check for invalid redeclarations.
1594 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1595 D->isTypeName(), SS,
1596 D->getLocation(), Prev))
1597 NewUD->setInvalidDecl();
1598
1599 }
1600
1601 if (!NewUD->isInvalidDecl() &&
1602 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001603 D->getLocation()))
1604 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001605
John McCalled976492009-12-04 22:46:56 +00001606 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1607 NewUD->setAccess(D->getAccess());
1608 Owner->addDecl(NewUD);
1609
John McCall9f54ad42009-12-10 09:41:52 +00001610 // Don't process the shadow decls for an invalid decl.
1611 if (NewUD->isInvalidDecl())
1612 return NewUD;
1613
John McCall323c3102009-12-22 22:26:37 +00001614 bool isFunctionScope = Owner->isFunctionOrMethod();
1615
John McCall9f54ad42009-12-10 09:41:52 +00001616 // Process the shadow decls.
1617 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1618 I != E; ++I) {
1619 UsingShadowDecl *Shadow = *I;
1620 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001621 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1622 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001623 TemplateArgs));
1624
1625 if (CheckRedeclaration &&
1626 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1627 continue;
1628
1629 UsingShadowDecl *InstShadow
1630 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1631 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001632
1633 if (isFunctionScope)
1634 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001635 }
John McCalled976492009-12-04 22:46:56 +00001636
1637 return NewUD;
1638}
1639
1640Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001641 // Ignore these; we handle them in bulk when processing the UsingDecl.
1642 return 0;
John McCalled976492009-12-04 22:46:56 +00001643}
1644
John McCall7ba107a2009-11-18 02:36:19 +00001645Decl * TemplateDeclInstantiator
1646 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001647 NestedNameSpecifier *NNS =
1648 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1649 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001650 TemplateArgs);
1651 if (!NNS)
1652 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001654 CXXScopeSpec SS;
1655 SS.setRange(D->getTargetNestedNameRange());
1656 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001658 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1659 // Hence, no tranformation is required for it.
1660 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001661 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001662 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001663 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001664 /*instantiation*/ true,
1665 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001666 if (UD)
John McCalled976492009-12-04 22:46:56 +00001667 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1668
John McCall7ba107a2009-11-18 02:36:19 +00001669 return UD;
1670}
1671
1672Decl * TemplateDeclInstantiator
1673 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1674 NestedNameSpecifier *NNS =
1675 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1676 D->getTargetNestedNameRange(),
1677 TemplateArgs);
1678 if (!NNS)
1679 return 0;
1680
1681 CXXScopeSpec SS;
1682 SS.setRange(D->getTargetNestedNameRange());
1683 SS.setScopeRep(NNS);
1684
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001685 DeclarationNameInfo NameInfo
1686 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1687
John McCall7ba107a2009-11-18 02:36:19 +00001688 NamedDecl *UD =
1689 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001690 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001691 /*instantiation*/ true,
1692 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001693 if (UD)
John McCalled976492009-12-04 22:46:56 +00001694 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1695
Anders Carlsson0d8df782009-08-29 19:37:28 +00001696 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001697}
1698
John McCallce3ff2b2009-08-25 22:02:44 +00001699Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001700 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001701 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001702 if (D->isInvalidDecl())
1703 return 0;
1704
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001705 return Instantiator.Visit(D);
1706}
1707
John McCalle29ba202009-08-20 01:44:21 +00001708/// \brief Instantiates a nested template parameter list in the current
1709/// instantiation context.
1710///
1711/// \param L The parameter list to instantiate
1712///
1713/// \returns NULL if there was an error
1714TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001715TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001716 // Get errors for all the parameters before bailing out.
1717 bool Invalid = false;
1718
1719 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001720 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001721 ParamVector Params;
1722 Params.reserve(N);
1723 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1724 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001725 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001726 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001727 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001728 }
1729
1730 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001731 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001732 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001733
1734 TemplateParameterList *InstL
1735 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1736 L->getLAngleLoc(), &Params.front(), N,
1737 L->getRAngleLoc());
1738 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001739}
John McCalle29ba202009-08-20 01:44:21 +00001740
Douglas Gregored9c0f92009-10-29 00:04:11 +00001741/// \brief Instantiate the declaration of a class template partial
1742/// specialization.
1743///
1744/// \param ClassTemplate the (instantiated) class template that is partially
1745// specialized by the instantiation of \p PartialSpec.
1746///
1747/// \param PartialSpec the (uninstantiated) class template partial
1748/// specialization that we are instantiating.
1749///
Douglas Gregord65587f2010-11-10 19:44:59 +00001750/// \returns The instantiated partial specialization, if successful; otherwise,
1751/// NULL to indicate an error.
1752ClassTemplatePartialSpecializationDecl *
Douglas Gregored9c0f92009-10-29 00:04:11 +00001753TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1754 ClassTemplateDecl *ClassTemplate,
1755 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001756 // Create a local instantiation scope for this class template partial
1757 // specialization, which will contain the instantiations of the template
1758 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001759 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001760
Douglas Gregored9c0f92009-10-29 00:04:11 +00001761 // Substitute into the template parameters of the class template partial
1762 // specialization.
1763 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1764 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1765 if (!InstParams)
Douglas Gregord65587f2010-11-10 19:44:59 +00001766 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001767
1768 // Substitute into the template arguments of the class template partial
1769 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001770 const TemplateArgumentLoc *PartialSpecTemplateArgs
1771 = PartialSpec->getTemplateArgsAsWritten();
1772 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1773
John McCalld5532b62009-11-23 01:53:49 +00001774 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001775 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001776 TemplateArgumentLoc Loc;
1777 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregord65587f2010-11-10 19:44:59 +00001778 return 0;
John McCalld5532b62009-11-23 01:53:49 +00001779 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001780 }
1781
1782
1783 // Check that the template argument list is well-formed for this
1784 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001785 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001786 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1787 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001788 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001789 false,
1790 Converted))
Douglas Gregord65587f2010-11-10 19:44:59 +00001791 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001792
1793 // Figure out where to insert this class template partial specialization
1794 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001795 void *InsertPos = 0;
1796 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001797 = ClassTemplate->findPartialSpecialization(Converted.data(),
1798 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001799
1800 // Build the canonical type that describes the converted template
1801 // arguments of the class template partial specialization.
1802 QualType CanonType
1803 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001804 Converted.data(),
1805 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001806
1807 // Build the fully-sugared type for this class template
1808 // specialization as the user wrote in the specialization
1809 // itself. This means that we'll pretty-print the type retrieved
1810 // from the specialization's declaration the way that the user
1811 // actually wrote the specialization, rather than formatting the
1812 // name based on the "canonical" representation used to store the
1813 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001814 TypeSourceInfo *WrittenTy
1815 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1816 TemplateName(ClassTemplate),
1817 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001818 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001819 CanonType);
1820
1821 if (PrevDecl) {
1822 // We've already seen a partial specialization with the same template
1823 // parameters and template arguments. This can happen, for example, when
1824 // substituting the outer template arguments ends up causing two
1825 // class template partial specializations of a member class template
1826 // to have identical forms, e.g.,
1827 //
1828 // template<typename T, typename U>
1829 // struct Outer {
1830 // template<typename X, typename Y> struct Inner;
1831 // template<typename Y> struct Inner<T, Y>;
1832 // template<typename Y> struct Inner<U, Y>;
1833 // };
1834 //
1835 // Outer<int, int> outer; // error: the partial specializations of Inner
1836 // // have the same signature.
1837 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
Douglas Gregord65587f2010-11-10 19:44:59 +00001838 << WrittenTy->getType();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001839 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1840 << SemaRef.Context.getTypeDeclType(PrevDecl);
Douglas Gregord65587f2010-11-10 19:44:59 +00001841 return 0;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001842 }
1843
1844
1845 // Create the class template partial specialization declaration.
1846 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001847 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1848 PartialSpec->getTagKind(),
1849 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001850 PartialSpec->getLocation(),
1851 InstParams,
1852 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001853 Converted.data(),
1854 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001855 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001856 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001857 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001858 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001859 // Substitute the nested name specifier, if any.
1860 if (SubstQualifier(PartialSpec, InstPartialSpec))
1861 return 0;
1862
Douglas Gregored9c0f92009-10-29 00:04:11 +00001863 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001864 InstPartialSpec->setTypeAsWritten(WrittenTy);
1865
Douglas Gregored9c0f92009-10-29 00:04:11 +00001866 // Add this partial specialization to the set of class template partial
1867 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001868 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregord65587f2010-11-10 19:44:59 +00001869 return InstPartialSpec;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001870}
1871
John McCall21ef0fa2010-03-11 09:03:00 +00001872TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001873TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001874 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001875 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1876 assert(OldTInfo && "substituting function without type source info");
1877 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001878 TypeSourceInfo *NewTInfo
1879 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1880 D->getTypeSpecStartLoc(),
1881 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001882 if (!NewTInfo)
1883 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001884
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001885 if (NewTInfo != OldTInfo) {
1886 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001887 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001888 if (FunctionProtoTypeLoc *OldProtoLoc
1889 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1890 TypeLoc NewTL = NewTInfo->getTypeLoc();
1891 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1892 assert(NewProtoLoc && "Missing prototype?");
1893 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1894 // FIXME: Variadic templates will break this.
1895 Params.push_back(NewProtoLoc->getArg(i));
1896 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001897 OldProtoLoc->getArg(i),
1898 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001899 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001900 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001901 } else {
1902 // The function type itself was not dependent and therefore no
1903 // substitution occurred. However, we still need to instantiate
1904 // the function parameters themselves.
1905 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001906 if (FunctionProtoTypeLoc *OldProtoLoc
1907 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1908 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1909 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1910 if (!Parm)
1911 return 0;
1912 Params.push_back(Parm);
1913 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001914 }
1915 }
John McCall21ef0fa2010-03-11 09:03:00 +00001916 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001917}
1918
Mike Stump1eb44332009-09-09 15:08:12 +00001919/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001920/// declaration (New) from the corresponding fields of its template (Tmpl).
1921///
1922/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001923bool
1924TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001925 FunctionDecl *Tmpl) {
1926 if (Tmpl->isDeleted())
1927 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Douglas Gregorcca9e962009-07-01 22:01:06 +00001929 // If we are performing substituting explicitly-specified template arguments
1930 // or deduced template arguments into a function template and we reach this
1931 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001932 // to keeping the new function template specialization. We therefore
1933 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001934 // into a template instantiation for this specific function template
1935 // specialization, which is not a SFINAE context, so that we diagnose any
1936 // further errors in the declaration itself.
1937 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1938 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1939 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1940 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001941 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001942 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001943 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001944 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001945 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001946 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1947 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001948 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001949 }
1950 }
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001952 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1953 assert(Proto && "Function template without prototype?");
1954
1955 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1956 Proto->getNoReturnAttr()) {
1957 // The function has an exception specification or a "noreturn"
1958 // attribute. Substitute into each of the exception types.
1959 llvm::SmallVector<QualType, 4> Exceptions;
1960 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1961 // FIXME: Poor location information!
1962 QualType T
1963 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1964 New->getLocation(), New->getDeclName());
1965 if (T.isNull() ||
1966 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1967 continue;
1968
1969 Exceptions.push_back(T);
1970 }
1971
1972 // Rebuild the function type
1973
1974 const FunctionProtoType *NewProto
1975 = New->getType()->getAs<FunctionProtoType>();
1976 assert(NewProto && "Template instantiation without function prototype?");
1977 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1978 NewProto->arg_type_begin(),
1979 NewProto->getNumArgs(),
1980 NewProto->isVariadic(),
1981 NewProto->getTypeQuals(),
1982 Proto->hasExceptionSpec(),
1983 Proto->hasAnyExceptionSpec(),
1984 Exceptions.size(),
1985 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001986 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001987 }
1988
John McCall1d8d1cc2010-08-01 02:01:53 +00001989 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001990
Douglas Gregore53060f2009-06-25 22:08:12 +00001991 return false;
1992}
1993
Douglas Gregor5545e162009-03-24 00:38:23 +00001994/// \brief Initializes common fields of an instantiated method
1995/// declaration (New) from the corresponding fields of its template
1996/// (Tmpl).
1997///
1998/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001999bool
2000TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002001 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002002 if (InitFunctionInstantiation(New, Tmpl))
2003 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Douglas Gregor5545e162009-03-24 00:38:23 +00002005 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002006 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002007 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002008
2009 // FIXME: attributes
2010 // FIXME: New needs a pointer to Tmpl
2011 return false;
2012}
Douglas Gregora58861f2009-05-13 20:28:22 +00002013
2014/// \brief Instantiate the definition of the given function from its
2015/// template.
2016///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002017/// \param PointOfInstantiation the point at which the instantiation was
2018/// required. Note that this is not precisely a "point of instantiation"
2019/// for the function, but it's close.
2020///
Douglas Gregora58861f2009-05-13 20:28:22 +00002021/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002022/// function template specialization or member function of a class template
2023/// specialization.
2024///
2025/// \param Recursive if true, recursively instantiates any functions that
2026/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002027///
2028/// \param DefinitionRequired if true, then we are performing an explicit
2029/// instantiation where the body of the function is required. Complain if
2030/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002031void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002032 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002033 bool Recursive,
2034 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002035 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002036 return;
2037
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002038 // Never instantiate an explicit specialization.
2039 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2040 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002041
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002042 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002043 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002044 Stmt *Pattern = 0;
2045 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002046 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002047
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002048 if (!Pattern) {
2049 if (DefinitionRequired) {
2050 if (Function->getPrimaryTemplate())
2051 Diag(PointOfInstantiation,
2052 diag::err_explicit_instantiation_undefined_func_template)
2053 << Function->getPrimaryTemplate();
2054 else
2055 Diag(PointOfInstantiation,
2056 diag::err_explicit_instantiation_undefined_member)
2057 << 1 << Function->getDeclName() << Function->getDeclContext();
2058
2059 if (PatternDecl)
2060 Diag(PatternDecl->getLocation(),
2061 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002062 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002063 } else if (Function->getTemplateSpecializationKind()
2064 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002065 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002066 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002067 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002068
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002069 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002070 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002071
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002072 // C++0x [temp.explicit]p9:
2073 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002074 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002075 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002076 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002077 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002078 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002079 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002081 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2082 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002083 return;
2084
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002085 // If we're performing recursive template instantiation, create our own
2086 // queue of pending implicit instantiations that we will instantiate later,
2087 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002088 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002089 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002090 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Douglas Gregor9679caf2010-05-12 17:27:19 +00002092 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002093 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002094 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002095
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002096 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002097 // recorded, unless we're actually a member function within a local
2098 // class, in which case we need to merge our results with the parent
2099 // scope (of the enclosing function).
2100 bool MergeWithParentScope = false;
2101 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2102 MergeWithParentScope = Rec->isLocalClass();
2103
2104 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002106 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002107 // instantiation scope, and set the parameter names to those used
2108 // in the template.
2109 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2110 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2111 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2112 FunctionParam->setDeclName(PatternParam->getDeclName());
2113 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2114 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002115
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002116 // Enter the scope of this instantiation. We don't use
2117 // PushDeclContext because we don't have a scope.
2118 DeclContext *PreviousContext = CurContext;
2119 CurContext = Function;
2120
Mike Stump1eb44332009-09-09 15:08:12 +00002121 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002122 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002123
2124 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002125 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002126 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2127 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2128 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002129 }
2130
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002131 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002132 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002133
Douglas Gregor52604ab2009-09-11 21:19:12 +00002134 if (Body.isInvalid())
2135 Function->setInvalidDecl();
2136
John McCall9ae2f072010-08-23 23:25:46 +00002137 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002138 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002139
John McCall0c01d182010-03-24 05:22:00 +00002140 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2141
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002142 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002143
2144 DeclGroupRef DG(Function);
2145 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Douglas Gregor60406be2010-01-16 22:29:39 +00002147 // This class may have local implicit instantiations that need to be
2148 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002149 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002150 Scope.Exit();
2151
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002152 if (Recursive) {
2153 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002154 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002155 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002157 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002158 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002159 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002160}
2161
2162/// \brief Instantiate the definition of the given variable from its
2163/// template.
2164///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002165/// \param PointOfInstantiation the point at which the instantiation was
2166/// required. Note that this is not precisely a "point of instantiation"
2167/// for the function, but it's close.
2168///
2169/// \param Var the already-instantiated declaration of a static member
2170/// variable of a class template specialization.
2171///
2172/// \param Recursive if true, recursively instantiates any functions that
2173/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002174///
2175/// \param DefinitionRequired if true, then we are performing an explicit
2176/// instantiation where an out-of-line definition of the member variable
2177/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002178void Sema::InstantiateStaticDataMemberDefinition(
2179 SourceLocation PointOfInstantiation,
2180 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002181 bool Recursive,
2182 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002183 if (Var->isInvalidDecl())
2184 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregor7caa6822009-07-24 20:34:43 +00002186 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002187 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002188 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002189 assert(Def->isStaticDataMember() && "Not a static data member?");
2190 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002191
Douglas Gregor0d035142009-10-27 18:42:08 +00002192 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002193 // We did not find an out-of-line definition of this static data member,
2194 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002195 // instantiate this definition (or provide a specialization for it) in
2196 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002197 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002198 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002199 Diag(PointOfInstantiation,
2200 diag::err_explicit_instantiation_undefined_member)
2201 << 2 << Var->getDeclName() << Var->getDeclContext();
2202 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002203 } else if (Var->getTemplateSpecializationKind()
2204 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002205 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002206 std::make_pair(Var, PointOfInstantiation));
2207 }
2208
Douglas Gregor7caa6822009-07-24 20:34:43 +00002209 return;
2210 }
2211
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002212 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002213 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002214 return;
2215
2216 // C++0x [temp.explicit]p9:
2217 // Except for inline functions, other explicit instantiation declarations
2218 // have the effect of suppressing the implicit instantiation of the entity
2219 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002220 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002221 == TSK_ExplicitInstantiationDeclaration)
2222 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Douglas Gregor7caa6822009-07-24 20:34:43 +00002224 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2225 if (Inst)
2226 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Douglas Gregor7caa6822009-07-24 20:34:43 +00002228 // If we're performing recursive template instantiation, create our own
2229 // queue of pending implicit instantiations that we will instantiate later,
2230 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002231 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002232 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002233 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002234
Douglas Gregor7caa6822009-07-24 20:34:43 +00002235 // Enter the scope of this instantiation. We don't use
2236 // PushDeclContext because we don't have a scope.
2237 DeclContext *PreviousContext = CurContext;
2238 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002240 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002241 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002242 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002243 CurContext = PreviousContext;
2244
2245 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002246 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2247 assert(MSInfo && "Missing member specialization information?");
2248 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2249 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002250 DeclGroupRef DG(Var);
2251 Consumer.HandleTopLevelDecl(DG);
2252 }
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Douglas Gregor7caa6822009-07-24 20:34:43 +00002254 if (Recursive) {
2255 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002256 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002257 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Douglas Gregor7caa6822009-07-24 20:34:43 +00002259 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002260 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002261 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002262}
Douglas Gregor815215d2009-05-27 05:35:12 +00002263
Anders Carlsson09025312009-08-29 05:16:22 +00002264void
2265Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2266 const CXXConstructorDecl *Tmpl,
2267 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Anders Carlsson09025312009-08-29 05:16:22 +00002269 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002270 bool AnyErrors = false;
2271
Anders Carlsson09025312009-08-29 05:16:22 +00002272 // Instantiate all the initializers.
2273 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002274 InitsEnd = Tmpl->init_end();
2275 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002276 CXXBaseOrMemberInitializer *Init = *Inits;
2277
Chandler Carruth030ef472010-09-03 21:54:20 +00002278 // Only instantiate written initializers, let Sema re-construct implicit
2279 // ones.
2280 if (!Init->isWritten())
2281 continue;
2282
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002283 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002284 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002286 // Instantiate the initializer.
2287 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002288 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002289 AnyErrors = true;
2290 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002291 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002292
Anders Carlsson09025312009-08-29 05:16:22 +00002293 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002294 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002295 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002296 TemplateArgs,
2297 Init->getSourceLocation(),
2298 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002299 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002300 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002301 New->setInvalidDecl();
2302 continue;
2303 }
2304
John McCalla93c9342009-12-07 02:54:59 +00002305 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002306 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002307 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002308 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002309 Init->getRParenLoc(),
2310 New->getParent());
2311 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002312 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002314 // Is this an anonymous union?
2315 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002316 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2317 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002318 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002319 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2320 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002321 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002322
2323 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002324 NewArgs.size(),
2325 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002326 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002327 Init->getRParenLoc());
2328 }
2329
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002330 if (NewInit.isInvalid()) {
2331 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002332 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002333 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002334 // FIXME: It would be nice if ASTOwningVector had a release function.
2335 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Anders Carlsson09025312009-08-29 05:16:22 +00002337 NewInits.push_back((MemInitTy *)NewInit.get());
2338 }
2339 }
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Anders Carlsson09025312009-08-29 05:16:22 +00002341 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002342 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002343 /*FIXME: ColonLoc */
2344 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002345 NewInits.data(), NewInits.size(),
2346 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002347}
2348
John McCall52a575a2009-08-29 08:11:13 +00002349// TODO: this could be templated if the various decl types used the
2350// same method name.
2351static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2352 ClassTemplateDecl *Instance) {
2353 Pattern = Pattern->getCanonicalDecl();
2354
2355 do {
2356 Instance = Instance->getCanonicalDecl();
2357 if (Pattern == Instance) return true;
2358 Instance = Instance->getInstantiatedFromMemberTemplate();
2359 } while (Instance);
2360
2361 return false;
2362}
2363
Douglas Gregor0d696532009-09-28 06:34:35 +00002364static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2365 FunctionTemplateDecl *Instance) {
2366 Pattern = Pattern->getCanonicalDecl();
2367
2368 do {
2369 Instance = Instance->getCanonicalDecl();
2370 if (Pattern == Instance) return true;
2371 Instance = Instance->getInstantiatedFromMemberTemplate();
2372 } while (Instance);
2373
2374 return false;
2375}
2376
Douglas Gregored9c0f92009-10-29 00:04:11 +00002377static bool
2378isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2379 ClassTemplatePartialSpecializationDecl *Instance) {
2380 Pattern
2381 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2382 do {
2383 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2384 Instance->getCanonicalDecl());
2385 if (Pattern == Instance)
2386 return true;
2387 Instance = Instance->getInstantiatedFromMember();
2388 } while (Instance);
2389
2390 return false;
2391}
2392
John McCall52a575a2009-08-29 08:11:13 +00002393static bool isInstantiationOf(CXXRecordDecl *Pattern,
2394 CXXRecordDecl *Instance) {
2395 Pattern = Pattern->getCanonicalDecl();
2396
2397 do {
2398 Instance = Instance->getCanonicalDecl();
2399 if (Pattern == Instance) return true;
2400 Instance = Instance->getInstantiatedFromMemberClass();
2401 } while (Instance);
2402
2403 return false;
2404}
2405
2406static bool isInstantiationOf(FunctionDecl *Pattern,
2407 FunctionDecl *Instance) {
2408 Pattern = Pattern->getCanonicalDecl();
2409
2410 do {
2411 Instance = Instance->getCanonicalDecl();
2412 if (Pattern == Instance) return true;
2413 Instance = Instance->getInstantiatedFromMemberFunction();
2414 } while (Instance);
2415
2416 return false;
2417}
2418
2419static bool isInstantiationOf(EnumDecl *Pattern,
2420 EnumDecl *Instance) {
2421 Pattern = Pattern->getCanonicalDecl();
2422
2423 do {
2424 Instance = Instance->getCanonicalDecl();
2425 if (Pattern == Instance) return true;
2426 Instance = Instance->getInstantiatedFromMemberEnum();
2427 } while (Instance);
2428
2429 return false;
2430}
2431
John McCalled976492009-12-04 22:46:56 +00002432static bool isInstantiationOf(UsingShadowDecl *Pattern,
2433 UsingShadowDecl *Instance,
2434 ASTContext &C) {
2435 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2436}
2437
2438static bool isInstantiationOf(UsingDecl *Pattern,
2439 UsingDecl *Instance,
2440 ASTContext &C) {
2441 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2442}
2443
John McCall7ba107a2009-11-18 02:36:19 +00002444static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2445 UsingDecl *Instance,
2446 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002447 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002448}
2449
2450static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002451 UsingDecl *Instance,
2452 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002453 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002454}
2455
John McCall52a575a2009-08-29 08:11:13 +00002456static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2457 VarDecl *Instance) {
2458 assert(Instance->isStaticDataMember());
2459
2460 Pattern = Pattern->getCanonicalDecl();
2461
2462 do {
2463 Instance = Instance->getCanonicalDecl();
2464 if (Pattern == Instance) return true;
2465 Instance = Instance->getInstantiatedFromStaticDataMember();
2466 } while (Instance);
2467
2468 return false;
2469}
2470
John McCalled976492009-12-04 22:46:56 +00002471// Other is the prospective instantiation
2472// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002473static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002474 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002475 if (UnresolvedUsingTypenameDecl *UUD
2476 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2477 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2478 return isInstantiationOf(UUD, UD, Ctx);
2479 }
2480 }
2481
2482 if (UnresolvedUsingValueDecl *UUD
2483 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002484 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2485 return isInstantiationOf(UUD, UD, Ctx);
2486 }
2487 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002488
Anders Carlsson0d8df782009-08-29 19:37:28 +00002489 return false;
2490 }
Mike Stump1eb44332009-09-09 15:08:12 +00002491
John McCall52a575a2009-08-29 08:11:13 +00002492 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2493 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002494
John McCall52a575a2009-08-29 08:11:13 +00002495 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2496 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002497
John McCall52a575a2009-08-29 08:11:13 +00002498 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2499 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002500
Douglas Gregor7caa6822009-07-24 20:34:43 +00002501 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002502 if (Var->isStaticDataMember())
2503 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2504
2505 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2506 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002507
Douglas Gregor0d696532009-09-28 06:34:35 +00002508 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2509 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2510
Douglas Gregored9c0f92009-10-29 00:04:11 +00002511 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2512 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2513 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2514 PartialSpec);
2515
Anders Carlssond8b285f2009-09-01 04:26:58 +00002516 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2517 if (!Field->getDeclName()) {
2518 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002519 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002520 cast<FieldDecl>(D);
2521 }
2522 }
Mike Stump1eb44332009-09-09 15:08:12 +00002523
John McCalled976492009-12-04 22:46:56 +00002524 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2525 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2526
2527 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2528 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2529
Douglas Gregor815215d2009-05-27 05:35:12 +00002530 return D->getDeclName() && isa<NamedDecl>(Other) &&
2531 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2532}
2533
2534template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002535static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002536 NamedDecl *D,
2537 ForwardIterator first,
2538 ForwardIterator last) {
2539 for (; first != last; ++first)
2540 if (isInstantiationOf(Ctx, D, *first))
2541 return cast<NamedDecl>(*first);
2542
2543 return 0;
2544}
2545
John McCall02cace72009-08-28 07:59:38 +00002546/// \brief Finds the instantiation of the given declaration context
2547/// within the current instantiation.
2548///
2549/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002550DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002551 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002552 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002553 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002554 return cast_or_null<DeclContext>(ID);
2555 } else return DC;
2556}
2557
Douglas Gregored961e72009-05-27 17:54:46 +00002558/// \brief Find the instantiation of the given declaration within the
2559/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002560///
2561/// This routine is intended to be used when \p D is a declaration
2562/// referenced from within a template, that needs to mapped into the
2563/// corresponding declaration within an instantiation. For example,
2564/// given:
2565///
2566/// \code
2567/// template<typename T>
2568/// struct X {
2569/// enum Kind {
2570/// KnownValue = sizeof(T)
2571/// };
2572///
2573/// bool getKind() const { return KnownValue; }
2574/// };
2575///
2576/// template struct X<int>;
2577/// \endcode
2578///
2579/// In the instantiation of X<int>::getKind(), we need to map the
2580/// EnumConstantDecl for KnownValue (which refers to
2581/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002582/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2583/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002584NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002585 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002586 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002587 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002588 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002589 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002590 // D is a local of some kind. Look into the map of local
2591 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002592 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002593 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002594
Douglas Gregore95b4092009-09-16 18:34:49 +00002595 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2596 if (!Record->isDependentContext())
2597 return D;
2598
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002599 // If the RecordDecl is actually the injected-class-name or a
2600 // "templated" declaration for a class template, class template
2601 // partial specialization, or a member class of a class template,
2602 // substitute into the injected-class-name of the class template
2603 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002604 QualType T;
2605 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2606
2607 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002608 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002609 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2610 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002611 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002612
2613 // If we call SubstType with an InjectedClassNameType here we
2614 // can end up in an infinite loop.
2615 T = Context.getTypeDeclType(Record);
2616 assert(isa<InjectedClassNameType>(T) &&
2617 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002618 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002619 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002620
2621 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002622 // Substitute into the injected-class-name to get the type
2623 // corresponding to the instantiation we want, which may also be
2624 // the current instantiation (if we're in a template
2625 // definition). This substitution should never fail, since we
2626 // know we can instantiate the injected-class-name or we
2627 // wouldn't have gotten to the injected-class-name!
2628
2629 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2630 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002631 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2632 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2633
2634 if (!T->isDependentType()) {
2635 assert(T->isRecordType() && "Instantiation must produce a record type");
2636 return T->getAs<RecordType>()->getDecl();
2637 }
2638
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002639 // We are performing "partial" template instantiation to create
2640 // the member declarations for the members of a class template
2641 // specialization. Therefore, D is actually referring to something
2642 // in the current instantiation. Look through the current
2643 // context, which contains actual instantiations, to find the
2644 // instantiation of the "current instantiation" that D refers
2645 // to.
2646 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002647 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002648 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002649 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002650 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002651 if (isInstantiationOf(ClassTemplate,
2652 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002653 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002654
2655 if (!DC->isDependentContext())
2656 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002657 }
2658
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002659 // We're performing "instantiation" of a member of the current
2660 // instantiation while we are type-checking the
2661 // definition. Compute the declaration context and return that.
2662 assert(!SawNonDependentContext &&
2663 "No dependent context while instantiating record");
2664 DeclContext *DC = computeDeclContext(T);
2665 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002666 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002667 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002668 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002669
Douglas Gregore95b4092009-09-16 18:34:49 +00002670 // Fall through to deal with other dependent record types (e.g.,
2671 // anonymous unions in class templates).
2672 }
John McCall52a575a2009-08-29 08:11:13 +00002673
Douglas Gregore95b4092009-09-16 18:34:49 +00002674 if (!ParentDC->isDependentContext())
2675 return D;
2676
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002677 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002678 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002679 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Douglas Gregor815215d2009-05-27 05:35:12 +00002681 if (ParentDC != D->getDeclContext()) {
2682 // We performed some kind of instantiation in the parent context,
2683 // so now we need to look into the instantiated parent context to
2684 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002685
John McCall3cb0ebd2010-03-10 03:28:59 +00002686 // If our context used to be dependent, we may need to instantiate
2687 // it before performing lookup into that context.
2688 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002689 if (!Spec->isDependentContext()) {
2690 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002691 const RecordType *Tag = T->getAs<RecordType>();
2692 assert(Tag && "type of non-dependent record is not a RecordType");
2693 if (!Tag->isBeingDefined() &&
2694 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2695 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002696
2697 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002698 }
2699 }
2700
Douglas Gregor815215d2009-05-27 05:35:12 +00002701 NamedDecl *Result = 0;
2702 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002703 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002704 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2705 } else {
2706 // Since we don't have a name for the entity we're looking for,
2707 // our only option is to walk through all of the declarations to
2708 // find that name. This will occur in a few cases:
2709 //
2710 // - anonymous struct/union within a template
2711 // - unnamed class/struct/union/enum within a template
2712 //
2713 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002714 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002715 ParentDC->decls_begin(),
2716 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002717 }
Mike Stump1eb44332009-09-09 15:08:12 +00002718
John McCall9f54ad42009-12-10 09:41:52 +00002719 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002720 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2721 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002722 && "Unable to find instantiation of declaration!");
2723
Douglas Gregor815215d2009-05-27 05:35:12 +00002724 D = Result;
2725 }
2726
Douglas Gregor815215d2009-05-27 05:35:12 +00002727 return D;
2728}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002729
Mike Stump1eb44332009-09-09 15:08:12 +00002730/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002731/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002732void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002733 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00002734 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002735 PendingImplicitInstantiation Inst;
2736
2737 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002738 Inst = PendingInstantiations.front();
2739 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00002740 } else {
2741 Inst = PendingLocalImplicitInstantiations.front();
2742 PendingLocalImplicitInstantiations.pop_front();
2743 }
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Douglas Gregor7caa6822009-07-24 20:34:43 +00002745 // Instantiate function definitions
2746 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00002747 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2748 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00002749 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
2750 TSK_ExplicitInstantiationDefinition;
2751 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
2752 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002753 continue;
2754 }
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Douglas Gregor7caa6822009-07-24 20:34:43 +00002756 // Instantiate static data member definitions.
2757 VarDecl *Var = cast<VarDecl>(Inst.first);
2758 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002759
Chandler Carruth291b4412010-02-13 10:17:50 +00002760 // Don't try to instantiate declarations if the most recent redeclaration
2761 // is invalid.
2762 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2763 continue;
2764
2765 // Check if the most recent declaration has changed the specialization kind
2766 // and removed the need for implicit instantiation.
2767 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2768 case TSK_Undeclared:
2769 assert(false && "Cannot instantitiate an undeclared specialization.");
2770 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00002771 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00002772 continue; // No longer need to instantiate this type.
2773 case TSK_ExplicitInstantiationDefinition:
2774 // We only need an instantiation if the pending instantiation *is* the
2775 // explicit instantiation.
2776 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00002777 case TSK_ImplicitInstantiation:
2778 break;
2779 }
2780
John McCallf312b1e2010-08-26 23:41:50 +00002781 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2782 "instantiating static data member "
2783 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002784
Chandler Carruth58e390e2010-08-25 08:27:02 +00002785 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
2786 TSK_ExplicitInstantiationDefinition;
2787 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
2788 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002789 }
2790}
John McCall0c01d182010-03-24 05:22:00 +00002791
2792void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2793 const MultiLevelTemplateArgumentList &TemplateArgs) {
2794 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2795 E = Pattern->ddiag_end(); I != E; ++I) {
2796 DependentDiagnostic *DD = *I;
2797
2798 switch (DD->getKind()) {
2799 case DependentDiagnostic::Access:
2800 HandleDependentAccessCheck(*DD, TemplateArgs);
2801 break;
2802 }
2803 }
2804}