blob: 26fbe6a114854c4835be5621aef2216d9ce5d342 [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
28namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000029 class TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000030 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000031 Sema &SemaRef;
32 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000033 const MultiLevelTemplateArgumentList &TemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregor8dbc2692009-03-17 21:15:40 +000035 public:
Douglas Gregor8dbc2692009-03-17 21:15:40 +000036 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000037 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000038 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000039
Mike Stump390b4cc2009-05-16 07:39:55 +000040 // FIXME: Once we get closer to completion, replace these manually-written
41 // declarations with automatically-generated ones from
Sean Hunt9a555912010-05-30 07:21:58 +000042 // clang/AST/DeclNodes.inc.
Douglas Gregor4f722be2009-03-25 15:45:12 +000043 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
44 Decl *VisitNamespaceDecl(NamespaceDecl *D);
John McCall3dbd3d52010-02-16 06:53:13 +000045 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000046 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000047 Decl *VisitVarDecl(VarDecl *D);
Abramo Bagnara6206d532010-06-05 05:09:32 +000048 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000049 Decl *VisitFieldDecl(FieldDecl *D);
50 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
51 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000052 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000053 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000054 Decl *VisitFunctionDecl(FunctionDecl *D,
55 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000056 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000057 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
58 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000059 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000060 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000061 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000062 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000063 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000064 Decl *VisitClassTemplatePartialSpecializationDecl(
65 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000066 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000067 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000068 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000069 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000070 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000071 Decl *VisitUsingDecl(UsingDecl *D);
72 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000073 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
74 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor8dbc2692009-03-17 21:15:40 +000076 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000077 Decl *VisitDecl(Decl *D) {
78 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
79 Diagnostic::Error,
80 "cannot instantiate %0 yet");
81 SemaRef.Diag(D->getLocation(), DiagID)
82 << D->getDeclKindName();
83
Douglas Gregor8dbc2692009-03-17 21:15:40 +000084 return 0;
85 }
Douglas Gregor5545e162009-03-24 00:38:23 +000086
87 // Helper functions for instantiating methods.
John McCall21ef0fa2010-03-11 09:03:00 +000088 TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000089 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000090 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000091 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000092
93 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000094 SubstTemplateParams(TemplateParameterList *List);
John McCallb6217662010-03-15 10:12:16 +000095
96 bool SubstQualifier(const DeclaratorDecl *OldDecl,
97 DeclaratorDecl *NewDecl);
98 bool SubstQualifier(const TagDecl *OldDecl,
99 TagDecl *NewDecl);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000100
101 bool InstantiateClassTemplatePartialSpecialization(
102 ClassTemplateDecl *ClassTemplate,
103 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000104 };
105}
106
John McCallb6217662010-03-15 10:12:16 +0000107bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
108 DeclaratorDecl *NewDecl) {
109 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
110 if (!OldQual) return false;
111
112 SourceRange QualRange = OldDecl->getQualifierRange();
113
114 NestedNameSpecifier *NewQual
115 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
116 if (!NewQual)
117 return true;
118
119 NewDecl->setQualifierInfo(NewQual, QualRange);
120 return false;
121}
122
123bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
124 TagDecl *NewDecl) {
125 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
126 if (!OldQual) return false;
127
128 SourceRange QualRange = OldDecl->getQualifierRange();
129
130 NestedNameSpecifier *NewQual
131 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
132 if (!NewQual)
133 return true;
134
135 NewDecl->setQualifierInfo(NewQual, QualRange);
136 return false;
137}
138
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000139// FIXME: Is this still too simple?
John McCall1d8d1cc2010-08-01 02:01:53 +0000140void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
141 Decl *Tmpl, Decl *New) {
Sean Huntcf807c42010-08-18 23:23:40 +0000142 for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
143 i != e; ++i) {
144 const Attr *TmplAttr = *i;
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000145 // FIXME: This should be generalized to more than just the AlignedAttr.
146 if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000147 if (Aligned->isAlignmentDependent()) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000148 // The alignment expression is not potentially evaluated.
John McCall1d8d1cc2010-08-01 02:01:53 +0000149 EnterExpressionEvaluationContext Unevaluated(*this,
John McCallf312b1e2010-08-26 23:41:50 +0000150 Sema::Unevaluated);
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000151
Sean Huntcf807c42010-08-18 23:23:40 +0000152 if (Aligned->isAlignmentExpr()) {
John McCall60d7b3a2010-08-24 06:29:42 +0000153 ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
Sean Huntcf807c42010-08-18 23:23:40 +0000154 TemplateArgs);
155 if (!Result.isInvalid())
156 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
157 }
158 else {
159 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
160 TemplateArgs,
161 Aligned->getLocation(),
162 DeclarationName());
163 if (Result)
164 AddAlignedAttr(Aligned->getLocation(), New, Result);
165 }
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000166 continue;
167 }
168 }
169
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000170 // FIXME: Is cloning correct for all attributes?
John McCall1d8d1cc2010-08-01 02:01:53 +0000171 Attr *NewAttr = TmplAttr->clone(Context);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000172 New->addAttr(NewAttr);
173 }
174}
175
Douglas Gregor4f722be2009-03-25 15:45:12 +0000176Decl *
177TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
178 assert(false && "Translation units cannot be instantiated");
179 return D;
180}
181
182Decl *
183TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
184 assert(false && "Namespaces cannot be instantiated");
185 return D;
186}
187
John McCall3dbd3d52010-02-16 06:53:13 +0000188Decl *
189TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
190 NamespaceAliasDecl *Inst
191 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
192 D->getNamespaceLoc(),
193 D->getAliasLoc(),
194 D->getNamespace()->getIdentifier(),
195 D->getQualifierRange(),
196 D->getQualifier(),
197 D->getTargetNameLoc(),
198 D->getNamespace());
199 Owner->addDecl(Inst);
200 return Inst;
201}
202
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000203Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
204 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000205 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000206 if (DI->getType()->isDependentType() ||
207 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000208 DI = SemaRef.SubstType(DI, TemplateArgs,
209 D->getLocation(), D->getDeclName());
210 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000211 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000212 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000213 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000214 } else {
215 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000216 }
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000218 // Create the new typedef
219 TypedefDecl *Typedef
220 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000221 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000222 if (Invalid)
223 Typedef->setInvalidDecl();
224
Douglas Gregord57a38e2010-04-23 16:25:07 +0000225 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
226 TagDecl *TD = TT->getDecl();
227
228 // If the TagDecl that the TypedefDecl points to is an anonymous decl
229 // keep track of the TypedefDecl.
230 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
231 TD->setTypedefForAnonDecl(Typedef);
232 }
233
John McCall5126fd02009-12-30 00:31:22 +0000234 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000235 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
236 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000237 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
238 }
239
John McCall1d8d1cc2010-08-01 02:01:53 +0000240 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000241
John McCall46460a62010-01-20 21:53:11 +0000242 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000243 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000245 return Typedef;
246}
247
Douglas Gregor6eef5192009-12-14 19:27:10 +0000248/// \brief Instantiate the arguments provided as part of initialization.
249///
250/// \returns true if an error occurred, false otherwise.
251static bool InstantiateInitializationArguments(Sema &SemaRef,
252 Expr **Args, unsigned NumArgs,
253 const MultiLevelTemplateArgumentList &TemplateArgs,
254 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
John McCallca0408f2010-08-23 06:44:23 +0000255 ASTOwningVector<Expr*> &InitArgs) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000256 for (unsigned I = 0; I != NumArgs; ++I) {
257 // When we hit the first defaulted argument, break out of the loop:
258 // we don't pass those default arguments on.
259 if (Args[I]->isDefaultArgument())
260 break;
261
John McCall60d7b3a2010-08-24 06:29:42 +0000262 ExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000263 if (Arg.isInvalid())
264 return true;
265
266 Expr *ArgExpr = (Expr *)Arg.get();
267 InitArgs.push_back(Arg.release());
268
269 // FIXME: We're faking all of the comma locations. Do we need them?
270 FakeCommaLocs.push_back(
271 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
272 }
273
274 return false;
275}
276
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000277/// \brief Instantiate an initializer, breaking it into separate
278/// initialization arguments.
279///
280/// \param S The semantic analysis object.
281///
282/// \param Init The initializer to instantiate.
283///
284/// \param TemplateArgs Template arguments to be substituted into the
285/// initializer.
286///
287/// \param NewArgs Will be filled in with the instantiation arguments.
288///
289/// \returns true if an error occurred, false otherwise
290static bool InstantiateInitializer(Sema &S, Expr *Init,
291 const MultiLevelTemplateArgumentList &TemplateArgs,
292 SourceLocation &LParenLoc,
293 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
John McCallca0408f2010-08-23 06:44:23 +0000294 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000295 SourceLocation &RParenLoc) {
296 NewArgs.clear();
297 LParenLoc = SourceLocation();
298 RParenLoc = SourceLocation();
299
300 if (!Init)
301 return false;
302
303 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
304 Init = ExprTemp->getSubExpr();
305
306 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
307 Init = Binder->getSubExpr();
308
309 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
310 Init = ICE->getSubExprAsWritten();
311
312 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
313 LParenLoc = ParenList->getLParenLoc();
314 RParenLoc = ParenList->getRParenLoc();
315 return InstantiateInitializationArguments(S, ParenList->getExprs(),
316 ParenList->getNumExprs(),
317 TemplateArgs, CommaLocs,
318 NewArgs);
319 }
320
321 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000322 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
323 if (InstantiateInitializationArguments(S,
324 Construct->getArgs(),
325 Construct->getNumArgs(),
326 TemplateArgs,
327 CommaLocs, NewArgs))
328 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000329
Douglas Gregor28329e52010-03-24 21:22:47 +0000330 // FIXME: Fake locations!
331 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
332 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
333 return false;
334 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000335 }
336
John McCall60d7b3a2010-08-24 06:29:42 +0000337 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000338 if (Result.isInvalid())
339 return true;
340
341 NewArgs.push_back(Result.takeAs<Expr>());
342 return false;
343}
344
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000345Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000346 // If this is the variable for an anonymous struct or union,
347 // instantiate the anonymous struct/union type first.
348 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
349 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
350 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
351 return 0;
352
John McCallce3ff2b2009-08-25 22:02:44 +0000353 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000354 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000355 TemplateArgs,
356 D->getTypeSpecStartLoc(),
357 D->getDeclName());
358 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000359 return 0;
360
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000361 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000362 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
363 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000364 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000365 D->getStorageClass(),
366 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000367 Var->setThreadSpecified(D->isThreadSpecified());
368 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000369
John McCallb6217662010-03-15 10:12:16 +0000370 // Substitute the nested name specifier, if any.
371 if (SubstQualifier(D, Var))
372 return 0;
373
Mike Stump1eb44332009-09-09 15:08:12 +0000374 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000375 // out-of-line, the instantiation will have the same lexical
376 // context (which will be a namespace scope) as the template.
377 if (D->isOutOfLine())
378 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000379
John McCall46460a62010-01-20 21:53:11 +0000380 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000381
382 if (!D->isStaticDataMember())
383 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000384
Mike Stump390b4cc2009-05-16 07:39:55 +0000385 // FIXME: In theory, we could have a previous declaration for variables that
386 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000387 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000388 // FIXME: having to fake up a LookupResult is dumb.
389 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000390 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000391 if (D->isStaticDataMember())
392 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000393 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Douglas Gregor7caa6822009-07-24 20:34:43 +0000395 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000396 if (!D->isStaticDataMember())
397 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000398 Owner->makeDeclVisibleInContext(Var);
399 } else {
400 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000401 if (Owner->isFunctionOrMethod())
402 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000403 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000404 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000405
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000406 // Link instantiations of static data members back to the template from
407 // which they were instantiated.
408 if (Var->isStaticDataMember())
409 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000410 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000411
Douglas Gregor60c93c92010-02-09 07:26:29 +0000412 if (Var->getAnyInitializer()) {
413 // We already have an initializer in the class.
414 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000415 if (Var->isStaticDataMember() && !D->isOutOfLine())
416 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
417 else
418 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
419
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000420 // Instantiate the initializer.
421 SourceLocation LParenLoc, RParenLoc;
422 llvm::SmallVector<SourceLocation, 4> CommaLocs;
John McCallca0408f2010-08-23 06:44:23 +0000423 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000424 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
425 CommaLocs, InitArgs, RParenLoc)) {
426 // Attach the initializer to the declaration.
427 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000428 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000429 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000430 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000431 move_arg(InitArgs),
432 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000433 RParenLoc);
434 } else if (InitArgs.size() == 1) {
John McCall9ae2f072010-08-23 23:25:46 +0000435 Expr *Init = InitArgs.take()[0];
436 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000437 } else {
438 assert(InitArgs.size() == 0);
John McCalld226f652010-08-21 09:40:31 +0000439 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000440 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000441 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000442 // FIXME: Not too happy about invalidating the declaration
443 // because of a bogus initializer.
444 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000445 }
446
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000447 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000448 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000449 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000450
Douglas Gregor5764f612010-05-08 23:05:03 +0000451 // Diagnose unused local variables.
452 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
453 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000454
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000455 return Var;
456}
457
Abramo Bagnara6206d532010-06-05 05:09:32 +0000458Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
459 AccessSpecDecl* AD
460 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
461 D->getAccessSpecifierLoc(), D->getColonLoc());
462 Owner->addHiddenDecl(AD);
463 return AD;
464}
465
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000466Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
467 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000468 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000469 if (DI->getType()->isDependentType() ||
470 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000471 DI = SemaRef.SubstType(DI, TemplateArgs,
472 D->getLocation(), D->getDeclName());
473 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000474 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000475 Invalid = true;
476 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000477 // C++ [temp.arg.type]p3:
478 // If a declaration acquires a function type through a type
479 // dependent on a template-parameter and this causes a
480 // declaration that does not use the syntactic form of a
481 // function declarator to have function type, the program is
482 // ill-formed.
483 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000484 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000485 Invalid = true;
486 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000487 } else {
488 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000489 }
490
491 Expr *BitWidth = D->getBitWidth();
492 if (Invalid)
493 BitWidth = 0;
494 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000495 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000496 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
John McCall60d7b3a2010-08-24 06:29:42 +0000498 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000499 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000500 if (InstantiatedBitWidth.isInvalid()) {
501 Invalid = true;
502 BitWidth = 0;
503 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000504 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000505 }
506
John McCall07fb6be2009-10-22 23:33:21 +0000507 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
508 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000509 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000510 D->getLocation(),
511 D->isMutable(),
512 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000513 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000514 D->getAccess(),
515 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000516 if (!Field) {
517 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000518 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
John McCall1d8d1cc2010-08-01 02:01:53 +0000521 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000522
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000523 if (Invalid)
524 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000526 if (!Field->getDeclName()) {
527 // Keep track of where this decl came from.
528 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000529 }
530 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
531 if (Parent->isAnonymousStructOrUnion() &&
532 Parent->getLookupContext()->isFunctionOrMethod())
533 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000536 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000537 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000538 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000539
540 return Field;
541}
542
John McCall02cace72009-08-28 07:59:38 +0000543Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000544 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000545 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000546 if (TypeSourceInfo *Ty = D->getFriendType()) {
547 TypeSourceInfo *InstTy =
548 SemaRef.SubstType(Ty, TemplateArgs,
549 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000550 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000551 return 0;
John McCall02cace72009-08-28 07:59:38 +0000552
Douglas Gregor06245bf2010-04-07 17:57:12 +0000553 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
554 if (!FD)
555 return 0;
556
557 FD->setAccess(AS_public);
558 Owner->addDecl(FD);
559 return FD;
560 }
561
562 NamedDecl *ND = D->getFriendDecl();
563 assert(ND && "friend decl must be a decl or a type!");
564
John McCallaf2094e2010-04-08 09:05:18 +0000565 // All of the Visit implementations for the various potential friend
566 // declarations have to be carefully written to work for friend
567 // objects, with the most important detail being that the target
568 // decl should almost certainly not be placed in Owner.
569 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000570 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000571
John McCall02cace72009-08-28 07:59:38 +0000572 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000573 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
574 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000575 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000576 Owner->addDecl(FD);
577 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000578}
579
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000580Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
581 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Douglas Gregorac7610d2009-06-22 20:57:11 +0000583 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000584 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
John McCall60d7b3a2010-08-24 06:29:42 +0000586 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000587 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000588 if (InstantiatedAssertExpr.isInvalid())
589 return 0;
590
John McCall60d7b3a2010-08-24 06:29:42 +0000591 ExprResult Message(D->getMessage());
Douglas Gregor43d9d922009-08-08 01:41:12 +0000592 D->getMessage()->Retain();
John McCalld226f652010-08-21 09:40:31 +0000593 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000594 InstantiatedAssertExpr.get(),
595 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000596}
597
598Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000599 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000600 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000601 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000602 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000603 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000604 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000605 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000606 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000607 Enum->startDefinition();
608
Douglas Gregor96084f12010-03-01 19:00:07 +0000609 if (D->getDeclContext()->isFunctionOrMethod())
610 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
611
John McCalld226f652010-08-21 09:40:31 +0000612 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000613
614 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000615 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
616 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000617 EC != ECEnd; ++EC) {
618 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000619 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000620 if (Expr *UninstValue = EC->getInitExpr()) {
621 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000622 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000623 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
John McCallce3ff2b2009-08-25 22:02:44 +0000625 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000626 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000627
628 // Drop the initial value and continue.
629 bool isInvalid = false;
630 if (Value.isInvalid()) {
631 Value = SemaRef.Owned((Expr *)0);
632 isInvalid = true;
633 }
634
Mike Stump1eb44332009-09-09 15:08:12 +0000635 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000636 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
637 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000638 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000639
640 if (isInvalid) {
641 if (EnumConst)
642 EnumConst->setInvalidDecl();
643 Enum->setInvalidDecl();
644 }
645
646 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000647 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000648 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000649 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000650 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000651
652 if (D->getDeclContext()->isFunctionOrMethod()) {
653 // If the enumeration is within a function or method, record the enum
654 // constant as a local.
655 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
656 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000657 }
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000660 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000661 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000662 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000663 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000664 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000665 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000666
667 return Enum;
668}
669
Douglas Gregor6477b692009-03-25 15:04:13 +0000670Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
671 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
672 return 0;
673}
674
John McCalle29ba202009-08-20 01:44:21 +0000675Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000676 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
677
Douglas Gregor550d9b22009-10-31 17:21:17 +0000678 // Create a local instantiation scope for this class template, which
679 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000680 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000681 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000682 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000683 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000684 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000685
686 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000687
688 // Instantiate the qualifier. We have to do this first in case
689 // we're a friend declaration, because if we are then we need to put
690 // the new declaration in the appropriate context.
691 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
692 if (Qualifier) {
693 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
694 Pattern->getQualifierRange(),
695 TemplateArgs);
696 if (!Qualifier) return 0;
697 }
698
699 CXXRecordDecl *PrevDecl = 0;
700 ClassTemplateDecl *PrevClassTemplate = 0;
701
702 // If this isn't a friend, then it's a member template, in which
703 // case we just want to build the instantiation in the
704 // specialization. If it is a friend, we want to build it in
705 // the appropriate context.
706 DeclContext *DC = Owner;
707 if (isFriend) {
708 if (Qualifier) {
709 CXXScopeSpec SS;
710 SS.setScopeRep(Qualifier);
711 SS.setRange(Pattern->getQualifierRange());
712 DC = SemaRef.computeDeclContext(SS);
713 if (!DC) return 0;
714 } else {
715 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
716 Pattern->getDeclContext(),
717 TemplateArgs);
718 }
719
720 // Look for a previous declaration of the template in the owning
721 // context.
722 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
723 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
724 SemaRef.LookupQualifiedName(R, DC);
725
726 if (R.isSingleResult()) {
727 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
728 if (PrevClassTemplate)
729 PrevDecl = PrevClassTemplate->getTemplatedDecl();
730 }
731
732 if (!PrevClassTemplate && Qualifier) {
733 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000734 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
735 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000736 return 0;
737 }
738
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000739 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000740 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000741 bool Complain = true;
742
743 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
744 // template for struct std::tr1::__detail::_Map_base, where the
745 // template parameters of the friend declaration don't match the
746 // template parameters of the original declaration. In this one
747 // case, we don't complain about the ill-formed friend
748 // declaration.
749 if (isFriend && Pattern->getIdentifier() &&
750 Pattern->getIdentifier()->isStr("_Map_base") &&
751 DC->isNamespace() &&
752 cast<NamespaceDecl>(DC)->getIdentifier() &&
753 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
754 DeclContext *DCParent = DC->getParent();
755 if (DCParent->isNamespace() &&
756 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
757 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
758 DeclContext *DCParent2 = DCParent->getParent();
759 if (DCParent2->isNamespace() &&
760 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
761 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
762 DCParent2->getParent()->isTranslationUnit())
763 Complain = false;
764 }
765 }
766
John McCall93ba8572010-03-25 06:39:04 +0000767 TemplateParameterList *PrevParams
768 = PrevClassTemplate->getTemplateParameters();
769
770 // Make sure the parameter lists match.
771 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000772 Complain,
773 Sema::TPL_TemplateMatch)) {
774 if (Complain)
775 return 0;
776
777 AdoptedPreviousTemplateParams = true;
778 InstParams = PrevParams;
779 }
John McCall93ba8572010-03-25 06:39:04 +0000780
781 // Do some additional validation, then merge default arguments
782 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000783 if (!AdoptedPreviousTemplateParams &&
784 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000785 Sema::TPC_ClassTemplate))
786 return 0;
787 }
788 }
789
John McCalle29ba202009-08-20 01:44:21 +0000790 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000791 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000792 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000793 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000794 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000795
John McCall93ba8572010-03-25 06:39:04 +0000796 if (Qualifier)
797 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000798
John McCalle29ba202009-08-20 01:44:21 +0000799 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000800 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
801 D->getIdentifier(), InstParams, RecordInst,
802 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000803 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000804
John McCall93ba8572010-03-25 06:39:04 +0000805 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000806 if (PrevClassTemplate)
807 Inst->setAccess(PrevClassTemplate->getAccess());
808 else
809 Inst->setAccess(D->getAccess());
810
John McCall93ba8572010-03-25 06:39:04 +0000811 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
812 // TODO: do we want to track the instantiation progeny of this
813 // friend target decl?
814 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000815 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000816 Inst->setInstantiatedFromMemberTemplate(D);
817 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000818
819 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000820 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000821 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000822
Douglas Gregor259571e2009-10-30 22:42:42 +0000823 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000824 if (isFriend) {
825 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000826 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000827 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000828
John McCalle29ba202009-08-20 01:44:21 +0000829 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000830
Douglas Gregored9c0f92009-10-29 00:04:11 +0000831 // Instantiate all of the partial specializations of this member class
832 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000833 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
834 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000835 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
836 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
837
John McCalle29ba202009-08-20 01:44:21 +0000838 return Inst;
839}
840
Douglas Gregord60e1052009-08-27 16:57:43 +0000841Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000842TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
843 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000844 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
845
846 // Lookup the already-instantiated declaration in the instantiation
847 // of the class template and return that.
848 DeclContext::lookup_result Found
849 = Owner->lookup(ClassTemplate->getDeclName());
850 if (Found.first == Found.second)
851 return 0;
852
853 ClassTemplateDecl *InstClassTemplate
854 = dyn_cast<ClassTemplateDecl>(*Found.first);
855 if (!InstClassTemplate)
856 return 0;
857
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000858 return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000859}
860
861Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000862TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000863 // Create a local instantiation scope for this function template, which
864 // will contain the instantiations of the template parameters and then get
865 // merged with the local instantiation scope for the function template
866 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000867 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000868
Douglas Gregord60e1052009-08-27 16:57:43 +0000869 TemplateParameterList *TempParams = D->getTemplateParameters();
870 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000871 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000872 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000873
Douglas Gregora735b202009-10-13 14:39:41 +0000874 FunctionDecl *Instantiated = 0;
875 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
876 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
877 InstParams));
878 else
879 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
880 D->getTemplatedDecl(),
881 InstParams));
882
883 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000884 return 0;
885
John McCall46460a62010-01-20 21:53:11 +0000886 Instantiated->setAccess(D->getAccess());
887
Mike Stump1eb44332009-09-09 15:08:12 +0000888 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000889 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000890 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000891 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000892 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000893 assert(InstTemplate &&
894 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000895
John McCallb1a56e72010-03-26 23:10:15 +0000896 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
897
John McCalle976ffe2009-12-14 23:19:40 +0000898 // Link the instantiation back to the pattern *unless* this is a
899 // non-definition friend declaration.
900 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000901 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000902 InstTemplate->setInstantiatedFromMemberTemplate(D);
903
John McCallb1a56e72010-03-26 23:10:15 +0000904 // Make declarations visible in the appropriate context.
905 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000906 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000907
Douglas Gregord60e1052009-08-27 16:57:43 +0000908 return InstTemplate;
909}
910
Douglas Gregord475b8d2009-03-25 21:17:03 +0000911Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
912 CXXRecordDecl *PrevDecl = 0;
913 if (D->isInjectedClassName())
914 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000915 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000916 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
917 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000918 TemplateArgs);
919 if (!Prev) return 0;
920 PrevDecl = cast<CXXRecordDecl>(Prev);
921 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000922
923 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000924 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000925 D->getLocation(), D->getIdentifier(),
926 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000927
928 // Substitute the nested name specifier, if any.
929 if (SubstQualifier(D, Record))
930 return 0;
931
Douglas Gregord475b8d2009-03-25 21:17:03 +0000932 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000933 // FIXME: Check against AS_none is an ugly hack to work around the issue that
934 // the tag decls introduced by friend class declarations don't have an access
935 // specifier. Remove once this area of the code gets sorted out.
936 if (D->getAccess() != AS_none)
937 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000938 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000939 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000940
John McCall02cace72009-08-28 07:59:38 +0000941 // If the original function was part of a friend declaration,
942 // inherit its namespace state.
943 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
944 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
945
Douglas Gregor9901c572010-05-21 00:31:19 +0000946 // Make sure that anonymous structs and unions are recorded.
947 if (D->isAnonymousStructOrUnion()) {
948 Record->setAnonymousStructOrUnion(true);
949 if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
950 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
951 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000952
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000953 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000954 return Record;
955}
956
John McCall02cace72009-08-28 07:59:38 +0000957/// Normal class members are of more specific types and therefore
958/// don't make it here. This function serves two purposes:
959/// 1) instantiating function templates
960/// 2) substituting friend declarations
961/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000962Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000963 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000964 // Check whether there is already a function template specialization for
965 // this declaration.
966 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
967 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000968 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000969 std::pair<const TemplateArgument *, unsigned> Innermost
970 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000972 FunctionDecl *SpecFunc
973 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
974 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor127102b2009-06-29 20:59:39 +0000976 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000977 if (SpecFunc)
978 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
John McCallb0cb0222010-03-27 05:57:59 +0000981 bool isFriend;
982 if (FunctionTemplate)
983 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
984 else
985 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
986
Douglas Gregor79c22782010-01-16 20:21:20 +0000987 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000988 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000989 !(isa<Decl>(Owner) &&
990 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000991 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregore53060f2009-06-25 22:08:12 +0000993 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000994 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
995 TInfo = SubstFunctionType(D, Params);
996 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000997 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000998 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000999
John McCalld325daa2010-03-26 04:53:08 +00001000 NestedNameSpecifier *Qualifier = D->getQualifier();
1001 if (Qualifier) {
1002 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1003 D->getQualifierRange(),
1004 TemplateArgs);
1005 if (!Qualifier) return 0;
1006 }
1007
John McCall68b6b872010-02-06 01:50:47 +00001008 // If we're instantiating a local function declaration, put the result
1009 // in the owner; otherwise we need to find the instantiated context.
1010 DeclContext *DC;
1011 if (D->getDeclContext()->isFunctionOrMethod())
1012 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001013 else if (isFriend && Qualifier) {
1014 CXXScopeSpec SS;
1015 SS.setScopeRep(Qualifier);
1016 SS.setRange(D->getQualifierRange());
1017 DC = SemaRef.computeDeclContext(SS);
1018 if (!DC) return 0;
1019 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001020 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1021 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001022 }
John McCall68b6b872010-02-06 01:50:47 +00001023
John McCall02cace72009-08-28 07:59:38 +00001024 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001025 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001026 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001027 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001028 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001029
John McCalld325daa2010-03-26 04:53:08 +00001030 if (Qualifier)
1031 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001032
John McCallb1a56e72010-03-26 23:10:15 +00001033 DeclContext *LexicalDC = Owner;
1034 if (!isFriend && D->isOutOfLine()) {
1035 assert(D->getDeclContext()->isFileContext());
1036 LexicalDC = D->getDeclContext();
1037 }
1038
1039 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Douglas Gregore53060f2009-06-25 22:08:12 +00001041 // Attach the parameters
1042 for (unsigned P = 0; P < Params.size(); ++P)
1043 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001044 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001045
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001046 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001047 if (TemplateParams) {
1048 // Our resulting instantiation is actually a function template, since we
1049 // are substituting only the outer template parameters. For example, given
1050 //
1051 // template<typename T>
1052 // struct X {
1053 // template<typename U> friend void f(T, U);
1054 // };
1055 //
1056 // X<int> x;
1057 //
1058 // We are instantiating the friend function template "f" within X<int>,
1059 // which means substituting int for T, but leaving "f" as a friend function
1060 // template.
1061 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001062 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001063 Function->getLocation(),
1064 Function->getDeclName(),
1065 TemplateParams, Function);
1066 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001067
1068 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001069
1070 if (isFriend && D->isThisDeclarationADefinition()) {
1071 // TODO: should we remember this connection regardless of whether
1072 // the friend declaration provided a body?
1073 FunctionTemplate->setInstantiatedFromMemberTemplate(
1074 D->getDescribedFunctionTemplate());
1075 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001076 } else if (FunctionTemplate) {
1077 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001078 std::pair<const TemplateArgument *, unsigned> Innermost
1079 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001080 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001081 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1082 Innermost.first,
1083 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001084 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001085 } else if (isFriend && D->isThisDeclarationADefinition()) {
1086 // TODO: should we remember this connection regardless of whether
1087 // the friend declaration provided a body?
1088 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001089 }
Douglas Gregora735b202009-10-13 14:39:41 +00001090
Douglas Gregore53060f2009-06-25 22:08:12 +00001091 if (InitFunctionInstantiation(Function, D))
1092 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregore53060f2009-06-25 22:08:12 +00001094 bool Redeclaration = false;
1095 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001096 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001097
John McCall68263142009-11-18 22:49:29 +00001098 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1099 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1100
John McCallaf2094e2010-04-08 09:05:18 +00001101 if (DependentFunctionTemplateSpecializationInfo *Info
1102 = D->getDependentSpecializationInfo()) {
1103 assert(isFriend && "non-friend has dependent specialization info?");
1104
1105 // This needs to be set now for future sanity.
1106 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1107
1108 // Instantiate the explicit template arguments.
1109 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1110 Info->getRAngleLoc());
1111 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1112 TemplateArgumentLoc Loc;
1113 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1114 return 0;
1115
1116 ExplicitArgs.addArgument(Loc);
1117 }
1118
1119 // Map the candidate templates to their instantiations.
1120 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1121 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1122 Info->getTemplate(I),
1123 TemplateArgs);
1124 if (!Temp) return 0;
1125
1126 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1127 }
1128
1129 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1130 &ExplicitArgs,
1131 Previous))
1132 Function->setInvalidDecl();
1133
1134 isExplicitSpecialization = true;
1135
1136 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001137 // Look only into the namespace where the friend would be declared to
1138 // find a previous declaration. This is the innermost enclosing namespace,
1139 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001140 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001141
Douglas Gregora735b202009-10-13 14:39:41 +00001142 // In C++, the previous declaration we find might be a tag type
1143 // (class or enum). In this case, the new declaration will hide the
1144 // tag type. Note that this does does not apply if we're declaring a
1145 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001146 if (Previous.isSingleTagDecl())
1147 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001148 }
1149
John McCall9f54ad42009-12-10 09:41:52 +00001150 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001151 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001152 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001153
John McCall76d32642010-04-24 01:30:58 +00001154 NamedDecl *PrincipalDecl = (TemplateParams
1155 ? cast<NamedDecl>(FunctionTemplate)
1156 : Function);
1157
Douglas Gregora735b202009-10-13 14:39:41 +00001158 // If the original function was part of a friend declaration,
1159 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001160 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001161 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001162 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001163 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001164 else
Douglas Gregora735b202009-10-13 14:39:41 +00001165 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001166
1167 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1168 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001169
Gabor Greif77535df2010-08-30 22:25:56 +00001170 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001171
Douglas Gregor238058c2010-05-18 05:45:02 +00001172 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1173 D->isThisDeclarationADefinition()) {
1174 // Check for a function body.
1175 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001176 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001177 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1178 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1179 << Function->getDeclName();
1180 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1181 Function->setInvalidDecl();
1182 }
1183 // Check for redefinitions due to other instantiations of this or
1184 // a similar friend function.
1185 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1186 REnd = Function->redecls_end();
1187 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001188 if (*R == Function)
1189 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001190 switch (R->getFriendObjectKind()) {
1191 case Decl::FOK_None:
1192 if (!queuedInstantiation && R->isUsed(false)) {
1193 if (MemberSpecializationInfo *MSInfo
1194 = Function->getMemberSpecializationInfo()) {
1195 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1196 SourceLocation Loc = R->getLocation(); // FIXME
1197 MSInfo->setPointOfInstantiation(Loc);
1198 SemaRef.PendingLocalImplicitInstantiations.push_back(
1199 std::make_pair(Function, Loc));
1200 queuedInstantiation = true;
1201 }
1202 }
1203 }
1204 break;
1205 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001206 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001207 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001208 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001209 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1210 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001211 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001212 Function->setInvalidDecl();
1213 break;
1214 }
1215 }
1216 }
1217 }
Douglas Gregora735b202009-10-13 14:39:41 +00001218 }
1219
John McCall76d32642010-04-24 01:30:58 +00001220 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1221 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1222 PrincipalDecl->setNonMemberOperator();
1223
Douglas Gregore53060f2009-06-25 22:08:12 +00001224 return Function;
1225}
1226
Douglas Gregord60e1052009-08-27 16:57:43 +00001227Decl *
1228TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1229 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001230 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1231 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001232 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001233 // We are creating a function template specialization from a function
1234 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001235 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001236 std::pair<const TemplateArgument *, unsigned> Innermost
1237 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001239 FunctionDecl *SpecFunc
1240 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1241 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor6b906862009-08-21 00:16:32 +00001243 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001244 if (SpecFunc)
1245 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001246 }
1247
John McCallb0cb0222010-03-27 05:57:59 +00001248 bool isFriend;
1249 if (FunctionTemplate)
1250 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1251 else
1252 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1253
Douglas Gregor79c22782010-01-16 20:21:20 +00001254 bool MergeWithParentScope = (TemplateParams != 0) ||
1255 !(isa<Decl>(Owner) &&
1256 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001257 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001258
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001259 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001260 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1261 TInfo = SubstFunctionType(D, Params);
1262 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001263 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001264 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001265
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001266 // \brief If the type of this function is not *directly* a function
1267 // type, then we're instantiating the a function that was declared
1268 // via a typedef, e.g.,
1269 //
1270 // typedef int functype(int, int);
1271 // functype func;
1272 //
1273 // In this case, we'll just go instantiate the ParmVarDecls that we
1274 // synthesized in the method declaration.
1275 if (!isa<FunctionProtoType>(T)) {
1276 assert(!Params.size() && "Instantiating type could not yield parameters");
1277 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1278 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1279 TemplateArgs);
1280 if (!P)
1281 return 0;
1282
1283 Params.push_back(P);
1284 }
1285 }
1286
John McCallb0cb0222010-03-27 05:57:59 +00001287 NestedNameSpecifier *Qualifier = D->getQualifier();
1288 if (Qualifier) {
1289 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1290 D->getQualifierRange(),
1291 TemplateArgs);
1292 if (!Qualifier) return 0;
1293 }
1294
1295 DeclContext *DC = Owner;
1296 if (isFriend) {
1297 if (Qualifier) {
1298 CXXScopeSpec SS;
1299 SS.setScopeRep(Qualifier);
1300 SS.setRange(D->getQualifierRange());
1301 DC = SemaRef.computeDeclContext(SS);
1302 } else {
1303 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1304 D->getDeclContext(),
1305 TemplateArgs);
1306 }
1307 if (!DC) return 0;
1308 }
1309
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001310 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001311 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001312 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Abramo Bagnara25777432010-08-11 22:01:17 +00001314 DeclarationNameInfo NameInfo
1315 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001316 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001317 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001318 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001319 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001320 Constructor->isInlineSpecified(),
1321 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001322 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001323 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001324 NameInfo, T,
1325 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001326 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001327 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001328 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001329 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001330 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001331 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001332 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001333 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1334 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001335 D->isStatic(),
1336 D->getStorageClassAsWritten(),
1337 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001338 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001339
John McCallb0cb0222010-03-27 05:57:59 +00001340 if (Qualifier)
1341 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001342
Douglas Gregord60e1052009-08-27 16:57:43 +00001343 if (TemplateParams) {
1344 // Our resulting instantiation is actually a function template, since we
1345 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001346 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001347 // template<typename T>
1348 // struct X {
1349 // template<typename U> void f(T, U);
1350 // };
1351 //
1352 // X<int> x;
1353 //
1354 // We are instantiating the member template "f" within X<int>, which means
1355 // substituting int for T, but leaving "f" as a member function template.
1356 // Build the function template itself.
1357 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1358 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001359 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001360 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001361 if (isFriend) {
1362 FunctionTemplate->setLexicalDeclContext(Owner);
1363 FunctionTemplate->setObjectOfFriendDecl(true);
1364 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001365 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001366 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001367 } else if (FunctionTemplate) {
1368 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001369 std::pair<const TemplateArgument *, unsigned> Innermost
1370 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001371 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001372 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1373 Innermost.first,
1374 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001375 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001376 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001377 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001378 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001379 }
1380
Mike Stump1eb44332009-09-09 15:08:12 +00001381 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001382 // out-of-line, the instantiation will have the same lexical
1383 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001384 if (isFriend) {
1385 Method->setLexicalDeclContext(Owner);
1386 Method->setObjectOfFriendDecl(true);
1387 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001388 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Douglas Gregor5545e162009-03-24 00:38:23 +00001390 // Attach the parameters
1391 for (unsigned P = 0; P < Params.size(); ++P)
1392 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001393 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001394
1395 if (InitMethodInstantiation(Method, D))
1396 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001397
Abramo Bagnara25777432010-08-11 22:01:17 +00001398 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1399 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001400
John McCallb0cb0222010-03-27 05:57:59 +00001401 if (!FunctionTemplate || TemplateParams || isFriend) {
1402 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregordec06662009-08-21 18:42:58 +00001404 // In C++, the previous declaration we find might be a tag type
1405 // (class or enum). In this case, the new declaration will hide the
1406 // tag type. Note that this does does not apply if we're declaring a
1407 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001408 if (Previous.isSingleTagDecl())
1409 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001410 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001411
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001412 bool Redeclaration = false;
1413 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001414 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001415 /*FIXME:*/OverloadableAttrRequired);
1416
Douglas Gregor4ba31362009-12-01 17:24:26 +00001417 if (D->isPure())
1418 SemaRef.CheckPureMethod(Method, SourceRange());
1419
John McCall46460a62010-01-20 21:53:11 +00001420 Method->setAccess(D->getAccess());
1421
John McCallb0cb0222010-03-27 05:57:59 +00001422 if (FunctionTemplate) {
1423 // If there's a function template, let our caller handle it.
1424 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1425 // Don't hide a (potentially) valid declaration with an invalid one.
1426 } else {
1427 NamedDecl *DeclToAdd = (TemplateParams
1428 ? cast<NamedDecl>(FunctionTemplate)
1429 : Method);
1430 if (isFriend)
1431 Record->makeDeclVisibleInContext(DeclToAdd);
1432 else
1433 Owner->addDecl(DeclToAdd);
1434 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001435
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001436 return Method;
1437}
1438
Douglas Gregor615c5d42009-03-24 16:43:20 +00001439Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001440 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001441}
1442
Douglas Gregor03b2b072009-03-24 00:15:49 +00001443Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001444 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001445}
1446
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001447Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001448 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001449}
1450
Douglas Gregor6477b692009-03-25 15:04:13 +00001451ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001452 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001453}
1454
John McCalle29ba202009-08-20 01:44:21 +00001455Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1456 TemplateTypeParmDecl *D) {
1457 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001458 const Type* T = D->getTypeForDecl();
1459 assert(T->isTemplateTypeParmType());
1460 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001461
John McCalle29ba202009-08-20 01:44:21 +00001462 TemplateTypeParmDecl *Inst =
1463 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregorefed5c82010-06-16 15:23:05 +00001464 TTPT->getDepth() - 1, TTPT->getIndex(),
1465 TTPT->getName(),
John McCalle29ba202009-08-20 01:44:21 +00001466 D->wasDeclaredWithTypename(),
1467 D->isParameterPack());
1468
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001469 if (D->hasDefaultArgument())
1470 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001471
Douglas Gregor550d9b22009-10-31 17:21:17 +00001472 // Introduce this template parameter's instantiation into the instantiation
1473 // scope.
1474 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1475
John McCalle29ba202009-08-20 01:44:21 +00001476 return Inst;
1477}
1478
Douglas Gregor33642df2009-10-23 23:25:44 +00001479Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1480 NonTypeTemplateParmDecl *D) {
1481 // Substitute into the type of the non-type template parameter.
1482 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001483 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001484 if (DI) {
1485 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1486 D->getDeclName());
1487 if (DI) T = DI->getType();
1488 } else {
1489 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1490 D->getDeclName());
1491 DI = 0;
1492 }
1493 if (T.isNull())
1494 return 0;
1495
1496 // Check that this type is acceptable for a non-type template parameter.
1497 bool Invalid = false;
1498 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1499 if (T.isNull()) {
1500 T = SemaRef.Context.IntTy;
1501 Invalid = true;
1502 }
1503
1504 NonTypeTemplateParmDecl *Param
1505 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1506 D->getDepth() - 1, D->getPosition(),
1507 D->getIdentifier(), T, DI);
1508 if (Invalid)
1509 Param->setInvalidDecl();
1510
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001511 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001512
1513 // Introduce this template parameter's instantiation into the instantiation
1514 // scope.
1515 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001516 return Param;
1517}
1518
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001519Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001520TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1521 TemplateTemplateParmDecl *D) {
1522 // Instantiate the template parameter list of the template template parameter.
1523 TemplateParameterList *TempParams = D->getTemplateParameters();
1524 TemplateParameterList *InstParams;
1525 {
1526 // Perform the actual substitution of template parameters within a new,
1527 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001528 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001529 InstParams = SubstTemplateParams(TempParams);
1530 if (!InstParams)
1531 return NULL;
1532 }
1533
1534 // Build the template template parameter.
1535 TemplateTemplateParmDecl *Param
1536 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1537 D->getDepth() - 1, D->getPosition(),
1538 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001539 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001540
Douglas Gregor9106ef72009-11-11 16:58:32 +00001541 // Introduce this template parameter's instantiation into the instantiation
1542 // scope.
1543 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1544
1545 return Param;
1546}
1547
Douglas Gregor48c32a72009-11-17 06:07:40 +00001548Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1549 // Using directives are never dependent, so they require no explicit
1550
1551 UsingDirectiveDecl *Inst
1552 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1553 D->getNamespaceKeyLocation(),
1554 D->getQualifierRange(), D->getQualifier(),
1555 D->getIdentLocation(),
1556 D->getNominatedNamespace(),
1557 D->getCommonAncestor());
1558 Owner->addDecl(Inst);
1559 return Inst;
1560}
1561
John McCalled976492009-12-04 22:46:56 +00001562Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1563 // The nested name specifier is non-dependent, so no transformation
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001564 // is required. The same holds for the name info.
1565 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001566
John McCall9f54ad42009-12-10 09:41:52 +00001567 // We only need to do redeclaration lookups if we're in a class
1568 // scope (in fact, it's not really even possible in non-class
1569 // scopes).
1570 bool CheckRedeclaration = Owner->isRecord();
1571
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001572 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1573 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001574
John McCalled976492009-12-04 22:46:56 +00001575 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001576 D->getNestedNameRange(),
1577 D->getUsingLocation(),
1578 D->getTargetNestedNameDecl(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001579 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001580 D->isTypeName());
1581
1582 CXXScopeSpec SS;
1583 SS.setScopeRep(D->getTargetNestedNameDecl());
1584 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001585
1586 if (CheckRedeclaration) {
1587 Prev.setHideTags(false);
1588 SemaRef.LookupQualifiedName(Prev, Owner);
1589
1590 // Check for invalid redeclarations.
1591 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1592 D->isTypeName(), SS,
1593 D->getLocation(), Prev))
1594 NewUD->setInvalidDecl();
1595
1596 }
1597
1598 if (!NewUD->isInvalidDecl() &&
1599 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001600 D->getLocation()))
1601 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001602
John McCalled976492009-12-04 22:46:56 +00001603 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1604 NewUD->setAccess(D->getAccess());
1605 Owner->addDecl(NewUD);
1606
John McCall9f54ad42009-12-10 09:41:52 +00001607 // Don't process the shadow decls for an invalid decl.
1608 if (NewUD->isInvalidDecl())
1609 return NewUD;
1610
John McCall323c3102009-12-22 22:26:37 +00001611 bool isFunctionScope = Owner->isFunctionOrMethod();
1612
John McCall9f54ad42009-12-10 09:41:52 +00001613 // Process the shadow decls.
1614 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1615 I != E; ++I) {
1616 UsingShadowDecl *Shadow = *I;
1617 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001618 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1619 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001620 TemplateArgs));
1621
1622 if (CheckRedeclaration &&
1623 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1624 continue;
1625
1626 UsingShadowDecl *InstShadow
1627 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1628 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001629
1630 if (isFunctionScope)
1631 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001632 }
John McCalled976492009-12-04 22:46:56 +00001633
1634 return NewUD;
1635}
1636
1637Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001638 // Ignore these; we handle them in bulk when processing the UsingDecl.
1639 return 0;
John McCalled976492009-12-04 22:46:56 +00001640}
1641
John McCall7ba107a2009-11-18 02:36:19 +00001642Decl * TemplateDeclInstantiator
1643 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001644 NestedNameSpecifier *NNS =
1645 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1646 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001647 TemplateArgs);
1648 if (!NNS)
1649 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001651 CXXScopeSpec SS;
1652 SS.setRange(D->getTargetNestedNameRange());
1653 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001655 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1656 // Hence, no tranformation is required for it.
1657 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001658 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001659 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001660 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001661 /*instantiation*/ true,
1662 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001663 if (UD)
John McCalled976492009-12-04 22:46:56 +00001664 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1665
John McCall7ba107a2009-11-18 02:36:19 +00001666 return UD;
1667}
1668
1669Decl * TemplateDeclInstantiator
1670 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1671 NestedNameSpecifier *NNS =
1672 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1673 D->getTargetNestedNameRange(),
1674 TemplateArgs);
1675 if (!NNS)
1676 return 0;
1677
1678 CXXScopeSpec SS;
1679 SS.setRange(D->getTargetNestedNameRange());
1680 SS.setScopeRep(NNS);
1681
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001682 DeclarationNameInfo NameInfo
1683 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1684
John McCall7ba107a2009-11-18 02:36:19 +00001685 NamedDecl *UD =
1686 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001687 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001688 /*instantiation*/ true,
1689 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001690 if (UD)
John McCalled976492009-12-04 22:46:56 +00001691 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1692
Anders Carlsson0d8df782009-08-29 19:37:28 +00001693 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001694}
1695
John McCallce3ff2b2009-08-25 22:02:44 +00001696Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001697 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001698 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001699 if (D->isInvalidDecl())
1700 return 0;
1701
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001702 return Instantiator.Visit(D);
1703}
1704
John McCalle29ba202009-08-20 01:44:21 +00001705/// \brief Instantiates a nested template parameter list in the current
1706/// instantiation context.
1707///
1708/// \param L The parameter list to instantiate
1709///
1710/// \returns NULL if there was an error
1711TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001712TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001713 // Get errors for all the parameters before bailing out.
1714 bool Invalid = false;
1715
1716 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001717 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001718 ParamVector Params;
1719 Params.reserve(N);
1720 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1721 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001722 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001723 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001724 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001725 }
1726
1727 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001728 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001729 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001730
1731 TemplateParameterList *InstL
1732 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1733 L->getLAngleLoc(), &Params.front(), N,
1734 L->getRAngleLoc());
1735 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001736}
John McCalle29ba202009-08-20 01:44:21 +00001737
Douglas Gregored9c0f92009-10-29 00:04:11 +00001738/// \brief Instantiate the declaration of a class template partial
1739/// specialization.
1740///
1741/// \param ClassTemplate the (instantiated) class template that is partially
1742// specialized by the instantiation of \p PartialSpec.
1743///
1744/// \param PartialSpec the (uninstantiated) class template partial
1745/// specialization that we are instantiating.
1746///
1747/// \returns true if there was an error, false otherwise.
1748bool
1749TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1750 ClassTemplateDecl *ClassTemplate,
1751 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001752 // Create a local instantiation scope for this class template partial
1753 // specialization, which will contain the instantiations of the template
1754 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001755 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001756
Douglas Gregored9c0f92009-10-29 00:04:11 +00001757 // Substitute into the template parameters of the class template partial
1758 // specialization.
1759 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1760 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1761 if (!InstParams)
1762 return true;
1763
1764 // Substitute into the template arguments of the class template partial
1765 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001766 const TemplateArgumentLoc *PartialSpecTemplateArgs
1767 = PartialSpec->getTemplateArgsAsWritten();
1768 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1769
John McCalld5532b62009-11-23 01:53:49 +00001770 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001771 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001772 TemplateArgumentLoc Loc;
1773 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001774 return true;
John McCalld5532b62009-11-23 01:53:49 +00001775 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001776 }
1777
1778
1779 // Check that the template argument list is well-formed for this
1780 // class template.
1781 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1782 InstTemplateArgs.size());
1783 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1784 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001785 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001786 false,
1787 Converted))
1788 return true;
1789
1790 // Figure out where to insert this class template partial specialization
1791 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001792 void *InsertPos = 0;
1793 ClassTemplateSpecializationDecl *PrevDecl
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001794 = ClassTemplate->findPartialSpecialization(Converted.getFlatArguments(),
1795 Converted.flatSize(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001796
1797 // Build the canonical type that describes the converted template
1798 // arguments of the class template partial specialization.
1799 QualType CanonType
1800 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1801 Converted.getFlatArguments(),
1802 Converted.flatSize());
1803
1804 // Build the fully-sugared type for this class template
1805 // specialization as the user wrote in the specialization
1806 // itself. This means that we'll pretty-print the type retrieved
1807 // from the specialization's declaration the way that the user
1808 // actually wrote the specialization, rather than formatting the
1809 // name based on the "canonical" representation used to store the
1810 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001811 TypeSourceInfo *WrittenTy
1812 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1813 TemplateName(ClassTemplate),
1814 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001815 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001816 CanonType);
1817
1818 if (PrevDecl) {
1819 // We've already seen a partial specialization with the same template
1820 // parameters and template arguments. This can happen, for example, when
1821 // substituting the outer template arguments ends up causing two
1822 // class template partial specializations of a member class template
1823 // to have identical forms, e.g.,
1824 //
1825 // template<typename T, typename U>
1826 // struct Outer {
1827 // template<typename X, typename Y> struct Inner;
1828 // template<typename Y> struct Inner<T, Y>;
1829 // template<typename Y> struct Inner<U, Y>;
1830 // };
1831 //
1832 // Outer<int, int> outer; // error: the partial specializations of Inner
1833 // // have the same signature.
1834 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1835 << WrittenTy;
1836 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1837 << SemaRef.Context.getTypeDeclType(PrevDecl);
1838 return true;
1839 }
1840
1841
1842 // Create the class template partial specialization declaration.
1843 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001844 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1845 PartialSpec->getTagKind(),
1846 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001847 PartialSpec->getLocation(),
1848 InstParams,
1849 ClassTemplate,
1850 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001851 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001852 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001853 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001854 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001855 // Substitute the nested name specifier, if any.
1856 if (SubstQualifier(PartialSpec, InstPartialSpec))
1857 return 0;
1858
Douglas Gregored9c0f92009-10-29 00:04:11 +00001859 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001860 InstPartialSpec->setTypeAsWritten(WrittenTy);
1861
Douglas Gregored9c0f92009-10-29 00:04:11 +00001862 // Add this partial specialization to the set of class template partial
1863 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001864 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001865 return false;
1866}
1867
John McCall21ef0fa2010-03-11 09:03:00 +00001868TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001869TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001870 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001871 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1872 assert(OldTInfo && "substituting function without type source info");
1873 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001874 TypeSourceInfo *NewTInfo
1875 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1876 D->getTypeSpecStartLoc(),
1877 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001878 if (!NewTInfo)
1879 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001880
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001881 if (NewTInfo != OldTInfo) {
1882 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001883 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001884 if (FunctionProtoTypeLoc *OldProtoLoc
1885 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1886 TypeLoc NewTL = NewTInfo->getTypeLoc();
1887 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1888 assert(NewProtoLoc && "Missing prototype?");
1889 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1890 // FIXME: Variadic templates will break this.
1891 Params.push_back(NewProtoLoc->getArg(i));
1892 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001893 OldProtoLoc->getArg(i),
1894 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001895 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001896 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001897 } else {
1898 // The function type itself was not dependent and therefore no
1899 // substitution occurred. However, we still need to instantiate
1900 // the function parameters themselves.
1901 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001902 if (FunctionProtoTypeLoc *OldProtoLoc
1903 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1904 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1905 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1906 if (!Parm)
1907 return 0;
1908 Params.push_back(Parm);
1909 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001910 }
1911 }
John McCall21ef0fa2010-03-11 09:03:00 +00001912 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001913}
1914
Mike Stump1eb44332009-09-09 15:08:12 +00001915/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001916/// declaration (New) from the corresponding fields of its template (Tmpl).
1917///
1918/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001919bool
1920TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001921 FunctionDecl *Tmpl) {
1922 if (Tmpl->isDeleted())
1923 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Douglas Gregorcca9e962009-07-01 22:01:06 +00001925 // If we are performing substituting explicitly-specified template arguments
1926 // or deduced template arguments into a function template and we reach this
1927 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001928 // to keeping the new function template specialization. We therefore
1929 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001930 // into a template instantiation for this specific function template
1931 // specialization, which is not a SFINAE context, so that we diagnose any
1932 // further errors in the declaration itself.
1933 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1934 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1935 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1936 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001937 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001938 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001939 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001940 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001941 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001942 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1943 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001944 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001945 }
1946 }
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001948 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1949 assert(Proto && "Function template without prototype?");
1950
1951 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1952 Proto->getNoReturnAttr()) {
1953 // The function has an exception specification or a "noreturn"
1954 // attribute. Substitute into each of the exception types.
1955 llvm::SmallVector<QualType, 4> Exceptions;
1956 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1957 // FIXME: Poor location information!
1958 QualType T
1959 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1960 New->getLocation(), New->getDeclName());
1961 if (T.isNull() ||
1962 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1963 continue;
1964
1965 Exceptions.push_back(T);
1966 }
1967
1968 // Rebuild the function type
1969
1970 const FunctionProtoType *NewProto
1971 = New->getType()->getAs<FunctionProtoType>();
1972 assert(NewProto && "Template instantiation without function prototype?");
1973 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1974 NewProto->arg_type_begin(),
1975 NewProto->getNumArgs(),
1976 NewProto->isVariadic(),
1977 NewProto->getTypeQuals(),
1978 Proto->hasExceptionSpec(),
1979 Proto->hasAnyExceptionSpec(),
1980 Exceptions.size(),
1981 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001982 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001983 }
1984
John McCall1d8d1cc2010-08-01 02:01:53 +00001985 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001986
Douglas Gregore53060f2009-06-25 22:08:12 +00001987 return false;
1988}
1989
Douglas Gregor5545e162009-03-24 00:38:23 +00001990/// \brief Initializes common fields of an instantiated method
1991/// declaration (New) from the corresponding fields of its template
1992/// (Tmpl).
1993///
1994/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001995bool
1996TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001997 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001998 if (InitFunctionInstantiation(New, Tmpl))
1999 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Douglas Gregor5545e162009-03-24 00:38:23 +00002001 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
2002 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002003 if (Tmpl->isVirtualAsWritten())
2004 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00002005
2006 // FIXME: attributes
2007 // FIXME: New needs a pointer to Tmpl
2008 return false;
2009}
Douglas Gregora58861f2009-05-13 20:28:22 +00002010
2011/// \brief Instantiate the definition of the given function from its
2012/// template.
2013///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002014/// \param PointOfInstantiation the point at which the instantiation was
2015/// required. Note that this is not precisely a "point of instantiation"
2016/// for the function, but it's close.
2017///
Douglas Gregora58861f2009-05-13 20:28:22 +00002018/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002019/// function template specialization or member function of a class template
2020/// specialization.
2021///
2022/// \param Recursive if true, recursively instantiates any functions that
2023/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002024///
2025/// \param DefinitionRequired if true, then we are performing an explicit
2026/// instantiation where the body of the function is required. Complain if
2027/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002028void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002029 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002030 bool Recursive,
2031 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002032 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002033 return;
2034
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002035 // Never instantiate an explicit specialization.
2036 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2037 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002038
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002039 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002040 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002041 Stmt *Pattern = 0;
2042 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002043 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002044
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002045 if (!Pattern) {
2046 if (DefinitionRequired) {
2047 if (Function->getPrimaryTemplate())
2048 Diag(PointOfInstantiation,
2049 diag::err_explicit_instantiation_undefined_func_template)
2050 << Function->getPrimaryTemplate();
2051 else
2052 Diag(PointOfInstantiation,
2053 diag::err_explicit_instantiation_undefined_member)
2054 << 1 << Function->getDeclName() << Function->getDeclContext();
2055
2056 if (PatternDecl)
2057 Diag(PatternDecl->getLocation(),
2058 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002059 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002060 } else if (Function->getTemplateSpecializationKind()
2061 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002062 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002063 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002064 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002065
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002066 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002067 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002068
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002069 // C++0x [temp.explicit]p9:
2070 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002071 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002072 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002073 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002074 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002075 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002076 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002078 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2079 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002080 return;
2081
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002082 // If we're performing recursive template instantiation, create our own
2083 // queue of pending implicit instantiations that we will instantiate later,
2084 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002085 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002086 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002087 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Douglas Gregor9679caf2010-05-12 17:27:19 +00002089 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002090 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002091 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002092
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002093 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002094 // recorded, unless we're actually a member function within a local
2095 // class, in which case we need to merge our results with the parent
2096 // scope (of the enclosing function).
2097 bool MergeWithParentScope = false;
2098 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2099 MergeWithParentScope = Rec->isLocalClass();
2100
2101 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002103 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002104 // instantiation scope, and set the parameter names to those used
2105 // in the template.
2106 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2107 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2108 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2109 FunctionParam->setDeclName(PatternParam->getDeclName());
2110 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2111 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002112
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002113 // Enter the scope of this instantiation. We don't use
2114 // PushDeclContext because we don't have a scope.
2115 DeclContext *PreviousContext = CurContext;
2116 CurContext = Function;
2117
Mike Stump1eb44332009-09-09 15:08:12 +00002118 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002119 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002120
2121 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002122 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002123 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2124 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2125 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002126 }
2127
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002128 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002129 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002130
Douglas Gregor52604ab2009-09-11 21:19:12 +00002131 if (Body.isInvalid())
2132 Function->setInvalidDecl();
2133
John McCall9ae2f072010-08-23 23:25:46 +00002134 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002135 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002136
John McCall0c01d182010-03-24 05:22:00 +00002137 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2138
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002139 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002140
2141 DeclGroupRef DG(Function);
2142 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Douglas Gregor60406be2010-01-16 22:29:39 +00002144 // This class may have local implicit instantiations that need to be
2145 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002146 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002147 Scope.Exit();
2148
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002149 if (Recursive) {
2150 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002151 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002152 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002153
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002154 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002155 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002156 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002157}
2158
2159/// \brief Instantiate the definition of the given variable from its
2160/// template.
2161///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002162/// \param PointOfInstantiation the point at which the instantiation was
2163/// required. Note that this is not precisely a "point of instantiation"
2164/// for the function, but it's close.
2165///
2166/// \param Var the already-instantiated declaration of a static member
2167/// variable of a class template specialization.
2168///
2169/// \param Recursive if true, recursively instantiates any functions that
2170/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002171///
2172/// \param DefinitionRequired if true, then we are performing an explicit
2173/// instantiation where an out-of-line definition of the member variable
2174/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002175void Sema::InstantiateStaticDataMemberDefinition(
2176 SourceLocation PointOfInstantiation,
2177 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002178 bool Recursive,
2179 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002180 if (Var->isInvalidDecl())
2181 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002182
Douglas Gregor7caa6822009-07-24 20:34:43 +00002183 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002184 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002185 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002186 assert(Def->isStaticDataMember() && "Not a static data member?");
2187 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002188
Douglas Gregor0d035142009-10-27 18:42:08 +00002189 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002190 // We did not find an out-of-line definition of this static data member,
2191 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002192 // instantiate this definition (or provide a specialization for it) in
2193 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002194 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002195 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002196 Diag(PointOfInstantiation,
2197 diag::err_explicit_instantiation_undefined_member)
2198 << 2 << Var->getDeclName() << Var->getDeclContext();
2199 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002200 } else if (Var->getTemplateSpecializationKind()
2201 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002202 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002203 std::make_pair(Var, PointOfInstantiation));
2204 }
2205
Douglas Gregor7caa6822009-07-24 20:34:43 +00002206 return;
2207 }
2208
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002209 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002210 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002211 return;
2212
2213 // C++0x [temp.explicit]p9:
2214 // Except for inline functions, other explicit instantiation declarations
2215 // have the effect of suppressing the implicit instantiation of the entity
2216 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002217 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002218 == TSK_ExplicitInstantiationDeclaration)
2219 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002220
Douglas Gregor7caa6822009-07-24 20:34:43 +00002221 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2222 if (Inst)
2223 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002224
Douglas Gregor7caa6822009-07-24 20:34:43 +00002225 // If we're performing recursive template instantiation, create our own
2226 // queue of pending implicit instantiations that we will instantiate later,
2227 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002228 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002229 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002230 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002231
Douglas Gregor7caa6822009-07-24 20:34:43 +00002232 // Enter the scope of this instantiation. We don't use
2233 // PushDeclContext because we don't have a scope.
2234 DeclContext *PreviousContext = CurContext;
2235 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002237 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002238 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002239 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002240 CurContext = PreviousContext;
2241
2242 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002243 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2244 assert(MSInfo && "Missing member specialization information?");
2245 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2246 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002247 DeclGroupRef DG(Var);
2248 Consumer.HandleTopLevelDecl(DG);
2249 }
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Douglas Gregor7caa6822009-07-24 20:34:43 +00002251 if (Recursive) {
2252 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002253 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002254 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Douglas Gregor7caa6822009-07-24 20:34:43 +00002256 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002257 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002258 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002259}
Douglas Gregor815215d2009-05-27 05:35:12 +00002260
Anders Carlsson09025312009-08-29 05:16:22 +00002261void
2262Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2263 const CXXConstructorDecl *Tmpl,
2264 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Anders Carlsson09025312009-08-29 05:16:22 +00002266 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002267 bool AnyErrors = false;
2268
Anders Carlsson09025312009-08-29 05:16:22 +00002269 // Instantiate all the initializers.
2270 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002271 InitsEnd = Tmpl->init_end();
2272 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002273 CXXBaseOrMemberInitializer *Init = *Inits;
2274
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002275 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002276 ASTOwningVector<Expr*> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002277 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002279 // Instantiate the initializer.
2280 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2281 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2282 AnyErrors = true;
2283 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002284 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002285
Anders Carlsson09025312009-08-29 05:16:22 +00002286 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002287 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002288 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002289 TemplateArgs,
2290 Init->getSourceLocation(),
2291 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002292 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002293 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002294 New->setInvalidDecl();
2295 continue;
2296 }
2297
John McCalla93c9342009-12-07 02:54:59 +00002298 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002299 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002300 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002301 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002302 Init->getRParenLoc(),
2303 New->getParent());
2304 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002305 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002307 // Is this an anonymous union?
2308 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002309 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2310 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002311 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002312 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2313 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002314 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002315
2316 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002317 NewArgs.size(),
2318 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002319 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002320 Init->getRParenLoc());
2321 }
2322
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002323 if (NewInit.isInvalid()) {
2324 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002325 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002326 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002327 // FIXME: It would be nice if ASTOwningVector had a release function.
2328 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Anders Carlsson09025312009-08-29 05:16:22 +00002330 NewInits.push_back((MemInitTy *)NewInit.get());
2331 }
2332 }
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Anders Carlsson09025312009-08-29 05:16:22 +00002334 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002335 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002336 /*FIXME: ColonLoc */
2337 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002338 NewInits.data(), NewInits.size(),
2339 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002340}
2341
John McCall52a575a2009-08-29 08:11:13 +00002342// TODO: this could be templated if the various decl types used the
2343// same method name.
2344static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2345 ClassTemplateDecl *Instance) {
2346 Pattern = Pattern->getCanonicalDecl();
2347
2348 do {
2349 Instance = Instance->getCanonicalDecl();
2350 if (Pattern == Instance) return true;
2351 Instance = Instance->getInstantiatedFromMemberTemplate();
2352 } while (Instance);
2353
2354 return false;
2355}
2356
Douglas Gregor0d696532009-09-28 06:34:35 +00002357static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2358 FunctionTemplateDecl *Instance) {
2359 Pattern = Pattern->getCanonicalDecl();
2360
2361 do {
2362 Instance = Instance->getCanonicalDecl();
2363 if (Pattern == Instance) return true;
2364 Instance = Instance->getInstantiatedFromMemberTemplate();
2365 } while (Instance);
2366
2367 return false;
2368}
2369
Douglas Gregored9c0f92009-10-29 00:04:11 +00002370static bool
2371isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2372 ClassTemplatePartialSpecializationDecl *Instance) {
2373 Pattern
2374 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2375 do {
2376 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2377 Instance->getCanonicalDecl());
2378 if (Pattern == Instance)
2379 return true;
2380 Instance = Instance->getInstantiatedFromMember();
2381 } while (Instance);
2382
2383 return false;
2384}
2385
John McCall52a575a2009-08-29 08:11:13 +00002386static bool isInstantiationOf(CXXRecordDecl *Pattern,
2387 CXXRecordDecl *Instance) {
2388 Pattern = Pattern->getCanonicalDecl();
2389
2390 do {
2391 Instance = Instance->getCanonicalDecl();
2392 if (Pattern == Instance) return true;
2393 Instance = Instance->getInstantiatedFromMemberClass();
2394 } while (Instance);
2395
2396 return false;
2397}
2398
2399static bool isInstantiationOf(FunctionDecl *Pattern,
2400 FunctionDecl *Instance) {
2401 Pattern = Pattern->getCanonicalDecl();
2402
2403 do {
2404 Instance = Instance->getCanonicalDecl();
2405 if (Pattern == Instance) return true;
2406 Instance = Instance->getInstantiatedFromMemberFunction();
2407 } while (Instance);
2408
2409 return false;
2410}
2411
2412static bool isInstantiationOf(EnumDecl *Pattern,
2413 EnumDecl *Instance) {
2414 Pattern = Pattern->getCanonicalDecl();
2415
2416 do {
2417 Instance = Instance->getCanonicalDecl();
2418 if (Pattern == Instance) return true;
2419 Instance = Instance->getInstantiatedFromMemberEnum();
2420 } while (Instance);
2421
2422 return false;
2423}
2424
John McCalled976492009-12-04 22:46:56 +00002425static bool isInstantiationOf(UsingShadowDecl *Pattern,
2426 UsingShadowDecl *Instance,
2427 ASTContext &C) {
2428 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2429}
2430
2431static bool isInstantiationOf(UsingDecl *Pattern,
2432 UsingDecl *Instance,
2433 ASTContext &C) {
2434 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2435}
2436
John McCall7ba107a2009-11-18 02:36:19 +00002437static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2438 UsingDecl *Instance,
2439 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002440 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002441}
2442
2443static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002444 UsingDecl *Instance,
2445 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002446 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002447}
2448
John McCall52a575a2009-08-29 08:11:13 +00002449static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2450 VarDecl *Instance) {
2451 assert(Instance->isStaticDataMember());
2452
2453 Pattern = Pattern->getCanonicalDecl();
2454
2455 do {
2456 Instance = Instance->getCanonicalDecl();
2457 if (Pattern == Instance) return true;
2458 Instance = Instance->getInstantiatedFromStaticDataMember();
2459 } while (Instance);
2460
2461 return false;
2462}
2463
John McCalled976492009-12-04 22:46:56 +00002464// Other is the prospective instantiation
2465// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002466static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002467 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002468 if (UnresolvedUsingTypenameDecl *UUD
2469 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2470 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2471 return isInstantiationOf(UUD, UD, Ctx);
2472 }
2473 }
2474
2475 if (UnresolvedUsingValueDecl *UUD
2476 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002477 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2478 return isInstantiationOf(UUD, UD, Ctx);
2479 }
2480 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002481
Anders Carlsson0d8df782009-08-29 19:37:28 +00002482 return false;
2483 }
Mike Stump1eb44332009-09-09 15:08:12 +00002484
John McCall52a575a2009-08-29 08:11:13 +00002485 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2486 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002487
John McCall52a575a2009-08-29 08:11:13 +00002488 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2489 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002490
John McCall52a575a2009-08-29 08:11:13 +00002491 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2492 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002493
Douglas Gregor7caa6822009-07-24 20:34:43 +00002494 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002495 if (Var->isStaticDataMember())
2496 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2497
2498 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2499 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002500
Douglas Gregor0d696532009-09-28 06:34:35 +00002501 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2502 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2503
Douglas Gregored9c0f92009-10-29 00:04:11 +00002504 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2505 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2506 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2507 PartialSpec);
2508
Anders Carlssond8b285f2009-09-01 04:26:58 +00002509 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2510 if (!Field->getDeclName()) {
2511 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002512 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002513 cast<FieldDecl>(D);
2514 }
2515 }
Mike Stump1eb44332009-09-09 15:08:12 +00002516
John McCalled976492009-12-04 22:46:56 +00002517 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2518 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2519
2520 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2521 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2522
Douglas Gregor815215d2009-05-27 05:35:12 +00002523 return D->getDeclName() && isa<NamedDecl>(Other) &&
2524 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2525}
2526
2527template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002528static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002529 NamedDecl *D,
2530 ForwardIterator first,
2531 ForwardIterator last) {
2532 for (; first != last; ++first)
2533 if (isInstantiationOf(Ctx, D, *first))
2534 return cast<NamedDecl>(*first);
2535
2536 return 0;
2537}
2538
John McCall02cace72009-08-28 07:59:38 +00002539/// \brief Finds the instantiation of the given declaration context
2540/// within the current instantiation.
2541///
2542/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002543DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002544 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002545 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002546 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002547 return cast_or_null<DeclContext>(ID);
2548 } else return DC;
2549}
2550
Douglas Gregored961e72009-05-27 17:54:46 +00002551/// \brief Find the instantiation of the given declaration within the
2552/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002553///
2554/// This routine is intended to be used when \p D is a declaration
2555/// referenced from within a template, that needs to mapped into the
2556/// corresponding declaration within an instantiation. For example,
2557/// given:
2558///
2559/// \code
2560/// template<typename T>
2561/// struct X {
2562/// enum Kind {
2563/// KnownValue = sizeof(T)
2564/// };
2565///
2566/// bool getKind() const { return KnownValue; }
2567/// };
2568///
2569/// template struct X<int>;
2570/// \endcode
2571///
2572/// In the instantiation of X<int>::getKind(), we need to map the
2573/// EnumConstantDecl for KnownValue (which refers to
2574/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002575/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2576/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002577NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002578 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002579 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002580 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002581 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002582 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002583 // D is a local of some kind. Look into the map of local
2584 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002585 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002586 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002587
Douglas Gregore95b4092009-09-16 18:34:49 +00002588 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2589 if (!Record->isDependentContext())
2590 return D;
2591
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002592 // If the RecordDecl is actually the injected-class-name or a
2593 // "templated" declaration for a class template, class template
2594 // partial specialization, or a member class of a class template,
2595 // substitute into the injected-class-name of the class template
2596 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002597 QualType T;
2598 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2599
2600 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002601 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002602 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2603 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002604 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002605
2606 // If we call SubstType with an InjectedClassNameType here we
2607 // can end up in an infinite loop.
2608 T = Context.getTypeDeclType(Record);
2609 assert(isa<InjectedClassNameType>(T) &&
2610 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002611 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002612 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002613
2614 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002615 // Substitute into the injected-class-name to get the type
2616 // corresponding to the instantiation we want, which may also be
2617 // the current instantiation (if we're in a template
2618 // definition). This substitution should never fail, since we
2619 // know we can instantiate the injected-class-name or we
2620 // wouldn't have gotten to the injected-class-name!
2621
2622 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2623 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002624 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2625 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2626
2627 if (!T->isDependentType()) {
2628 assert(T->isRecordType() && "Instantiation must produce a record type");
2629 return T->getAs<RecordType>()->getDecl();
2630 }
2631
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002632 // We are performing "partial" template instantiation to create
2633 // the member declarations for the members of a class template
2634 // specialization. Therefore, D is actually referring to something
2635 // in the current instantiation. Look through the current
2636 // context, which contains actual instantiations, to find the
2637 // instantiation of the "current instantiation" that D refers
2638 // to.
2639 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002640 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002641 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002642 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002643 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002644 if (isInstantiationOf(ClassTemplate,
2645 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002646 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002647
2648 if (!DC->isDependentContext())
2649 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002650 }
2651
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002652 // We're performing "instantiation" of a member of the current
2653 // instantiation while we are type-checking the
2654 // definition. Compute the declaration context and return that.
2655 assert(!SawNonDependentContext &&
2656 "No dependent context while instantiating record");
2657 DeclContext *DC = computeDeclContext(T);
2658 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002659 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002660 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002661 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002662
Douglas Gregore95b4092009-09-16 18:34:49 +00002663 // Fall through to deal with other dependent record types (e.g.,
2664 // anonymous unions in class templates).
2665 }
John McCall52a575a2009-08-29 08:11:13 +00002666
Douglas Gregore95b4092009-09-16 18:34:49 +00002667 if (!ParentDC->isDependentContext())
2668 return D;
2669
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002670 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002671 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002672 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Douglas Gregor815215d2009-05-27 05:35:12 +00002674 if (ParentDC != D->getDeclContext()) {
2675 // We performed some kind of instantiation in the parent context,
2676 // so now we need to look into the instantiated parent context to
2677 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002678
John McCall3cb0ebd2010-03-10 03:28:59 +00002679 // If our context used to be dependent, we may need to instantiate
2680 // it before performing lookup into that context.
2681 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002682 if (!Spec->isDependentContext()) {
2683 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002684 const RecordType *Tag = T->getAs<RecordType>();
2685 assert(Tag && "type of non-dependent record is not a RecordType");
2686 if (!Tag->isBeingDefined() &&
2687 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2688 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002689 }
2690 }
2691
Douglas Gregor815215d2009-05-27 05:35:12 +00002692 NamedDecl *Result = 0;
2693 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002694 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002695 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2696 } else {
2697 // Since we don't have a name for the entity we're looking for,
2698 // our only option is to walk through all of the declarations to
2699 // find that name. This will occur in a few cases:
2700 //
2701 // - anonymous struct/union within a template
2702 // - unnamed class/struct/union/enum within a template
2703 //
2704 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002705 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002706 ParentDC->decls_begin(),
2707 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002708 }
Mike Stump1eb44332009-09-09 15:08:12 +00002709
John McCall9f54ad42009-12-10 09:41:52 +00002710 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002711 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2712 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002713 && "Unable to find instantiation of declaration!");
2714
Douglas Gregor815215d2009-05-27 05:35:12 +00002715 D = Result;
2716 }
2717
Douglas Gregor815215d2009-05-27 05:35:12 +00002718 return D;
2719}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002720
Mike Stump1eb44332009-09-09 15:08:12 +00002721/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002722/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002723void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002724 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00002725 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002726 PendingImplicitInstantiation Inst;
2727
2728 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002729 Inst = PendingInstantiations.front();
2730 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00002731 } else {
2732 Inst = PendingLocalImplicitInstantiations.front();
2733 PendingLocalImplicitInstantiations.pop_front();
2734 }
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Douglas Gregor7caa6822009-07-24 20:34:43 +00002736 // Instantiate function definitions
2737 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00002738 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2739 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00002740 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
2741 TSK_ExplicitInstantiationDefinition;
2742 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
2743 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002744 continue;
2745 }
Mike Stump1eb44332009-09-09 15:08:12 +00002746
Douglas Gregor7caa6822009-07-24 20:34:43 +00002747 // Instantiate static data member definitions.
2748 VarDecl *Var = cast<VarDecl>(Inst.first);
2749 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002750
Chandler Carruth291b4412010-02-13 10:17:50 +00002751 // Don't try to instantiate declarations if the most recent redeclaration
2752 // is invalid.
2753 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2754 continue;
2755
2756 // Check if the most recent declaration has changed the specialization kind
2757 // and removed the need for implicit instantiation.
2758 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2759 case TSK_Undeclared:
2760 assert(false && "Cannot instantitiate an undeclared specialization.");
2761 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00002762 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00002763 continue; // No longer need to instantiate this type.
2764 case TSK_ExplicitInstantiationDefinition:
2765 // We only need an instantiation if the pending instantiation *is* the
2766 // explicit instantiation.
2767 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00002768 case TSK_ImplicitInstantiation:
2769 break;
2770 }
2771
John McCallf312b1e2010-08-26 23:41:50 +00002772 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2773 "instantiating static data member "
2774 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002775
Chandler Carruth58e390e2010-08-25 08:27:02 +00002776 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
2777 TSK_ExplicitInstantiationDefinition;
2778 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
2779 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002780 }
2781}
John McCall0c01d182010-03-24 05:22:00 +00002782
2783void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2784 const MultiLevelTemplateArgumentList &TemplateArgs) {
2785 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2786 E = Pattern->ddiag_end(); I != E; ++I) {
2787 DependentDiagnostic *DD = *I;
2788
2789 switch (DD->getKind()) {
2790 case DependentDiagnostic::Access:
2791 HandleDependentAccessCheck(*DD, TemplateArgs);
2792 break;
2793 }
2794 }
2795}
John McCallf312b1e2010-08-26 23:41:50 +00002796