blob: c97a8788ef205c037c1b1e99594c7a3b1401ca3b [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,
John McCallca0408f2010-08-23 06:44:23 +0000254 ASTOwningVector<Expr*> &InitArgs) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000255 for (unsigned I = 0; I != NumArgs; ++I) {
256 // When we hit the first defaulted argument, break out of the loop:
257 // we don't pass those default arguments on.
258 if (Args[I]->isDefaultArgument())
259 break;
260
John McCall60d7b3a2010-08-24 06:29:42 +0000261 ExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000262 if (Arg.isInvalid())
263 return true;
264
Douglas Gregor6eef5192009-12-14 19:27:10 +0000265 InitArgs.push_back(Arg.release());
Douglas Gregor6eef5192009-12-14 19:27:10 +0000266 }
267
268 return false;
269}
270
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000271/// \brief Instantiate an initializer, breaking it into separate
272/// initialization arguments.
273///
274/// \param S The semantic analysis object.
275///
276/// \param Init The initializer to instantiate.
277///
278/// \param TemplateArgs Template arguments to be substituted into the
279/// initializer.
280///
281/// \param NewArgs Will be filled in with the instantiation arguments.
282///
283/// \returns true if an error occurred, false otherwise
284static bool InstantiateInitializer(Sema &S, Expr *Init,
285 const MultiLevelTemplateArgumentList &TemplateArgs,
286 SourceLocation &LParenLoc,
John McCallca0408f2010-08-23 06:44:23 +0000287 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000288 SourceLocation &RParenLoc) {
289 NewArgs.clear();
290 LParenLoc = SourceLocation();
291 RParenLoc = SourceLocation();
292
293 if (!Init)
294 return false;
295
296 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
297 Init = ExprTemp->getSubExpr();
298
299 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
300 Init = Binder->getSubExpr();
301
302 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
303 Init = ICE->getSubExprAsWritten();
304
305 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
306 LParenLoc = ParenList->getLParenLoc();
307 RParenLoc = ParenList->getRParenLoc();
308 return InstantiateInitializationArguments(S, ParenList->getExprs(),
309 ParenList->getNumExprs(),
Douglas Gregora1a04782010-09-09 16:33:13 +0000310 TemplateArgs, NewArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000311 }
312
313 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000314 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
315 if (InstantiateInitializationArguments(S,
316 Construct->getArgs(),
317 Construct->getNumArgs(),
Douglas Gregora1a04782010-09-09 16:33:13 +0000318 TemplateArgs, NewArgs))
Douglas Gregor28329e52010-03-24 21:22:47 +0000319 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000320
Douglas Gregor28329e52010-03-24 21:22:47 +0000321 // FIXME: Fake locations!
322 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
Douglas Gregora1a04782010-09-09 16:33:13 +0000323 RParenLoc = LParenLoc;
Douglas Gregor28329e52010-03-24 21:22:47 +0000324 return false;
325 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000326 }
327
John McCall60d7b3a2010-08-24 06:29:42 +0000328 ExprResult Result = S.SubstExpr(Init, TemplateArgs);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000329 if (Result.isInvalid())
330 return true;
331
332 NewArgs.push_back(Result.takeAs<Expr>());
333 return false;
334}
335
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000336Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000337 // If this is the variable for an anonymous struct or union,
338 // instantiate the anonymous struct/union type first.
339 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
340 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
341 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
342 return 0;
343
John McCallce3ff2b2009-08-25 22:02:44 +0000344 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000345 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000346 TemplateArgs,
347 D->getTypeSpecStartLoc(),
348 D->getDeclName());
349 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000350 return 0;
351
Douglas Gregorc6dbc3f2010-09-12 07:37:24 +0000352 if (DI->getType()->isFunctionType()) {
353 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
354 << D->isStaticDataMember() << DI->getType();
355 return 0;
356 }
357
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000358 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000359 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
360 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000361 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000362 D->getStorageClass(),
363 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000364 Var->setThreadSpecified(D->isThreadSpecified());
365 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000366
John McCallb6217662010-03-15 10:12:16 +0000367 // Substitute the nested name specifier, if any.
368 if (SubstQualifier(D, Var))
369 return 0;
370
Mike Stump1eb44332009-09-09 15:08:12 +0000371 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000372 // out-of-line, the instantiation will have the same lexical
373 // context (which will be a namespace scope) as the template.
374 if (D->isOutOfLine())
375 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000376
John McCall46460a62010-01-20 21:53:11 +0000377 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000378
379 if (!D->isStaticDataMember())
380 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000381
Mike Stump390b4cc2009-05-16 07:39:55 +0000382 // FIXME: In theory, we could have a previous declaration for variables that
383 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000384 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000385 // FIXME: having to fake up a LookupResult is dumb.
386 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000387 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000388 if (D->isStaticDataMember())
389 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000390 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor7caa6822009-07-24 20:34:43 +0000392 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000393 if (!D->isStaticDataMember())
394 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000395 Owner->makeDeclVisibleInContext(Var);
396 } else {
397 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000398 if (Owner->isFunctionOrMethod())
399 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000400 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000401 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000402
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000403 // Link instantiations of static data members back to the template from
404 // which they were instantiated.
405 if (Var->isStaticDataMember())
406 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000407 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000408
Douglas Gregor60c93c92010-02-09 07:26:29 +0000409 if (Var->getAnyInitializer()) {
410 // We already have an initializer in the class.
411 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000412 if (Var->isStaticDataMember() && !D->isOutOfLine())
413 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
414 else
415 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
416
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000417 // Instantiate the initializer.
418 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +0000419 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000420 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
Douglas Gregora1a04782010-09-09 16:33:13 +0000421 InitArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000422 // Attach the initializer to the declaration.
423 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000424 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000425 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000426 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000427 move_arg(InitArgs),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000428 RParenLoc);
429 } else if (InitArgs.size() == 1) {
John McCall9ae2f072010-08-23 23:25:46 +0000430 Expr *Init = InitArgs.take()[0];
431 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000432 } else {
433 assert(InitArgs.size() == 0);
John McCalld226f652010-08-21 09:40:31 +0000434 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000435 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000436 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000437 // FIXME: Not too happy about invalidating the declaration
438 // because of a bogus initializer.
439 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000440 }
441
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000442 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000443 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000444 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000445
Douglas Gregor5764f612010-05-08 23:05:03 +0000446 // Diagnose unused local variables.
447 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
448 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000449
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000450 return Var;
451}
452
Abramo Bagnara6206d532010-06-05 05:09:32 +0000453Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
454 AccessSpecDecl* AD
455 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
456 D->getAccessSpecifierLoc(), D->getColonLoc());
457 Owner->addHiddenDecl(AD);
458 return AD;
459}
460
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000461Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
462 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000463 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000464 if (DI->getType()->isDependentType() ||
465 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000466 DI = SemaRef.SubstType(DI, TemplateArgs,
467 D->getLocation(), D->getDeclName());
468 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000469 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000470 Invalid = true;
471 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000472 // C++ [temp.arg.type]p3:
473 // If a declaration acquires a function type through a type
474 // dependent on a template-parameter and this causes a
475 // declaration that does not use the syntactic form of a
476 // function declarator to have function type, the program is
477 // ill-formed.
478 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000479 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000480 Invalid = true;
481 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000482 } else {
483 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000484 }
485
486 Expr *BitWidth = D->getBitWidth();
487 if (Invalid)
488 BitWidth = 0;
489 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000490 // The bit-width expression is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000491 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
John McCall60d7b3a2010-08-24 06:29:42 +0000493 ExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000494 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000495 if (InstantiatedBitWidth.isInvalid()) {
496 Invalid = true;
497 BitWidth = 0;
498 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000499 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000500 }
501
John McCall07fb6be2009-10-22 23:33:21 +0000502 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
503 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000504 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000505 D->getLocation(),
506 D->isMutable(),
507 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000508 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000509 D->getAccess(),
510 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000511 if (!Field) {
512 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000513 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
John McCall1d8d1cc2010-08-01 02:01:53 +0000516 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000517
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000518 if (Invalid)
519 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000521 if (!Field->getDeclName()) {
522 // Keep track of where this decl came from.
523 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000524 }
525 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
526 if (Parent->isAnonymousStructOrUnion() &&
Sebastian Redl7a126a42010-08-31 00:36:30 +0000527 Parent->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000528 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000531 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000532 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000533 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000534
535 return Field;
536}
537
John McCall02cace72009-08-28 07:59:38 +0000538Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000539 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000540 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000541 if (TypeSourceInfo *Ty = D->getFriendType()) {
542 TypeSourceInfo *InstTy =
543 SemaRef.SubstType(Ty, TemplateArgs,
544 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000545 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000546 return 0;
John McCall02cace72009-08-28 07:59:38 +0000547
Douglas Gregor06245bf2010-04-07 17:57:12 +0000548 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
549 if (!FD)
550 return 0;
551
552 FD->setAccess(AS_public);
553 Owner->addDecl(FD);
554 return FD;
555 }
556
557 NamedDecl *ND = D->getFriendDecl();
558 assert(ND && "friend decl must be a decl or a type!");
559
John McCallaf2094e2010-04-08 09:05:18 +0000560 // All of the Visit implementations for the various potential friend
561 // declarations have to be carefully written to work for friend
562 // objects, with the most important detail being that the target
563 // decl should almost certainly not be placed in Owner.
564 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000565 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
John McCall02cace72009-08-28 07:59:38 +0000567 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000568 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
569 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000570 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000571 Owner->addDecl(FD);
572 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000573}
574
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000575Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
576 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Douglas Gregorac7610d2009-06-22 20:57:11 +0000578 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000579 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
John McCall60d7b3a2010-08-24 06:29:42 +0000581 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000582 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000583 if (InstantiatedAssertExpr.isInvalid())
584 return 0;
585
John McCall60d7b3a2010-08-24 06:29:42 +0000586 ExprResult Message(D->getMessage());
Douglas Gregor43d9d922009-08-08 01:41:12 +0000587 D->getMessage()->Retain();
John McCalld226f652010-08-21 09:40:31 +0000588 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000589 InstantiatedAssertExpr.get(),
590 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000591}
592
593Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000594 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000595 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000596 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000597 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000598 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000599 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000600 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000602 Enum->startDefinition();
603
Douglas Gregor96084f12010-03-01 19:00:07 +0000604 if (D->getDeclContext()->isFunctionOrMethod())
605 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
606
John McCalld226f652010-08-21 09:40:31 +0000607 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000608
609 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000610 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
611 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000612 EC != ECEnd; ++EC) {
613 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000614 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000615 if (Expr *UninstValue = EC->getInitExpr()) {
616 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000617 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000618 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
John McCallce3ff2b2009-08-25 22:02:44 +0000620 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000621 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000622
623 // Drop the initial value and continue.
624 bool isInvalid = false;
625 if (Value.isInvalid()) {
626 Value = SemaRef.Owned((Expr *)0);
627 isInvalid = true;
628 }
629
Mike Stump1eb44332009-09-09 15:08:12 +0000630 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000631 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
632 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000633 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000634
635 if (isInvalid) {
636 if (EnumConst)
637 EnumConst->setInvalidDecl();
638 Enum->setInvalidDecl();
639 }
640
641 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000642 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000643 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000644 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000645 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000646
647 if (D->getDeclContext()->isFunctionOrMethod()) {
648 // If the enumeration is within a function or method, record the enum
649 // constant as a local.
650 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
651 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000652 }
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000655 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000656 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000657 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000658 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000659 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000660 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000661
662 return Enum;
663}
664
Douglas Gregor6477b692009-03-25 15:04:13 +0000665Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
666 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
667 return 0;
668}
669
John McCalle29ba202009-08-20 01:44:21 +0000670Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000671 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
672
Douglas Gregor550d9b22009-10-31 17:21:17 +0000673 // Create a local instantiation scope for this class template, which
674 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000675 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000676 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000677 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000678 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000679 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000680
681 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000682
683 // Instantiate the qualifier. We have to do this first in case
684 // we're a friend declaration, because if we are then we need to put
685 // the new declaration in the appropriate context.
686 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
687 if (Qualifier) {
688 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
689 Pattern->getQualifierRange(),
690 TemplateArgs);
691 if (!Qualifier) return 0;
692 }
693
694 CXXRecordDecl *PrevDecl = 0;
695 ClassTemplateDecl *PrevClassTemplate = 0;
696
697 // If this isn't a friend, then it's a member template, in which
698 // case we just want to build the instantiation in the
699 // specialization. If it is a friend, we want to build it in
700 // the appropriate context.
701 DeclContext *DC = Owner;
702 if (isFriend) {
703 if (Qualifier) {
704 CXXScopeSpec SS;
705 SS.setScopeRep(Qualifier);
706 SS.setRange(Pattern->getQualifierRange());
707 DC = SemaRef.computeDeclContext(SS);
708 if (!DC) return 0;
709 } else {
710 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
711 Pattern->getDeclContext(),
712 TemplateArgs);
713 }
714
715 // Look for a previous declaration of the template in the owning
716 // context.
717 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
718 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
719 SemaRef.LookupQualifiedName(R, DC);
720
721 if (R.isSingleResult()) {
722 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
723 if (PrevClassTemplate)
724 PrevDecl = PrevClassTemplate->getTemplatedDecl();
725 }
726
727 if (!PrevClassTemplate && Qualifier) {
728 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000729 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
730 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000731 return 0;
732 }
733
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000734 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000735 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000736 bool Complain = true;
737
738 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
739 // template for struct std::tr1::__detail::_Map_base, where the
740 // template parameters of the friend declaration don't match the
741 // template parameters of the original declaration. In this one
742 // case, we don't complain about the ill-formed friend
743 // declaration.
744 if (isFriend && Pattern->getIdentifier() &&
745 Pattern->getIdentifier()->isStr("_Map_base") &&
746 DC->isNamespace() &&
747 cast<NamespaceDecl>(DC)->getIdentifier() &&
748 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
749 DeclContext *DCParent = DC->getParent();
750 if (DCParent->isNamespace() &&
751 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
752 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
753 DeclContext *DCParent2 = DCParent->getParent();
754 if (DCParent2->isNamespace() &&
755 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
756 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
757 DCParent2->getParent()->isTranslationUnit())
758 Complain = false;
759 }
760 }
761
John McCall93ba8572010-03-25 06:39:04 +0000762 TemplateParameterList *PrevParams
763 = PrevClassTemplate->getTemplateParameters();
764
765 // Make sure the parameter lists match.
766 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000767 Complain,
768 Sema::TPL_TemplateMatch)) {
769 if (Complain)
770 return 0;
771
772 AdoptedPreviousTemplateParams = true;
773 InstParams = PrevParams;
774 }
John McCall93ba8572010-03-25 06:39:04 +0000775
776 // Do some additional validation, then merge default arguments
777 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000778 if (!AdoptedPreviousTemplateParams &&
779 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000780 Sema::TPC_ClassTemplate))
781 return 0;
782 }
783 }
784
John McCalle29ba202009-08-20 01:44:21 +0000785 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000786 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000787 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000788 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000789 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000790
John McCall93ba8572010-03-25 06:39:04 +0000791 if (Qualifier)
792 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000793
John McCalle29ba202009-08-20 01:44:21 +0000794 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000795 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
796 D->getIdentifier(), InstParams, RecordInst,
797 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000798 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000799
John McCall93ba8572010-03-25 06:39:04 +0000800 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000801 if (PrevClassTemplate)
802 Inst->setAccess(PrevClassTemplate->getAccess());
803 else
804 Inst->setAccess(D->getAccess());
805
John McCall93ba8572010-03-25 06:39:04 +0000806 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
807 // TODO: do we want to track the instantiation progeny of this
808 // friend target decl?
809 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000810 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000811 Inst->setInstantiatedFromMemberTemplate(D);
812 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000813
814 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000815 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000816 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000817
Douglas Gregor259571e2009-10-30 22:42:42 +0000818 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000819 if (isFriend) {
820 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000821 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000822 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000823
John McCalle29ba202009-08-20 01:44:21 +0000824 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000825
Douglas Gregored9c0f92009-10-29 00:04:11 +0000826 // Instantiate all of the partial specializations of this member class
827 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000828 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
829 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000830 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
831 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
832
John McCalle29ba202009-08-20 01:44:21 +0000833 return Inst;
834}
835
Douglas Gregord60e1052009-08-27 16:57:43 +0000836Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000837TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
838 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000839 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
840
841 // Lookup the already-instantiated declaration in the instantiation
842 // of the class template and return that.
843 DeclContext::lookup_result Found
844 = Owner->lookup(ClassTemplate->getDeclName());
845 if (Found.first == Found.second)
846 return 0;
847
848 ClassTemplateDecl *InstClassTemplate
849 = dyn_cast<ClassTemplateDecl>(*Found.first);
850 if (!InstClassTemplate)
851 return 0;
852
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000853 return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000854}
855
856Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000857TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000858 // Create a local instantiation scope for this function template, which
859 // will contain the instantiations of the template parameters and then get
860 // merged with the local instantiation scope for the function template
861 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000862 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000863
Douglas Gregord60e1052009-08-27 16:57:43 +0000864 TemplateParameterList *TempParams = D->getTemplateParameters();
865 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000866 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000867 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000868
Douglas Gregora735b202009-10-13 14:39:41 +0000869 FunctionDecl *Instantiated = 0;
870 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
871 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
872 InstParams));
873 else
874 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
875 D->getTemplatedDecl(),
876 InstParams));
877
878 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000879 return 0;
880
John McCall46460a62010-01-20 21:53:11 +0000881 Instantiated->setAccess(D->getAccess());
882
Mike Stump1eb44332009-09-09 15:08:12 +0000883 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000884 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000885 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000886 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000887 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000888 assert(InstTemplate &&
889 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000890
John McCallb1a56e72010-03-26 23:10:15 +0000891 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
892
John McCalle976ffe2009-12-14 23:19:40 +0000893 // Link the instantiation back to the pattern *unless* this is a
894 // non-definition friend declaration.
895 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000896 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000897 InstTemplate->setInstantiatedFromMemberTemplate(D);
898
John McCallb1a56e72010-03-26 23:10:15 +0000899 // Make declarations visible in the appropriate context.
900 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000901 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000902
Douglas Gregord60e1052009-08-27 16:57:43 +0000903 return InstTemplate;
904}
905
Douglas Gregord475b8d2009-03-25 21:17:03 +0000906Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
907 CXXRecordDecl *PrevDecl = 0;
908 if (D->isInjectedClassName())
909 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000910 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000911 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
912 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000913 TemplateArgs);
914 if (!Prev) return 0;
915 PrevDecl = cast<CXXRecordDecl>(Prev);
916 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000917
918 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000919 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000920 D->getLocation(), D->getIdentifier(),
921 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000922
923 // Substitute the nested name specifier, if any.
924 if (SubstQualifier(D, Record))
925 return 0;
926
Douglas Gregord475b8d2009-03-25 21:17:03 +0000927 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000928 // FIXME: Check against AS_none is an ugly hack to work around the issue that
929 // the tag decls introduced by friend class declarations don't have an access
930 // specifier. Remove once this area of the code gets sorted out.
931 if (D->getAccess() != AS_none)
932 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000933 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000934 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000935
John McCall02cace72009-08-28 07:59:38 +0000936 // If the original function was part of a friend declaration,
937 // inherit its namespace state.
938 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
939 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
940
Douglas Gregor9901c572010-05-21 00:31:19 +0000941 // Make sure that anonymous structs and unions are recorded.
942 if (D->isAnonymousStructOrUnion()) {
943 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000944 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000945 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
946 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000947
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000948 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000949 return Record;
950}
951
John McCall02cace72009-08-28 07:59:38 +0000952/// Normal class members are of more specific types and therefore
953/// don't make it here. This function serves two purposes:
954/// 1) instantiating function templates
955/// 2) substituting friend declarations
956/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000957Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000958 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000959 // Check whether there is already a function template specialization for
960 // this declaration.
961 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
962 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000963 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000964 std::pair<const TemplateArgument *, unsigned> Innermost
965 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000967 FunctionDecl *SpecFunc
968 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
969 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Douglas Gregor127102b2009-06-29 20:59:39 +0000971 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000972 if (SpecFunc)
973 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
John McCallb0cb0222010-03-27 05:57:59 +0000976 bool isFriend;
977 if (FunctionTemplate)
978 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
979 else
980 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
981
Douglas Gregor79c22782010-01-16 20:21:20 +0000982 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000983 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000984 !(isa<Decl>(Owner) &&
985 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +0000986 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregore53060f2009-06-25 22:08:12 +0000988 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000989 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
990 TInfo = SubstFunctionType(D, Params);
991 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000992 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000993 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000994
John McCalld325daa2010-03-26 04:53:08 +0000995 NestedNameSpecifier *Qualifier = D->getQualifier();
996 if (Qualifier) {
997 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
998 D->getQualifierRange(),
999 TemplateArgs);
1000 if (!Qualifier) return 0;
1001 }
1002
John McCall68b6b872010-02-06 01:50:47 +00001003 // If we're instantiating a local function declaration, put the result
1004 // in the owner; otherwise we need to find the instantiated context.
1005 DeclContext *DC;
1006 if (D->getDeclContext()->isFunctionOrMethod())
1007 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001008 else if (isFriend && Qualifier) {
1009 CXXScopeSpec SS;
1010 SS.setScopeRep(Qualifier);
1011 SS.setRange(D->getQualifierRange());
1012 DC = SemaRef.computeDeclContext(SS);
1013 if (!DC) return 0;
1014 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001015 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1016 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001017 }
John McCall68b6b872010-02-06 01:50:47 +00001018
John McCall02cace72009-08-28 07:59:38 +00001019 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001020 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001021 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001022 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001023 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001024
John McCalld325daa2010-03-26 04:53:08 +00001025 if (Qualifier)
1026 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001027
John McCallb1a56e72010-03-26 23:10:15 +00001028 DeclContext *LexicalDC = Owner;
1029 if (!isFriend && D->isOutOfLine()) {
1030 assert(D->getDeclContext()->isFileContext());
1031 LexicalDC = D->getDeclContext();
1032 }
1033
1034 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Douglas Gregore53060f2009-06-25 22:08:12 +00001036 // Attach the parameters
1037 for (unsigned P = 0; P < Params.size(); ++P)
1038 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001039 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001040
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001041 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001042 if (TemplateParams) {
1043 // Our resulting instantiation is actually a function template, since we
1044 // are substituting only the outer template parameters. For example, given
1045 //
1046 // template<typename T>
1047 // struct X {
1048 // template<typename U> friend void f(T, U);
1049 // };
1050 //
1051 // X<int> x;
1052 //
1053 // We are instantiating the friend function template "f" within X<int>,
1054 // which means substituting int for T, but leaving "f" as a friend function
1055 // template.
1056 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001057 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001058 Function->getLocation(),
1059 Function->getDeclName(),
1060 TemplateParams, Function);
1061 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001062
1063 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001064
1065 if (isFriend && D->isThisDeclarationADefinition()) {
1066 // TODO: should we remember this connection regardless of whether
1067 // the friend declaration provided a body?
1068 FunctionTemplate->setInstantiatedFromMemberTemplate(
1069 D->getDescribedFunctionTemplate());
1070 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001071 } else if (FunctionTemplate) {
1072 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001073 std::pair<const TemplateArgument *, unsigned> Innermost
1074 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001075 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001076 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1077 Innermost.first,
1078 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001079 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001080 } else if (isFriend && D->isThisDeclarationADefinition()) {
1081 // TODO: should we remember this connection regardless of whether
1082 // the friend declaration provided a body?
1083 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001084 }
Douglas Gregora735b202009-10-13 14:39:41 +00001085
Douglas Gregore53060f2009-06-25 22:08:12 +00001086 if (InitFunctionInstantiation(Function, D))
1087 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Douglas Gregore53060f2009-06-25 22:08:12 +00001089 bool Redeclaration = false;
1090 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001091 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001092
John McCall68263142009-11-18 22:49:29 +00001093 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1094 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1095
John McCallaf2094e2010-04-08 09:05:18 +00001096 if (DependentFunctionTemplateSpecializationInfo *Info
1097 = D->getDependentSpecializationInfo()) {
1098 assert(isFriend && "non-friend has dependent specialization info?");
1099
1100 // This needs to be set now for future sanity.
1101 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1102
1103 // Instantiate the explicit template arguments.
1104 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1105 Info->getRAngleLoc());
1106 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1107 TemplateArgumentLoc Loc;
1108 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1109 return 0;
1110
1111 ExplicitArgs.addArgument(Loc);
1112 }
1113
1114 // Map the candidate templates to their instantiations.
1115 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1116 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1117 Info->getTemplate(I),
1118 TemplateArgs);
1119 if (!Temp) return 0;
1120
1121 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1122 }
1123
1124 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1125 &ExplicitArgs,
1126 Previous))
1127 Function->setInvalidDecl();
1128
1129 isExplicitSpecialization = true;
1130
1131 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001132 // Look only into the namespace where the friend would be declared to
1133 // find a previous declaration. This is the innermost enclosing namespace,
1134 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001135 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001136
Douglas Gregora735b202009-10-13 14:39:41 +00001137 // In C++, the previous declaration we find might be a tag type
1138 // (class or enum). In this case, the new declaration will hide the
1139 // tag type. Note that this does does not apply if we're declaring a
1140 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001141 if (Previous.isSingleTagDecl())
1142 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001143 }
1144
John McCall9f54ad42009-12-10 09:41:52 +00001145 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001146 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001147 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001148
John McCall76d32642010-04-24 01:30:58 +00001149 NamedDecl *PrincipalDecl = (TemplateParams
1150 ? cast<NamedDecl>(FunctionTemplate)
1151 : Function);
1152
Douglas Gregora735b202009-10-13 14:39:41 +00001153 // If the original function was part of a friend declaration,
1154 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001155 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001156 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001157 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001158 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001159 else
Douglas Gregora735b202009-10-13 14:39:41 +00001160 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001161
1162 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1163 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001164
Gabor Greif77535df2010-08-30 22:25:56 +00001165 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001166
Douglas Gregor238058c2010-05-18 05:45:02 +00001167 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1168 D->isThisDeclarationADefinition()) {
1169 // Check for a function body.
1170 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001171 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001172 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1173 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1174 << Function->getDeclName();
1175 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1176 Function->setInvalidDecl();
1177 }
1178 // Check for redefinitions due to other instantiations of this or
1179 // a similar friend function.
1180 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1181 REnd = Function->redecls_end();
1182 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001183 if (*R == Function)
1184 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001185 switch (R->getFriendObjectKind()) {
1186 case Decl::FOK_None:
1187 if (!queuedInstantiation && R->isUsed(false)) {
1188 if (MemberSpecializationInfo *MSInfo
1189 = Function->getMemberSpecializationInfo()) {
1190 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1191 SourceLocation Loc = R->getLocation(); // FIXME
1192 MSInfo->setPointOfInstantiation(Loc);
1193 SemaRef.PendingLocalImplicitInstantiations.push_back(
1194 std::make_pair(Function, Loc));
1195 queuedInstantiation = true;
1196 }
1197 }
1198 }
1199 break;
1200 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001201 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001202 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001203 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001204 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1205 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001206 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001207 Function->setInvalidDecl();
1208 break;
1209 }
1210 }
1211 }
1212 }
Douglas Gregora735b202009-10-13 14:39:41 +00001213 }
1214
John McCall76d32642010-04-24 01:30:58 +00001215 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1216 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1217 PrincipalDecl->setNonMemberOperator();
1218
Douglas Gregore53060f2009-06-25 22:08:12 +00001219 return Function;
1220}
1221
Douglas Gregord60e1052009-08-27 16:57:43 +00001222Decl *
1223TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1224 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001225 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1226 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001227 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001228 // We are creating a function template specialization from a function
1229 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001230 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001231 std::pair<const TemplateArgument *, unsigned> Innermost
1232 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001234 FunctionDecl *SpecFunc
1235 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1236 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregor6b906862009-08-21 00:16:32 +00001238 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001239 if (SpecFunc)
1240 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001241 }
1242
John McCallb0cb0222010-03-27 05:57:59 +00001243 bool isFriend;
1244 if (FunctionTemplate)
1245 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1246 else
1247 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1248
Douglas Gregor79c22782010-01-16 20:21:20 +00001249 bool MergeWithParentScope = (TemplateParams != 0) ||
1250 !(isa<Decl>(Owner) &&
1251 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001252 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001253
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001254 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001255 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1256 TInfo = SubstFunctionType(D, Params);
1257 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001258 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001259 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001260
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001261 // \brief If the type of this function is not *directly* a function
1262 // type, then we're instantiating the a function that was declared
1263 // via a typedef, e.g.,
1264 //
1265 // typedef int functype(int, int);
1266 // functype func;
1267 //
1268 // In this case, we'll just go instantiate the ParmVarDecls that we
1269 // synthesized in the method declaration.
1270 if (!isa<FunctionProtoType>(T)) {
1271 assert(!Params.size() && "Instantiating type could not yield parameters");
1272 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1273 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1274 TemplateArgs);
1275 if (!P)
1276 return 0;
1277
1278 Params.push_back(P);
1279 }
1280 }
1281
John McCallb0cb0222010-03-27 05:57:59 +00001282 NestedNameSpecifier *Qualifier = D->getQualifier();
1283 if (Qualifier) {
1284 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1285 D->getQualifierRange(),
1286 TemplateArgs);
1287 if (!Qualifier) return 0;
1288 }
1289
1290 DeclContext *DC = Owner;
1291 if (isFriend) {
1292 if (Qualifier) {
1293 CXXScopeSpec SS;
1294 SS.setScopeRep(Qualifier);
1295 SS.setRange(D->getQualifierRange());
1296 DC = SemaRef.computeDeclContext(SS);
1297 } else {
1298 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1299 D->getDeclContext(),
1300 TemplateArgs);
1301 }
1302 if (!DC) return 0;
1303 }
1304
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001305 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001306 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001307 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Abramo Bagnara25777432010-08-11 22:01:17 +00001309 DeclarationNameInfo NameInfo
1310 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001311 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001312 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001313 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001314 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001315 Constructor->isInlineSpecified(),
1316 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001317 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001318 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001319 NameInfo, T,
1320 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001321 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001322 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001323 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001324 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001325 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001326 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001327 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001328 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1329 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001330 D->isStatic(),
1331 D->getStorageClassAsWritten(),
1332 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001333 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001334
John McCallb0cb0222010-03-27 05:57:59 +00001335 if (Qualifier)
1336 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001337
Douglas Gregord60e1052009-08-27 16:57:43 +00001338 if (TemplateParams) {
1339 // Our resulting instantiation is actually a function template, since we
1340 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001341 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001342 // template<typename T>
1343 // struct X {
1344 // template<typename U> void f(T, U);
1345 // };
1346 //
1347 // X<int> x;
1348 //
1349 // We are instantiating the member template "f" within X<int>, which means
1350 // substituting int for T, but leaving "f" as a member function template.
1351 // Build the function template itself.
1352 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1353 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001354 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001355 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001356 if (isFriend) {
1357 FunctionTemplate->setLexicalDeclContext(Owner);
1358 FunctionTemplate->setObjectOfFriendDecl(true);
1359 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001360 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001361 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001362 } else if (FunctionTemplate) {
1363 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001364 std::pair<const TemplateArgument *, unsigned> Innermost
1365 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001366 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001367 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1368 Innermost.first,
1369 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001370 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001371 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001372 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001373 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001374 }
1375
Mike Stump1eb44332009-09-09 15:08:12 +00001376 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001377 // out-of-line, the instantiation will have the same lexical
1378 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001379 if (isFriend) {
1380 Method->setLexicalDeclContext(Owner);
1381 Method->setObjectOfFriendDecl(true);
1382 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001383 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Douglas Gregor5545e162009-03-24 00:38:23 +00001385 // Attach the parameters
1386 for (unsigned P = 0; P < Params.size(); ++P)
1387 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001388 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001389
1390 if (InitMethodInstantiation(Method, D))
1391 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001392
Abramo Bagnara25777432010-08-11 22:01:17 +00001393 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1394 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001395
John McCallb0cb0222010-03-27 05:57:59 +00001396 if (!FunctionTemplate || TemplateParams || isFriend) {
1397 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregordec06662009-08-21 18:42:58 +00001399 // In C++, the previous declaration we find might be a tag type
1400 // (class or enum). In this case, the new declaration will hide the
1401 // tag type. Note that this does does not apply if we're declaring a
1402 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001403 if (Previous.isSingleTagDecl())
1404 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001405 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001406
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001407 bool Redeclaration = false;
1408 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001409 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001410 /*FIXME:*/OverloadableAttrRequired);
1411
Douglas Gregor4ba31362009-12-01 17:24:26 +00001412 if (D->isPure())
1413 SemaRef.CheckPureMethod(Method, SourceRange());
1414
John McCall46460a62010-01-20 21:53:11 +00001415 Method->setAccess(D->getAccess());
1416
John McCallb0cb0222010-03-27 05:57:59 +00001417 if (FunctionTemplate) {
1418 // If there's a function template, let our caller handle it.
1419 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1420 // Don't hide a (potentially) valid declaration with an invalid one.
1421 } else {
1422 NamedDecl *DeclToAdd = (TemplateParams
1423 ? cast<NamedDecl>(FunctionTemplate)
1424 : Method);
1425 if (isFriend)
1426 Record->makeDeclVisibleInContext(DeclToAdd);
1427 else
1428 Owner->addDecl(DeclToAdd);
1429 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001430
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001431 return Method;
1432}
1433
Douglas Gregor615c5d42009-03-24 16:43:20 +00001434Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001435 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001436}
1437
Douglas Gregor03b2b072009-03-24 00:15:49 +00001438Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001439 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001440}
1441
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001442Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001443 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001444}
1445
Douglas Gregor6477b692009-03-25 15:04:13 +00001446ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001447 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001448}
1449
John McCalle29ba202009-08-20 01:44:21 +00001450Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1451 TemplateTypeParmDecl *D) {
1452 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001453 const Type* T = D->getTypeForDecl();
1454 assert(T->isTemplateTypeParmType());
1455 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001456
John McCalle29ba202009-08-20 01:44:21 +00001457 TemplateTypeParmDecl *Inst =
1458 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001459 TTPT->getDepth() - TemplateArgs.getNumLevels(),
1460 TTPT->getIndex(),TTPT->getName(),
John McCalle29ba202009-08-20 01:44:21 +00001461 D->wasDeclaredWithTypename(),
1462 D->isParameterPack());
1463
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001464 if (D->hasDefaultArgument())
1465 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001466
Douglas Gregor550d9b22009-10-31 17:21:17 +00001467 // Introduce this template parameter's instantiation into the instantiation
1468 // scope.
1469 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1470
John McCalle29ba202009-08-20 01:44:21 +00001471 return Inst;
1472}
1473
Douglas Gregor33642df2009-10-23 23:25:44 +00001474Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1475 NonTypeTemplateParmDecl *D) {
1476 // Substitute into the type of the non-type template parameter.
1477 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001478 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001479 if (DI) {
1480 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1481 D->getDeclName());
1482 if (DI) T = DI->getType();
1483 } else {
1484 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1485 D->getDeclName());
1486 DI = 0;
1487 }
1488 if (T.isNull())
1489 return 0;
1490
1491 // Check that this type is acceptable for a non-type template parameter.
1492 bool Invalid = false;
1493 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1494 if (T.isNull()) {
1495 T = SemaRef.Context.IntTy;
1496 Invalid = true;
1497 }
1498
1499 NonTypeTemplateParmDecl *Param
1500 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001501 D->getDepth() - TemplateArgs.getNumLevels(),
1502 D->getPosition(), D->getIdentifier(), T,
1503 DI);
Douglas Gregor33642df2009-10-23 23:25:44 +00001504 if (Invalid)
1505 Param->setInvalidDecl();
1506
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001507 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001508
1509 // Introduce this template parameter's instantiation into the instantiation
1510 // scope.
1511 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001512 return Param;
1513}
1514
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001515Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001516TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1517 TemplateTemplateParmDecl *D) {
1518 // Instantiate the template parameter list of the template template parameter.
1519 TemplateParameterList *TempParams = D->getTemplateParameters();
1520 TemplateParameterList *InstParams;
1521 {
1522 // Perform the actual substitution of template parameters within a new,
1523 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001524 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001525 InstParams = SubstTemplateParams(TempParams);
1526 if (!InstParams)
1527 return NULL;
1528 }
1529
1530 // Build the template template parameter.
1531 TemplateTemplateParmDecl *Param
1532 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001533 D->getDepth() - TemplateArgs.getNumLevels(),
1534 D->getPosition(), D->getIdentifier(),
1535 InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001536 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001537
Douglas Gregor9106ef72009-11-11 16:58:32 +00001538 // Introduce this template parameter's instantiation into the instantiation
1539 // scope.
1540 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1541
1542 return Param;
1543}
1544
Douglas Gregor48c32a72009-11-17 06:07:40 +00001545Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1546 // Using directives are never dependent, so they require no explicit
1547
1548 UsingDirectiveDecl *Inst
1549 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1550 D->getNamespaceKeyLocation(),
1551 D->getQualifierRange(), D->getQualifier(),
1552 D->getIdentLocation(),
1553 D->getNominatedNamespace(),
1554 D->getCommonAncestor());
1555 Owner->addDecl(Inst);
1556 return Inst;
1557}
1558
John McCalled976492009-12-04 22:46:56 +00001559Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1560 // The nested name specifier is non-dependent, so no transformation
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001561 // is required. The same holds for the name info.
1562 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001563
John McCall9f54ad42009-12-10 09:41:52 +00001564 // We only need to do redeclaration lookups if we're in a class
1565 // scope (in fact, it's not really even possible in non-class
1566 // scopes).
1567 bool CheckRedeclaration = Owner->isRecord();
1568
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001569 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1570 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001571
John McCalled976492009-12-04 22:46:56 +00001572 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001573 D->getNestedNameRange(),
1574 D->getUsingLocation(),
1575 D->getTargetNestedNameDecl(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001576 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001577 D->isTypeName());
1578
1579 CXXScopeSpec SS;
1580 SS.setScopeRep(D->getTargetNestedNameDecl());
1581 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001582
1583 if (CheckRedeclaration) {
1584 Prev.setHideTags(false);
1585 SemaRef.LookupQualifiedName(Prev, Owner);
1586
1587 // Check for invalid redeclarations.
1588 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1589 D->isTypeName(), SS,
1590 D->getLocation(), Prev))
1591 NewUD->setInvalidDecl();
1592
1593 }
1594
1595 if (!NewUD->isInvalidDecl() &&
1596 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001597 D->getLocation()))
1598 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001599
John McCalled976492009-12-04 22:46:56 +00001600 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1601 NewUD->setAccess(D->getAccess());
1602 Owner->addDecl(NewUD);
1603
John McCall9f54ad42009-12-10 09:41:52 +00001604 // Don't process the shadow decls for an invalid decl.
1605 if (NewUD->isInvalidDecl())
1606 return NewUD;
1607
John McCall323c3102009-12-22 22:26:37 +00001608 bool isFunctionScope = Owner->isFunctionOrMethod();
1609
John McCall9f54ad42009-12-10 09:41:52 +00001610 // Process the shadow decls.
1611 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1612 I != E; ++I) {
1613 UsingShadowDecl *Shadow = *I;
1614 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001615 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1616 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001617 TemplateArgs));
1618
1619 if (CheckRedeclaration &&
1620 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1621 continue;
1622
1623 UsingShadowDecl *InstShadow
1624 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1625 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001626
1627 if (isFunctionScope)
1628 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001629 }
John McCalled976492009-12-04 22:46:56 +00001630
1631 return NewUD;
1632}
1633
1634Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001635 // Ignore these; we handle them in bulk when processing the UsingDecl.
1636 return 0;
John McCalled976492009-12-04 22:46:56 +00001637}
1638
John McCall7ba107a2009-11-18 02:36:19 +00001639Decl * TemplateDeclInstantiator
1640 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001641 NestedNameSpecifier *NNS =
1642 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1643 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001644 TemplateArgs);
1645 if (!NNS)
1646 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001648 CXXScopeSpec SS;
1649 SS.setRange(D->getTargetNestedNameRange());
1650 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001652 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1653 // Hence, no tranformation is required for it.
1654 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001655 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001656 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001657 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001658 /*instantiation*/ true,
1659 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001660 if (UD)
John McCalled976492009-12-04 22:46:56 +00001661 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1662
John McCall7ba107a2009-11-18 02:36:19 +00001663 return UD;
1664}
1665
1666Decl * TemplateDeclInstantiator
1667 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1668 NestedNameSpecifier *NNS =
1669 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1670 D->getTargetNestedNameRange(),
1671 TemplateArgs);
1672 if (!NNS)
1673 return 0;
1674
1675 CXXScopeSpec SS;
1676 SS.setRange(D->getTargetNestedNameRange());
1677 SS.setScopeRep(NNS);
1678
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001679 DeclarationNameInfo NameInfo
1680 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1681
John McCall7ba107a2009-11-18 02:36:19 +00001682 NamedDecl *UD =
1683 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001684 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001685 /*instantiation*/ true,
1686 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001687 if (UD)
John McCalled976492009-12-04 22:46:56 +00001688 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1689
Anders Carlsson0d8df782009-08-29 19:37:28 +00001690 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001691}
1692
John McCallce3ff2b2009-08-25 22:02:44 +00001693Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001694 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001695 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001696 if (D->isInvalidDecl())
1697 return 0;
1698
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001699 return Instantiator.Visit(D);
1700}
1701
John McCalle29ba202009-08-20 01:44:21 +00001702/// \brief Instantiates a nested template parameter list in the current
1703/// instantiation context.
1704///
1705/// \param L The parameter list to instantiate
1706///
1707/// \returns NULL if there was an error
1708TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001709TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001710 // Get errors for all the parameters before bailing out.
1711 bool Invalid = false;
1712
1713 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001714 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001715 ParamVector Params;
1716 Params.reserve(N);
1717 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1718 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001719 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001720 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001721 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001722 }
1723
1724 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001725 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001726 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001727
1728 TemplateParameterList *InstL
1729 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1730 L->getLAngleLoc(), &Params.front(), N,
1731 L->getRAngleLoc());
1732 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001733}
John McCalle29ba202009-08-20 01:44:21 +00001734
Douglas Gregored9c0f92009-10-29 00:04:11 +00001735/// \brief Instantiate the declaration of a class template partial
1736/// specialization.
1737///
1738/// \param ClassTemplate the (instantiated) class template that is partially
1739// specialized by the instantiation of \p PartialSpec.
1740///
1741/// \param PartialSpec the (uninstantiated) class template partial
1742/// specialization that we are instantiating.
1743///
1744/// \returns true if there was an error, false otherwise.
1745bool
1746TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1747 ClassTemplateDecl *ClassTemplate,
1748 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001749 // Create a local instantiation scope for this class template partial
1750 // specialization, which will contain the instantiations of the template
1751 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001752 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001753
Douglas Gregored9c0f92009-10-29 00:04:11 +00001754 // Substitute into the template parameters of the class template partial
1755 // specialization.
1756 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1757 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1758 if (!InstParams)
1759 return true;
1760
1761 // Substitute into the template arguments of the class template partial
1762 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001763 const TemplateArgumentLoc *PartialSpecTemplateArgs
1764 = PartialSpec->getTemplateArgsAsWritten();
1765 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1766
John McCalld5532b62009-11-23 01:53:49 +00001767 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001768 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001769 TemplateArgumentLoc Loc;
1770 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001771 return true;
John McCalld5532b62009-11-23 01:53:49 +00001772 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001773 }
1774
1775
1776 // Check that the template argument list is well-formed for this
1777 // class template.
1778 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1779 InstTemplateArgs.size());
1780 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1781 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001782 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001783 false,
1784 Converted))
1785 return true;
1786
1787 // Figure out where to insert this class template partial specialization
1788 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001789 void *InsertPos = 0;
1790 ClassTemplateSpecializationDecl *PrevDecl
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001791 = ClassTemplate->findPartialSpecialization(Converted.getFlatArguments(),
1792 Converted.flatSize(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001793
1794 // Build the canonical type that describes the converted template
1795 // arguments of the class template partial specialization.
1796 QualType CanonType
1797 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1798 Converted.getFlatArguments(),
1799 Converted.flatSize());
1800
1801 // Build the fully-sugared type for this class template
1802 // specialization as the user wrote in the specialization
1803 // itself. This means that we'll pretty-print the type retrieved
1804 // from the specialization's declaration the way that the user
1805 // actually wrote the specialization, rather than formatting the
1806 // name based on the "canonical" representation used to store the
1807 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001808 TypeSourceInfo *WrittenTy
1809 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1810 TemplateName(ClassTemplate),
1811 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001812 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001813 CanonType);
1814
1815 if (PrevDecl) {
1816 // We've already seen a partial specialization with the same template
1817 // parameters and template arguments. This can happen, for example, when
1818 // substituting the outer template arguments ends up causing two
1819 // class template partial specializations of a member class template
1820 // to have identical forms, e.g.,
1821 //
1822 // template<typename T, typename U>
1823 // struct Outer {
1824 // template<typename X, typename Y> struct Inner;
1825 // template<typename Y> struct Inner<T, Y>;
1826 // template<typename Y> struct Inner<U, Y>;
1827 // };
1828 //
1829 // Outer<int, int> outer; // error: the partial specializations of Inner
1830 // // have the same signature.
1831 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1832 << WrittenTy;
1833 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1834 << SemaRef.Context.getTypeDeclType(PrevDecl);
1835 return true;
1836 }
1837
1838
1839 // Create the class template partial specialization declaration.
1840 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001841 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1842 PartialSpec->getTagKind(),
1843 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001844 PartialSpec->getLocation(),
1845 InstParams,
1846 ClassTemplate,
1847 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001848 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001849 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001850 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001851 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001852 // Substitute the nested name specifier, if any.
1853 if (SubstQualifier(PartialSpec, InstPartialSpec))
1854 return 0;
1855
Douglas Gregored9c0f92009-10-29 00:04:11 +00001856 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001857 InstPartialSpec->setTypeAsWritten(WrittenTy);
1858
Douglas Gregored9c0f92009-10-29 00:04:11 +00001859 // Add this partial specialization to the set of class template partial
1860 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001861 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001862 return false;
1863}
1864
John McCall21ef0fa2010-03-11 09:03:00 +00001865TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001866TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001867 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001868 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1869 assert(OldTInfo && "substituting function without type source info");
1870 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001871 TypeSourceInfo *NewTInfo
1872 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1873 D->getTypeSpecStartLoc(),
1874 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001875 if (!NewTInfo)
1876 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001877
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001878 if (NewTInfo != OldTInfo) {
1879 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001880 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001881 if (FunctionProtoTypeLoc *OldProtoLoc
1882 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1883 TypeLoc NewTL = NewTInfo->getTypeLoc();
1884 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1885 assert(NewProtoLoc && "Missing prototype?");
1886 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1887 // FIXME: Variadic templates will break this.
1888 Params.push_back(NewProtoLoc->getArg(i));
1889 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001890 OldProtoLoc->getArg(i),
1891 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001892 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001893 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001894 } else {
1895 // The function type itself was not dependent and therefore no
1896 // substitution occurred. However, we still need to instantiate
1897 // the function parameters themselves.
1898 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001899 if (FunctionProtoTypeLoc *OldProtoLoc
1900 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1901 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1902 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1903 if (!Parm)
1904 return 0;
1905 Params.push_back(Parm);
1906 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001907 }
1908 }
John McCall21ef0fa2010-03-11 09:03:00 +00001909 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001910}
1911
Mike Stump1eb44332009-09-09 15:08:12 +00001912/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001913/// declaration (New) from the corresponding fields of its template (Tmpl).
1914///
1915/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001916bool
1917TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001918 FunctionDecl *Tmpl) {
1919 if (Tmpl->isDeleted())
1920 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Douglas Gregorcca9e962009-07-01 22:01:06 +00001922 // If we are performing substituting explicitly-specified template arguments
1923 // or deduced template arguments into a function template and we reach this
1924 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001925 // to keeping the new function template specialization. We therefore
1926 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001927 // into a template instantiation for this specific function template
1928 // specialization, which is not a SFINAE context, so that we diagnose any
1929 // further errors in the declaration itself.
1930 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1931 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1932 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1933 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001934 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001935 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001936 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001937 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001938 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001939 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1940 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001941 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001942 }
1943 }
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001945 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1946 assert(Proto && "Function template without prototype?");
1947
1948 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1949 Proto->getNoReturnAttr()) {
1950 // The function has an exception specification or a "noreturn"
1951 // attribute. Substitute into each of the exception types.
1952 llvm::SmallVector<QualType, 4> Exceptions;
1953 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1954 // FIXME: Poor location information!
1955 QualType T
1956 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1957 New->getLocation(), New->getDeclName());
1958 if (T.isNull() ||
1959 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1960 continue;
1961
1962 Exceptions.push_back(T);
1963 }
1964
1965 // Rebuild the function type
1966
1967 const FunctionProtoType *NewProto
1968 = New->getType()->getAs<FunctionProtoType>();
1969 assert(NewProto && "Template instantiation without function prototype?");
1970 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1971 NewProto->arg_type_begin(),
1972 NewProto->getNumArgs(),
1973 NewProto->isVariadic(),
1974 NewProto->getTypeQuals(),
1975 Proto->hasExceptionSpec(),
1976 Proto->hasAnyExceptionSpec(),
1977 Exceptions.size(),
1978 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001979 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001980 }
1981
John McCall1d8d1cc2010-08-01 02:01:53 +00001982 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001983
Douglas Gregore53060f2009-06-25 22:08:12 +00001984 return false;
1985}
1986
Douglas Gregor5545e162009-03-24 00:38:23 +00001987/// \brief Initializes common fields of an instantiated method
1988/// declaration (New) from the corresponding fields of its template
1989/// (Tmpl).
1990///
1991/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001992bool
1993TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001994 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001995 if (InitFunctionInstantiation(New, Tmpl))
1996 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Douglas Gregor5545e162009-03-24 00:38:23 +00001998 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1999 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002000 if (Tmpl->isVirtualAsWritten())
2001 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00002002
2003 // FIXME: attributes
2004 // FIXME: New needs a pointer to Tmpl
2005 return false;
2006}
Douglas Gregora58861f2009-05-13 20:28:22 +00002007
2008/// \brief Instantiate the definition of the given function from its
2009/// template.
2010///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002011/// \param PointOfInstantiation the point at which the instantiation was
2012/// required. Note that this is not precisely a "point of instantiation"
2013/// for the function, but it's close.
2014///
Douglas Gregora58861f2009-05-13 20:28:22 +00002015/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002016/// function template specialization or member function of a class template
2017/// specialization.
2018///
2019/// \param Recursive if true, recursively instantiates any functions that
2020/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002021///
2022/// \param DefinitionRequired if true, then we are performing an explicit
2023/// instantiation where the body of the function is required. Complain if
2024/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002025void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002026 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002027 bool Recursive,
2028 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002029 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002030 return;
2031
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002032 // Never instantiate an explicit specialization.
2033 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2034 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002035
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002036 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002037 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002038 Stmt *Pattern = 0;
2039 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002040 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002041
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002042 if (!Pattern) {
2043 if (DefinitionRequired) {
2044 if (Function->getPrimaryTemplate())
2045 Diag(PointOfInstantiation,
2046 diag::err_explicit_instantiation_undefined_func_template)
2047 << Function->getPrimaryTemplate();
2048 else
2049 Diag(PointOfInstantiation,
2050 diag::err_explicit_instantiation_undefined_member)
2051 << 1 << Function->getDeclName() << Function->getDeclContext();
2052
2053 if (PatternDecl)
2054 Diag(PatternDecl->getLocation(),
2055 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002056 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002057 } else if (Function->getTemplateSpecializationKind()
2058 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002059 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002060 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002061 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002062
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002063 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002064 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002065
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002066 // C++0x [temp.explicit]p9:
2067 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002068 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002069 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002070 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002071 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002072 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002073 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002075 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2076 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002077 return;
2078
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002079 // If we're performing recursive template instantiation, create our own
2080 // queue of pending implicit instantiations that we will instantiate later,
2081 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002082 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002083 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002084 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002085
Douglas Gregor9679caf2010-05-12 17:27:19 +00002086 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002087 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002088 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002089
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002090 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002091 // recorded, unless we're actually a member function within a local
2092 // class, in which case we need to merge our results with the parent
2093 // scope (of the enclosing function).
2094 bool MergeWithParentScope = false;
2095 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2096 MergeWithParentScope = Rec->isLocalClass();
2097
2098 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002099
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002100 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002101 // instantiation scope, and set the parameter names to those used
2102 // in the template.
2103 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2104 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2105 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2106 FunctionParam->setDeclName(PatternParam->getDeclName());
2107 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2108 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002109
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002110 // Enter the scope of this instantiation. We don't use
2111 // PushDeclContext because we don't have a scope.
2112 DeclContext *PreviousContext = CurContext;
2113 CurContext = Function;
2114
Mike Stump1eb44332009-09-09 15:08:12 +00002115 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002116 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002117
2118 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002119 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002120 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2121 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2122 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002123 }
2124
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002125 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002126 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002127
Douglas Gregor52604ab2009-09-11 21:19:12 +00002128 if (Body.isInvalid())
2129 Function->setInvalidDecl();
2130
John McCall9ae2f072010-08-23 23:25:46 +00002131 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002132 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002133
John McCall0c01d182010-03-24 05:22:00 +00002134 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2135
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002136 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002137
2138 DeclGroupRef DG(Function);
2139 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Douglas Gregor60406be2010-01-16 22:29:39 +00002141 // This class may have local implicit instantiations that need to be
2142 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002143 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002144 Scope.Exit();
2145
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002146 if (Recursive) {
2147 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002148 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002149 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002151 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002152 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002153 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002154}
2155
2156/// \brief Instantiate the definition of the given variable from its
2157/// template.
2158///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002159/// \param PointOfInstantiation the point at which the instantiation was
2160/// required. Note that this is not precisely a "point of instantiation"
2161/// for the function, but it's close.
2162///
2163/// \param Var the already-instantiated declaration of a static member
2164/// variable of a class template specialization.
2165///
2166/// \param Recursive if true, recursively instantiates any functions that
2167/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002168///
2169/// \param DefinitionRequired if true, then we are performing an explicit
2170/// instantiation where an out-of-line definition of the member variable
2171/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002172void Sema::InstantiateStaticDataMemberDefinition(
2173 SourceLocation PointOfInstantiation,
2174 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002175 bool Recursive,
2176 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002177 if (Var->isInvalidDecl())
2178 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Douglas Gregor7caa6822009-07-24 20:34:43 +00002180 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002181 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002182 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002183 assert(Def->isStaticDataMember() && "Not a static data member?");
2184 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregor0d035142009-10-27 18:42:08 +00002186 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002187 // We did not find an out-of-line definition of this static data member,
2188 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002189 // instantiate this definition (or provide a specialization for it) in
2190 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002191 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002192 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002193 Diag(PointOfInstantiation,
2194 diag::err_explicit_instantiation_undefined_member)
2195 << 2 << Var->getDeclName() << Var->getDeclContext();
2196 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002197 } else if (Var->getTemplateSpecializationKind()
2198 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002199 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002200 std::make_pair(Var, PointOfInstantiation));
2201 }
2202
Douglas Gregor7caa6822009-07-24 20:34:43 +00002203 return;
2204 }
2205
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002206 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002207 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002208 return;
2209
2210 // C++0x [temp.explicit]p9:
2211 // Except for inline functions, other explicit instantiation declarations
2212 // have the effect of suppressing the implicit instantiation of the entity
2213 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002214 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002215 == TSK_ExplicitInstantiationDeclaration)
2216 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002217
Douglas Gregor7caa6822009-07-24 20:34:43 +00002218 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2219 if (Inst)
2220 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002221
Douglas Gregor7caa6822009-07-24 20:34:43 +00002222 // If we're performing recursive template instantiation, create our own
2223 // queue of pending implicit instantiations that we will instantiate later,
2224 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002225 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002226 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002227 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Douglas Gregor7caa6822009-07-24 20:34:43 +00002229 // Enter the scope of this instantiation. We don't use
2230 // PushDeclContext because we don't have a scope.
2231 DeclContext *PreviousContext = CurContext;
2232 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002234 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002235 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002236 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002237 CurContext = PreviousContext;
2238
2239 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002240 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2241 assert(MSInfo && "Missing member specialization information?");
2242 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2243 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002244 DeclGroupRef DG(Var);
2245 Consumer.HandleTopLevelDecl(DG);
2246 }
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Douglas Gregor7caa6822009-07-24 20:34:43 +00002248 if (Recursive) {
2249 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002250 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002251 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Douglas Gregor7caa6822009-07-24 20:34:43 +00002253 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002254 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002255 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002256}
Douglas Gregor815215d2009-05-27 05:35:12 +00002257
Anders Carlsson09025312009-08-29 05:16:22 +00002258void
2259Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2260 const CXXConstructorDecl *Tmpl,
2261 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002262
Anders Carlsson09025312009-08-29 05:16:22 +00002263 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002264 bool AnyErrors = false;
2265
Anders Carlsson09025312009-08-29 05:16:22 +00002266 // Instantiate all the initializers.
2267 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002268 InitsEnd = Tmpl->init_end();
2269 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002270 CXXBaseOrMemberInitializer *Init = *Inits;
2271
Chandler Carruth030ef472010-09-03 21:54:20 +00002272 // Only instantiate written initializers, let Sema re-construct implicit
2273 // ones.
2274 if (!Init->isWritten())
2275 continue;
2276
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002277 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002278 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002280 // Instantiate the initializer.
2281 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002282 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002283 AnyErrors = true;
2284 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002285 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002286
Anders Carlsson09025312009-08-29 05:16:22 +00002287 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002288 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002289 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002290 TemplateArgs,
2291 Init->getSourceLocation(),
2292 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002293 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002294 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002295 New->setInvalidDecl();
2296 continue;
2297 }
2298
John McCalla93c9342009-12-07 02:54:59 +00002299 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002300 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002301 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002302 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002303 Init->getRParenLoc(),
2304 New->getParent());
2305 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002306 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002308 // Is this an anonymous union?
2309 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002310 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2311 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002312 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002313 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2314 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002315 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002316
2317 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002318 NewArgs.size(),
2319 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002320 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002321 Init->getRParenLoc());
2322 }
2323
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002324 if (NewInit.isInvalid()) {
2325 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002326 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002327 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002328 // FIXME: It would be nice if ASTOwningVector had a release function.
2329 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002330
Anders Carlsson09025312009-08-29 05:16:22 +00002331 NewInits.push_back((MemInitTy *)NewInit.get());
2332 }
2333 }
Mike Stump1eb44332009-09-09 15:08:12 +00002334
Anders Carlsson09025312009-08-29 05:16:22 +00002335 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002336 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002337 /*FIXME: ColonLoc */
2338 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002339 NewInits.data(), NewInits.size(),
2340 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002341}
2342
John McCall52a575a2009-08-29 08:11:13 +00002343// TODO: this could be templated if the various decl types used the
2344// same method name.
2345static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2346 ClassTemplateDecl *Instance) {
2347 Pattern = Pattern->getCanonicalDecl();
2348
2349 do {
2350 Instance = Instance->getCanonicalDecl();
2351 if (Pattern == Instance) return true;
2352 Instance = Instance->getInstantiatedFromMemberTemplate();
2353 } while (Instance);
2354
2355 return false;
2356}
2357
Douglas Gregor0d696532009-09-28 06:34:35 +00002358static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2359 FunctionTemplateDecl *Instance) {
2360 Pattern = Pattern->getCanonicalDecl();
2361
2362 do {
2363 Instance = Instance->getCanonicalDecl();
2364 if (Pattern == Instance) return true;
2365 Instance = Instance->getInstantiatedFromMemberTemplate();
2366 } while (Instance);
2367
2368 return false;
2369}
2370
Douglas Gregored9c0f92009-10-29 00:04:11 +00002371static bool
2372isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2373 ClassTemplatePartialSpecializationDecl *Instance) {
2374 Pattern
2375 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2376 do {
2377 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2378 Instance->getCanonicalDecl());
2379 if (Pattern == Instance)
2380 return true;
2381 Instance = Instance->getInstantiatedFromMember();
2382 } while (Instance);
2383
2384 return false;
2385}
2386
John McCall52a575a2009-08-29 08:11:13 +00002387static bool isInstantiationOf(CXXRecordDecl *Pattern,
2388 CXXRecordDecl *Instance) {
2389 Pattern = Pattern->getCanonicalDecl();
2390
2391 do {
2392 Instance = Instance->getCanonicalDecl();
2393 if (Pattern == Instance) return true;
2394 Instance = Instance->getInstantiatedFromMemberClass();
2395 } while (Instance);
2396
2397 return false;
2398}
2399
2400static bool isInstantiationOf(FunctionDecl *Pattern,
2401 FunctionDecl *Instance) {
2402 Pattern = Pattern->getCanonicalDecl();
2403
2404 do {
2405 Instance = Instance->getCanonicalDecl();
2406 if (Pattern == Instance) return true;
2407 Instance = Instance->getInstantiatedFromMemberFunction();
2408 } while (Instance);
2409
2410 return false;
2411}
2412
2413static bool isInstantiationOf(EnumDecl *Pattern,
2414 EnumDecl *Instance) {
2415 Pattern = Pattern->getCanonicalDecl();
2416
2417 do {
2418 Instance = Instance->getCanonicalDecl();
2419 if (Pattern == Instance) return true;
2420 Instance = Instance->getInstantiatedFromMemberEnum();
2421 } while (Instance);
2422
2423 return false;
2424}
2425
John McCalled976492009-12-04 22:46:56 +00002426static bool isInstantiationOf(UsingShadowDecl *Pattern,
2427 UsingShadowDecl *Instance,
2428 ASTContext &C) {
2429 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2430}
2431
2432static bool isInstantiationOf(UsingDecl *Pattern,
2433 UsingDecl *Instance,
2434 ASTContext &C) {
2435 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2436}
2437
John McCall7ba107a2009-11-18 02:36:19 +00002438static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2439 UsingDecl *Instance,
2440 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002441 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002442}
2443
2444static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002445 UsingDecl *Instance,
2446 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002447 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002448}
2449
John McCall52a575a2009-08-29 08:11:13 +00002450static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2451 VarDecl *Instance) {
2452 assert(Instance->isStaticDataMember());
2453
2454 Pattern = Pattern->getCanonicalDecl();
2455
2456 do {
2457 Instance = Instance->getCanonicalDecl();
2458 if (Pattern == Instance) return true;
2459 Instance = Instance->getInstantiatedFromStaticDataMember();
2460 } while (Instance);
2461
2462 return false;
2463}
2464
John McCalled976492009-12-04 22:46:56 +00002465// Other is the prospective instantiation
2466// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002467static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002468 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002469 if (UnresolvedUsingTypenameDecl *UUD
2470 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2471 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2472 return isInstantiationOf(UUD, UD, Ctx);
2473 }
2474 }
2475
2476 if (UnresolvedUsingValueDecl *UUD
2477 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002478 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2479 return isInstantiationOf(UUD, UD, Ctx);
2480 }
2481 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002482
Anders Carlsson0d8df782009-08-29 19:37:28 +00002483 return false;
2484 }
Mike Stump1eb44332009-09-09 15:08:12 +00002485
John McCall52a575a2009-08-29 08:11:13 +00002486 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2487 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002488
John McCall52a575a2009-08-29 08:11:13 +00002489 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2490 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002491
John McCall52a575a2009-08-29 08:11:13 +00002492 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2493 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002494
Douglas Gregor7caa6822009-07-24 20:34:43 +00002495 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002496 if (Var->isStaticDataMember())
2497 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2498
2499 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2500 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002501
Douglas Gregor0d696532009-09-28 06:34:35 +00002502 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2503 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2504
Douglas Gregored9c0f92009-10-29 00:04:11 +00002505 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2506 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2507 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2508 PartialSpec);
2509
Anders Carlssond8b285f2009-09-01 04:26:58 +00002510 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2511 if (!Field->getDeclName()) {
2512 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002513 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002514 cast<FieldDecl>(D);
2515 }
2516 }
Mike Stump1eb44332009-09-09 15:08:12 +00002517
John McCalled976492009-12-04 22:46:56 +00002518 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2519 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2520
2521 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2522 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2523
Douglas Gregor815215d2009-05-27 05:35:12 +00002524 return D->getDeclName() && isa<NamedDecl>(Other) &&
2525 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2526}
2527
2528template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002529static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002530 NamedDecl *D,
2531 ForwardIterator first,
2532 ForwardIterator last) {
2533 for (; first != last; ++first)
2534 if (isInstantiationOf(Ctx, D, *first))
2535 return cast<NamedDecl>(*first);
2536
2537 return 0;
2538}
2539
John McCall02cace72009-08-28 07:59:38 +00002540/// \brief Finds the instantiation of the given declaration context
2541/// within the current instantiation.
2542///
2543/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002544DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002545 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002546 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002547 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002548 return cast_or_null<DeclContext>(ID);
2549 } else return DC;
2550}
2551
Douglas Gregored961e72009-05-27 17:54:46 +00002552/// \brief Find the instantiation of the given declaration within the
2553/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002554///
2555/// This routine is intended to be used when \p D is a declaration
2556/// referenced from within a template, that needs to mapped into the
2557/// corresponding declaration within an instantiation. For example,
2558/// given:
2559///
2560/// \code
2561/// template<typename T>
2562/// struct X {
2563/// enum Kind {
2564/// KnownValue = sizeof(T)
2565/// };
2566///
2567/// bool getKind() const { return KnownValue; }
2568/// };
2569///
2570/// template struct X<int>;
2571/// \endcode
2572///
2573/// In the instantiation of X<int>::getKind(), we need to map the
2574/// EnumConstantDecl for KnownValue (which refers to
2575/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002576/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2577/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002578NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002579 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002580 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002581 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002582 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002583 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002584 // D is a local of some kind. Look into the map of local
2585 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002586 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002587 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002588
Douglas Gregore95b4092009-09-16 18:34:49 +00002589 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2590 if (!Record->isDependentContext())
2591 return D;
2592
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002593 // If the RecordDecl is actually the injected-class-name or a
2594 // "templated" declaration for a class template, class template
2595 // partial specialization, or a member class of a class template,
2596 // substitute into the injected-class-name of the class template
2597 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002598 QualType T;
2599 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2600
2601 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002602 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002603 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2604 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002605 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002606
2607 // If we call SubstType with an InjectedClassNameType here we
2608 // can end up in an infinite loop.
2609 T = Context.getTypeDeclType(Record);
2610 assert(isa<InjectedClassNameType>(T) &&
2611 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002612 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002613 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002614
2615 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002616 // Substitute into the injected-class-name to get the type
2617 // corresponding to the instantiation we want, which may also be
2618 // the current instantiation (if we're in a template
2619 // definition). This substitution should never fail, since we
2620 // know we can instantiate the injected-class-name or we
2621 // wouldn't have gotten to the injected-class-name!
2622
2623 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2624 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002625 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2626 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2627
2628 if (!T->isDependentType()) {
2629 assert(T->isRecordType() && "Instantiation must produce a record type");
2630 return T->getAs<RecordType>()->getDecl();
2631 }
2632
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002633 // We are performing "partial" template instantiation to create
2634 // the member declarations for the members of a class template
2635 // specialization. Therefore, D is actually referring to something
2636 // in the current instantiation. Look through the current
2637 // context, which contains actual instantiations, to find the
2638 // instantiation of the "current instantiation" that D refers
2639 // to.
2640 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002641 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002642 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002643 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002644 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002645 if (isInstantiationOf(ClassTemplate,
2646 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002647 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002648
2649 if (!DC->isDependentContext())
2650 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002651 }
2652
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002653 // We're performing "instantiation" of a member of the current
2654 // instantiation while we are type-checking the
2655 // definition. Compute the declaration context and return that.
2656 assert(!SawNonDependentContext &&
2657 "No dependent context while instantiating record");
2658 DeclContext *DC = computeDeclContext(T);
2659 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002660 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002661 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002662 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002663
Douglas Gregore95b4092009-09-16 18:34:49 +00002664 // Fall through to deal with other dependent record types (e.g.,
2665 // anonymous unions in class templates).
2666 }
John McCall52a575a2009-08-29 08:11:13 +00002667
Douglas Gregore95b4092009-09-16 18:34:49 +00002668 if (!ParentDC->isDependentContext())
2669 return D;
2670
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002671 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002672 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002673 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Douglas Gregor815215d2009-05-27 05:35:12 +00002675 if (ParentDC != D->getDeclContext()) {
2676 // We performed some kind of instantiation in the parent context,
2677 // so now we need to look into the instantiated parent context to
2678 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002679
John McCall3cb0ebd2010-03-10 03:28:59 +00002680 // If our context used to be dependent, we may need to instantiate
2681 // it before performing lookup into that context.
2682 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002683 if (!Spec->isDependentContext()) {
2684 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002685 const RecordType *Tag = T->getAs<RecordType>();
2686 assert(Tag && "type of non-dependent record is not a RecordType");
2687 if (!Tag->isBeingDefined() &&
2688 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2689 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002690 }
2691 }
2692
Douglas Gregor815215d2009-05-27 05:35:12 +00002693 NamedDecl *Result = 0;
2694 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002695 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002696 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2697 } else {
2698 // Since we don't have a name for the entity we're looking for,
2699 // our only option is to walk through all of the declarations to
2700 // find that name. This will occur in a few cases:
2701 //
2702 // - anonymous struct/union within a template
2703 // - unnamed class/struct/union/enum within a template
2704 //
2705 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002706 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002707 ParentDC->decls_begin(),
2708 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
John McCall9f54ad42009-12-10 09:41:52 +00002711 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002712 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2713 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002714 && "Unable to find instantiation of declaration!");
2715
Douglas Gregor815215d2009-05-27 05:35:12 +00002716 D = Result;
2717 }
2718
Douglas Gregor815215d2009-05-27 05:35:12 +00002719 return D;
2720}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002721
Mike Stump1eb44332009-09-09 15:08:12 +00002722/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002723/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002724void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002725 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00002726 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002727 PendingImplicitInstantiation Inst;
2728
2729 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002730 Inst = PendingInstantiations.front();
2731 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00002732 } else {
2733 Inst = PendingLocalImplicitInstantiations.front();
2734 PendingLocalImplicitInstantiations.pop_front();
2735 }
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Douglas Gregor7caa6822009-07-24 20:34:43 +00002737 // Instantiate function definitions
2738 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00002739 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2740 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00002741 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
2742 TSK_ExplicitInstantiationDefinition;
2743 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
2744 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002745 continue;
2746 }
Mike Stump1eb44332009-09-09 15:08:12 +00002747
Douglas Gregor7caa6822009-07-24 20:34:43 +00002748 // Instantiate static data member definitions.
2749 VarDecl *Var = cast<VarDecl>(Inst.first);
2750 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002751
Chandler Carruth291b4412010-02-13 10:17:50 +00002752 // Don't try to instantiate declarations if the most recent redeclaration
2753 // is invalid.
2754 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2755 continue;
2756
2757 // Check if the most recent declaration has changed the specialization kind
2758 // and removed the need for implicit instantiation.
2759 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2760 case TSK_Undeclared:
2761 assert(false && "Cannot instantitiate an undeclared specialization.");
2762 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00002763 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00002764 continue; // No longer need to instantiate this type.
2765 case TSK_ExplicitInstantiationDefinition:
2766 // We only need an instantiation if the pending instantiation *is* the
2767 // explicit instantiation.
2768 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00002769 case TSK_ImplicitInstantiation:
2770 break;
2771 }
2772
John McCallf312b1e2010-08-26 23:41:50 +00002773 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2774 "instantiating static data member "
2775 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Chandler Carruth58e390e2010-08-25 08:27:02 +00002777 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
2778 TSK_ExplicitInstantiationDefinition;
2779 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
2780 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002781 }
2782}
John McCall0c01d182010-03-24 05:22:00 +00002783
2784void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2785 const MultiLevelTemplateArgumentList &TemplateArgs) {
2786 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2787 E = Pattern->ddiag_end(); I != E; ++I) {
2788 DependentDiagnostic *DD = *I;
2789
2790 switch (DD->getKind()) {
2791 case DependentDiagnostic::Access:
2792 HandleDependentAccessCheck(*DD, TemplateArgs);
2793 break;
2794 }
2795 }
2796}
John McCallf312b1e2010-08-26 23:41:50 +00002797