blob: c4bcc1e7d909b167861e6cc9b99ab74216d11fd8 [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);
John McCall9a34edb2010-10-19 01:40:49 +0000553 FD->setUnsupportedFriend(D->isUnsupportedFriend());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000554 Owner->addDecl(FD);
555 return FD;
556 }
557
558 NamedDecl *ND = D->getFriendDecl();
559 assert(ND && "friend decl must be a decl or a type!");
560
John McCallaf2094e2010-04-08 09:05:18 +0000561 // All of the Visit implementations for the various potential friend
562 // declarations have to be carefully written to work for friend
563 // objects, with the most important detail being that the target
564 // decl should almost certainly not be placed in Owner.
565 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000566 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
John McCall02cace72009-08-28 07:59:38 +0000568 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000569 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
570 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000571 FD->setAccess(AS_public);
John McCall9a34edb2010-10-19 01:40:49 +0000572 FD->setUnsupportedFriend(D->isUnsupportedFriend());
John McCall02cace72009-08-28 07:59:38 +0000573 Owner->addDecl(FD);
574 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000575}
576
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000577Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
578 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregorac7610d2009-06-22 20:57:11 +0000580 // The expression in a static assertion is not potentially evaluated.
John McCallf312b1e2010-08-26 23:41:50 +0000581 EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
John McCall60d7b3a2010-08-24 06:29:42 +0000583 ExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000584 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000585 if (InstantiatedAssertExpr.isInvalid())
586 return 0;
587
John McCall60d7b3a2010-08-24 06:29:42 +0000588 ExprResult Message(D->getMessage());
John McCall3fa5cae2010-10-26 07:05:15 +0000589 D->getMessage();
John McCalld226f652010-08-21 09:40:31 +0000590 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000591 InstantiatedAssertExpr.get(),
592 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000593}
594
595Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000596 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000597 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000598 D->getTagKeywordLoc(),
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000599 /*PrevDecl=*/0,
600 D->isScoped(), D->isFixed());
601 if (D->isFixed()) {
602 if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
603 // If we have type source information for the underlying type, it means it
604 // has been explicitly set by the user. Perform substitution on it before
605 // moving on.
606 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
607 Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
608 TemplateArgs,
609 UnderlyingLoc,
610 DeclarationName()));
611
612 if (!Enum->getIntegerTypeSourceInfo())
613 Enum->setIntegerType(SemaRef.Context.IntTy);
614 }
615 else {
616 assert(!D->getIntegerType()->isDependentType()
617 && "Dependent type without type source info");
618 Enum->setIntegerType(D->getIntegerType());
619 }
620 }
621
John McCall5b629aa2010-10-22 23:36:17 +0000622 SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
623
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000624 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000625 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000626 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000627 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000628 Enum->startDefinition();
629
Douglas Gregor96084f12010-03-01 19:00:07 +0000630 if (D->getDeclContext()->isFunctionOrMethod())
631 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
632
John McCalld226f652010-08-21 09:40:31 +0000633 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000634
635 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000636 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
637 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000638 EC != ECEnd; ++EC) {
639 // The specified value for the enumerator.
John McCall60d7b3a2010-08-24 06:29:42 +0000640 ExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000641 if (Expr *UninstValue = EC->getInitExpr()) {
642 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000643 EnterExpressionEvaluationContext Unevaluated(SemaRef,
John McCallf312b1e2010-08-26 23:41:50 +0000644 Sema::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
John McCallce3ff2b2009-08-25 22:02:44 +0000646 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000647 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000648
649 // Drop the initial value and continue.
650 bool isInvalid = false;
651 if (Value.isInvalid()) {
652 Value = SemaRef.Owned((Expr *)0);
653 isInvalid = true;
654 }
655
Mike Stump1eb44332009-09-09 15:08:12 +0000656 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000657 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
658 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000659 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000660
661 if (isInvalid) {
662 if (EnumConst)
663 EnumConst->setInvalidDecl();
664 Enum->setInvalidDecl();
665 }
666
667 if (EnumConst) {
John McCall5b629aa2010-10-22 23:36:17 +0000668 SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
669
John McCall3b85ecf2010-01-23 22:37:59 +0000670 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000671 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000672 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000673 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000674
675 if (D->getDeclContext()->isFunctionOrMethod()) {
676 // If the enumeration is within a function or method, record the enum
677 // constant as a local.
678 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
679 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000680 }
681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000683 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000684 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000685 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000686 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000687 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000688 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000689
690 return Enum;
691}
692
Douglas Gregor6477b692009-03-25 15:04:13 +0000693Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
694 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
695 return 0;
696}
697
John McCalle29ba202009-08-20 01:44:21 +0000698Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000699 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
700
Douglas Gregor550d9b22009-10-31 17:21:17 +0000701 // Create a local instantiation scope for this class template, which
702 // will contain the instantiations of the template parameters.
John McCall2a7fb272010-08-25 05:32:35 +0000703 LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000704 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000705 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000706 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000707 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000708
709 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000710
711 // Instantiate the qualifier. We have to do this first in case
712 // we're a friend declaration, because if we are then we need to put
713 // the new declaration in the appropriate context.
714 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
715 if (Qualifier) {
716 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
717 Pattern->getQualifierRange(),
718 TemplateArgs);
719 if (!Qualifier) return 0;
720 }
721
722 CXXRecordDecl *PrevDecl = 0;
723 ClassTemplateDecl *PrevClassTemplate = 0;
724
725 // If this isn't a friend, then it's a member template, in which
726 // case we just want to build the instantiation in the
727 // specialization. If it is a friend, we want to build it in
728 // the appropriate context.
729 DeclContext *DC = Owner;
730 if (isFriend) {
731 if (Qualifier) {
732 CXXScopeSpec SS;
733 SS.setScopeRep(Qualifier);
734 SS.setRange(Pattern->getQualifierRange());
735 DC = SemaRef.computeDeclContext(SS);
736 if (!DC) return 0;
737 } else {
738 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
739 Pattern->getDeclContext(),
740 TemplateArgs);
741 }
742
743 // Look for a previous declaration of the template in the owning
744 // context.
745 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
746 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
747 SemaRef.LookupQualifiedName(R, DC);
748
749 if (R.isSingleResult()) {
750 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
751 if (PrevClassTemplate)
752 PrevDecl = PrevClassTemplate->getTemplatedDecl();
753 }
754
755 if (!PrevClassTemplate && Qualifier) {
756 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000757 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
758 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000759 return 0;
760 }
761
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000762 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000763 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000764 bool Complain = true;
765
766 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
767 // template for struct std::tr1::__detail::_Map_base, where the
768 // template parameters of the friend declaration don't match the
769 // template parameters of the original declaration. In this one
770 // case, we don't complain about the ill-formed friend
771 // declaration.
772 if (isFriend && Pattern->getIdentifier() &&
773 Pattern->getIdentifier()->isStr("_Map_base") &&
774 DC->isNamespace() &&
775 cast<NamespaceDecl>(DC)->getIdentifier() &&
776 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
777 DeclContext *DCParent = DC->getParent();
778 if (DCParent->isNamespace() &&
779 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
780 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
781 DeclContext *DCParent2 = DCParent->getParent();
782 if (DCParent2->isNamespace() &&
783 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
784 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
785 DCParent2->getParent()->isTranslationUnit())
786 Complain = false;
787 }
788 }
789
John McCall93ba8572010-03-25 06:39:04 +0000790 TemplateParameterList *PrevParams
791 = PrevClassTemplate->getTemplateParameters();
792
793 // Make sure the parameter lists match.
794 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000795 Complain,
796 Sema::TPL_TemplateMatch)) {
797 if (Complain)
798 return 0;
799
800 AdoptedPreviousTemplateParams = true;
801 InstParams = PrevParams;
802 }
John McCall93ba8572010-03-25 06:39:04 +0000803
804 // Do some additional validation, then merge default arguments
805 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000806 if (!AdoptedPreviousTemplateParams &&
807 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000808 Sema::TPC_ClassTemplate))
809 return 0;
810 }
811 }
812
John McCalle29ba202009-08-20 01:44:21 +0000813 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000814 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000815 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000816 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000817 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000818
John McCall93ba8572010-03-25 06:39:04 +0000819 if (Qualifier)
820 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000821
John McCalle29ba202009-08-20 01:44:21 +0000822 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000823 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
824 D->getIdentifier(), InstParams, RecordInst,
825 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000826 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000827
John McCall93ba8572010-03-25 06:39:04 +0000828 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000829 if (PrevClassTemplate)
830 Inst->setAccess(PrevClassTemplate->getAccess());
831 else
832 Inst->setAccess(D->getAccess());
833
John McCall93ba8572010-03-25 06:39:04 +0000834 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
835 // TODO: do we want to track the instantiation progeny of this
836 // friend target decl?
837 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000838 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000839 Inst->setInstantiatedFromMemberTemplate(D);
840 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000841
842 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000843 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000844 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000845
Douglas Gregor259571e2009-10-30 22:42:42 +0000846 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000847 if (isFriend) {
848 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000849 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000850 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000851
John McCalle29ba202009-08-20 01:44:21 +0000852 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000853
Douglas Gregored9c0f92009-10-29 00:04:11 +0000854 // Instantiate all of the partial specializations of this member class
855 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000856 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
857 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000858 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
859 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
860
John McCalle29ba202009-08-20 01:44:21 +0000861 return Inst;
862}
863
Douglas Gregord60e1052009-08-27 16:57:43 +0000864Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000865TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
866 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000867 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
868
869 // Lookup the already-instantiated declaration in the instantiation
870 // of the class template and return that.
871 DeclContext::lookup_result Found
872 = Owner->lookup(ClassTemplate->getDeclName());
873 if (Found.first == Found.second)
874 return 0;
875
876 ClassTemplateDecl *InstClassTemplate
877 = dyn_cast<ClassTemplateDecl>(*Found.first);
878 if (!InstClassTemplate)
879 return 0;
880
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000881 return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000882}
883
884Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000885TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000886 // Create a local instantiation scope for this function template, which
887 // will contain the instantiations of the template parameters and then get
888 // merged with the local instantiation scope for the function template
889 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000890 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000891
Douglas Gregord60e1052009-08-27 16:57:43 +0000892 TemplateParameterList *TempParams = D->getTemplateParameters();
893 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000894 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000895 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000896
Douglas Gregora735b202009-10-13 14:39:41 +0000897 FunctionDecl *Instantiated = 0;
898 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
899 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
900 InstParams));
901 else
902 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
903 D->getTemplatedDecl(),
904 InstParams));
905
906 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000907 return 0;
908
John McCall46460a62010-01-20 21:53:11 +0000909 Instantiated->setAccess(D->getAccess());
910
Mike Stump1eb44332009-09-09 15:08:12 +0000911 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000912 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000913 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000914 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000915 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000916 assert(InstTemplate &&
917 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000918
John McCallb1a56e72010-03-26 23:10:15 +0000919 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
920
John McCalle976ffe2009-12-14 23:19:40 +0000921 // Link the instantiation back to the pattern *unless* this is a
922 // non-definition friend declaration.
923 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000924 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000925 InstTemplate->setInstantiatedFromMemberTemplate(D);
926
John McCallb1a56e72010-03-26 23:10:15 +0000927 // Make declarations visible in the appropriate context.
928 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000929 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000930
Douglas Gregord60e1052009-08-27 16:57:43 +0000931 return InstTemplate;
932}
933
Douglas Gregord475b8d2009-03-25 21:17:03 +0000934Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
935 CXXRecordDecl *PrevDecl = 0;
936 if (D->isInjectedClassName())
937 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000938 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000939 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
940 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000941 TemplateArgs);
942 if (!Prev) return 0;
943 PrevDecl = cast<CXXRecordDecl>(Prev);
944 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000945
946 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000947 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000948 D->getLocation(), D->getIdentifier(),
949 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000950
951 // Substitute the nested name specifier, if any.
952 if (SubstQualifier(D, Record))
953 return 0;
954
Douglas Gregord475b8d2009-03-25 21:17:03 +0000955 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000956 // FIXME: Check against AS_none is an ugly hack to work around the issue that
957 // the tag decls introduced by friend class declarations don't have an access
958 // specifier. Remove once this area of the code gets sorted out.
959 if (D->getAccess() != AS_none)
960 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000961 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000962 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000963
John McCall02cace72009-08-28 07:59:38 +0000964 // If the original function was part of a friend declaration,
965 // inherit its namespace state.
966 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
967 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
968
Douglas Gregor9901c572010-05-21 00:31:19 +0000969 // Make sure that anonymous structs and unions are recorded.
970 if (D->isAnonymousStructOrUnion()) {
971 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000972 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000973 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
974 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000975
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000976 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000977 return Record;
978}
979
John McCall02cace72009-08-28 07:59:38 +0000980/// Normal class members are of more specific types and therefore
981/// don't make it here. This function serves two purposes:
982/// 1) instantiating function templates
983/// 2) substituting friend declarations
984/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000985Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000986 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000987 // Check whether there is already a function template specialization for
988 // this declaration.
989 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
990 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000991 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000992 std::pair<const TemplateArgument *, unsigned> Innermost
993 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000995 FunctionDecl *SpecFunc
996 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
997 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Douglas Gregor127102b2009-06-29 20:59:39 +0000999 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001000 if (SpecFunc)
1001 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +00001002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
John McCallb0cb0222010-03-27 05:57:59 +00001004 bool isFriend;
1005 if (FunctionTemplate)
1006 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1007 else
1008 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1009
Douglas Gregor79c22782010-01-16 20:21:20 +00001010 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +00001011 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +00001012 !(isa<Decl>(Owner) &&
1013 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001014 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregore53060f2009-06-25 22:08:12 +00001016 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001017 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1018 TInfo = SubstFunctionType(D, Params);
1019 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001020 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001021 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +00001022
John McCalld325daa2010-03-26 04:53:08 +00001023 NestedNameSpecifier *Qualifier = D->getQualifier();
1024 if (Qualifier) {
1025 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1026 D->getQualifierRange(),
1027 TemplateArgs);
1028 if (!Qualifier) return 0;
1029 }
1030
John McCall68b6b872010-02-06 01:50:47 +00001031 // If we're instantiating a local function declaration, put the result
1032 // in the owner; otherwise we need to find the instantiated context.
1033 DeclContext *DC;
1034 if (D->getDeclContext()->isFunctionOrMethod())
1035 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001036 else if (isFriend && Qualifier) {
1037 CXXScopeSpec SS;
1038 SS.setScopeRep(Qualifier);
1039 SS.setRange(D->getQualifierRange());
1040 DC = SemaRef.computeDeclContext(SS);
1041 if (!DC) return 0;
1042 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001043 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1044 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001045 }
John McCall68b6b872010-02-06 01:50:47 +00001046
John McCall02cace72009-08-28 07:59:38 +00001047 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001048 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001049 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001050 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001051 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001052
John McCalld325daa2010-03-26 04:53:08 +00001053 if (Qualifier)
1054 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001055
John McCallb1a56e72010-03-26 23:10:15 +00001056 DeclContext *LexicalDC = Owner;
1057 if (!isFriend && D->isOutOfLine()) {
1058 assert(D->getDeclContext()->isFileContext());
1059 LexicalDC = D->getDeclContext();
1060 }
1061
1062 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Douglas Gregore53060f2009-06-25 22:08:12 +00001064 // Attach the parameters
1065 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001066 if (Params[P])
1067 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001068 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001069
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001070 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001071 if (TemplateParams) {
1072 // Our resulting instantiation is actually a function template, since we
1073 // are substituting only the outer template parameters. For example, given
1074 //
1075 // template<typename T>
1076 // struct X {
1077 // template<typename U> friend void f(T, U);
1078 // };
1079 //
1080 // X<int> x;
1081 //
1082 // We are instantiating the friend function template "f" within X<int>,
1083 // which means substituting int for T, but leaving "f" as a friend function
1084 // template.
1085 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001086 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001087 Function->getLocation(),
1088 Function->getDeclName(),
1089 TemplateParams, Function);
1090 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001091
1092 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001093
1094 if (isFriend && D->isThisDeclarationADefinition()) {
1095 // TODO: should we remember this connection regardless of whether
1096 // the friend declaration provided a body?
1097 FunctionTemplate->setInstantiatedFromMemberTemplate(
1098 D->getDescribedFunctionTemplate());
1099 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001100 } else if (FunctionTemplate) {
1101 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001102 std::pair<const TemplateArgument *, unsigned> Innermost
1103 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001104 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001105 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001106 Innermost.first,
1107 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001108 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001109 } else if (isFriend && D->isThisDeclarationADefinition()) {
1110 // TODO: should we remember this connection regardless of whether
1111 // the friend declaration provided a body?
1112 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001113 }
Douglas Gregora735b202009-10-13 14:39:41 +00001114
Douglas Gregore53060f2009-06-25 22:08:12 +00001115 if (InitFunctionInstantiation(Function, D))
1116 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Douglas Gregore53060f2009-06-25 22:08:12 +00001118 bool Redeclaration = false;
1119 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001120 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001121
John McCall68263142009-11-18 22:49:29 +00001122 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1123 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1124
John McCallaf2094e2010-04-08 09:05:18 +00001125 if (DependentFunctionTemplateSpecializationInfo *Info
1126 = D->getDependentSpecializationInfo()) {
1127 assert(isFriend && "non-friend has dependent specialization info?");
1128
1129 // This needs to be set now for future sanity.
1130 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1131
1132 // Instantiate the explicit template arguments.
1133 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1134 Info->getRAngleLoc());
1135 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1136 TemplateArgumentLoc Loc;
1137 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1138 return 0;
1139
1140 ExplicitArgs.addArgument(Loc);
1141 }
1142
1143 // Map the candidate templates to their instantiations.
1144 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1145 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1146 Info->getTemplate(I),
1147 TemplateArgs);
1148 if (!Temp) return 0;
1149
1150 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1151 }
1152
1153 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1154 &ExplicitArgs,
1155 Previous))
1156 Function->setInvalidDecl();
1157
1158 isExplicitSpecialization = true;
1159
1160 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001161 // Look only into the namespace where the friend would be declared to
1162 // find a previous declaration. This is the innermost enclosing namespace,
1163 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001164 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001165
Douglas Gregora735b202009-10-13 14:39:41 +00001166 // In C++, the previous declaration we find might be a tag type
1167 // (class or enum). In this case, the new declaration will hide the
1168 // tag type. Note that this does does not apply if we're declaring a
1169 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001170 if (Previous.isSingleTagDecl())
1171 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001172 }
1173
John McCall9f54ad42009-12-10 09:41:52 +00001174 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001175 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001176 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001177
John McCall76d32642010-04-24 01:30:58 +00001178 NamedDecl *PrincipalDecl = (TemplateParams
1179 ? cast<NamedDecl>(FunctionTemplate)
1180 : Function);
1181
Douglas Gregora735b202009-10-13 14:39:41 +00001182 // If the original function was part of a friend declaration,
1183 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001184 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001185 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001186 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001187 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001188 else
Douglas Gregora735b202009-10-13 14:39:41 +00001189 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001190
1191 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1192 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001193
Gabor Greif77535df2010-08-30 22:25:56 +00001194 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001195
Douglas Gregor238058c2010-05-18 05:45:02 +00001196 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1197 D->isThisDeclarationADefinition()) {
1198 // Check for a function body.
1199 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001200 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001201 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1202 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1203 << Function->getDeclName();
1204 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1205 Function->setInvalidDecl();
1206 }
1207 // Check for redefinitions due to other instantiations of this or
1208 // a similar friend function.
1209 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1210 REnd = Function->redecls_end();
1211 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001212 if (*R == Function)
1213 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001214 switch (R->getFriendObjectKind()) {
1215 case Decl::FOK_None:
1216 if (!queuedInstantiation && R->isUsed(false)) {
1217 if (MemberSpecializationInfo *MSInfo
1218 = Function->getMemberSpecializationInfo()) {
1219 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1220 SourceLocation Loc = R->getLocation(); // FIXME
1221 MSInfo->setPointOfInstantiation(Loc);
1222 SemaRef.PendingLocalImplicitInstantiations.push_back(
1223 std::make_pair(Function, Loc));
1224 queuedInstantiation = true;
1225 }
1226 }
1227 }
1228 break;
1229 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001230 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001231 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001232 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001233 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1234 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001235 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001236 Function->setInvalidDecl();
1237 break;
1238 }
1239 }
1240 }
1241 }
Douglas Gregora735b202009-10-13 14:39:41 +00001242 }
1243
John McCall76d32642010-04-24 01:30:58 +00001244 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1245 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1246 PrincipalDecl->setNonMemberOperator();
1247
Douglas Gregore53060f2009-06-25 22:08:12 +00001248 return Function;
1249}
1250
Douglas Gregord60e1052009-08-27 16:57:43 +00001251Decl *
1252TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1253 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001254 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1255 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001256 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001257 // We are creating a function template specialization from a function
1258 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001259 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001260 std::pair<const TemplateArgument *, unsigned> Innermost
1261 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001263 FunctionDecl *SpecFunc
1264 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1265 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Douglas Gregor6b906862009-08-21 00:16:32 +00001267 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001268 if (SpecFunc)
1269 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001270 }
1271
John McCallb0cb0222010-03-27 05:57:59 +00001272 bool isFriend;
1273 if (FunctionTemplate)
1274 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1275 else
1276 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1277
Douglas Gregor79c22782010-01-16 20:21:20 +00001278 bool MergeWithParentScope = (TemplateParams != 0) ||
1279 !(isa<Decl>(Owner) &&
1280 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001281 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001282
John McCall4eab39f2010-10-19 02:26:41 +00001283 // Instantiate enclosing template arguments for friends.
1284 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1285 unsigned NumTempParamLists = 0;
1286 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1287 TempParamLists.set_size(NumTempParamLists);
1288 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1289 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1290 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1291 if (!InstParams)
1292 return NULL;
1293 TempParamLists[I] = InstParams;
1294 }
1295 }
1296
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001297 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001298 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1299 TInfo = SubstFunctionType(D, Params);
1300 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001301 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001302 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001303
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001304 // \brief If the type of this function is not *directly* a function
1305 // type, then we're instantiating the a function that was declared
1306 // via a typedef, e.g.,
1307 //
1308 // typedef int functype(int, int);
1309 // functype func;
1310 //
1311 // In this case, we'll just go instantiate the ParmVarDecls that we
1312 // synthesized in the method declaration.
1313 if (!isa<FunctionProtoType>(T)) {
1314 assert(!Params.size() && "Instantiating type could not yield parameters");
1315 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1316 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1317 TemplateArgs);
1318 if (!P)
1319 return 0;
1320
1321 Params.push_back(P);
1322 }
1323 }
1324
John McCallb0cb0222010-03-27 05:57:59 +00001325 NestedNameSpecifier *Qualifier = D->getQualifier();
1326 if (Qualifier) {
1327 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1328 D->getQualifierRange(),
1329 TemplateArgs);
1330 if (!Qualifier) return 0;
1331 }
1332
1333 DeclContext *DC = Owner;
1334 if (isFriend) {
1335 if (Qualifier) {
1336 CXXScopeSpec SS;
1337 SS.setScopeRep(Qualifier);
1338 SS.setRange(D->getQualifierRange());
1339 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001340
1341 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1342 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001343 } else {
1344 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1345 D->getDeclContext(),
1346 TemplateArgs);
1347 }
1348 if (!DC) return 0;
1349 }
1350
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001351 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001352 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001353 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Abramo Bagnara25777432010-08-11 22:01:17 +00001355 DeclarationNameInfo NameInfo
1356 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001357 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001358 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001359 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001360 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001361 Constructor->isInlineSpecified(),
1362 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001363 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001364 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001365 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001366 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001367 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001368 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001369 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001370 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001371 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001372 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001373 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001374 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1375 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001376 D->isStatic(),
1377 D->getStorageClassAsWritten(),
1378 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001379 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001380
John McCallb0cb0222010-03-27 05:57:59 +00001381 if (Qualifier)
1382 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001383
Douglas Gregord60e1052009-08-27 16:57:43 +00001384 if (TemplateParams) {
1385 // Our resulting instantiation is actually a function template, since we
1386 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001387 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001388 // template<typename T>
1389 // struct X {
1390 // template<typename U> void f(T, U);
1391 // };
1392 //
1393 // X<int> x;
1394 //
1395 // We are instantiating the member template "f" within X<int>, which means
1396 // substituting int for T, but leaving "f" as a member function template.
1397 // Build the function template itself.
1398 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1399 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001400 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001401 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001402 if (isFriend) {
1403 FunctionTemplate->setLexicalDeclContext(Owner);
1404 FunctionTemplate->setObjectOfFriendDecl(true);
1405 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001406 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001407 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001408 } else if (FunctionTemplate) {
1409 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001410 std::pair<const TemplateArgument *, unsigned> Innermost
1411 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001412 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001413 TemplateArgumentList::CreateCopy(SemaRef.Context,
1414 Innermost.first,
1415 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001416 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001417 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001418 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001419 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001420 }
1421
Mike Stump1eb44332009-09-09 15:08:12 +00001422 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001423 // out-of-line, the instantiation will have the same lexical
1424 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001425 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001426 if (NumTempParamLists)
1427 Method->setTemplateParameterListsInfo(SemaRef.Context,
1428 NumTempParamLists,
1429 TempParamLists.data());
1430
John McCallb0cb0222010-03-27 05:57:59 +00001431 Method->setLexicalDeclContext(Owner);
1432 Method->setObjectOfFriendDecl(true);
1433 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001434 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Douglas Gregor5545e162009-03-24 00:38:23 +00001436 // Attach the parameters
1437 for (unsigned P = 0; P < Params.size(); ++P)
1438 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001439 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001440
1441 if (InitMethodInstantiation(Method, D))
1442 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001443
Abramo Bagnara25777432010-08-11 22:01:17 +00001444 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1445 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001446
John McCallb0cb0222010-03-27 05:57:59 +00001447 if (!FunctionTemplate || TemplateParams || isFriend) {
1448 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregordec06662009-08-21 18:42:58 +00001450 // In C++, the previous declaration we find might be a tag type
1451 // (class or enum). In this case, the new declaration will hide the
1452 // tag type. Note that this does does not apply if we're declaring a
1453 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001454 if (Previous.isSingleTagDecl())
1455 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001456 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001457
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001458 bool Redeclaration = false;
1459 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001460 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001461 /*FIXME:*/OverloadableAttrRequired);
1462
Douglas Gregor4ba31362009-12-01 17:24:26 +00001463 if (D->isPure())
1464 SemaRef.CheckPureMethod(Method, SourceRange());
1465
John McCall46460a62010-01-20 21:53:11 +00001466 Method->setAccess(D->getAccess());
1467
John McCallb0cb0222010-03-27 05:57:59 +00001468 if (FunctionTemplate) {
1469 // If there's a function template, let our caller handle it.
1470 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1471 // Don't hide a (potentially) valid declaration with an invalid one.
1472 } else {
1473 NamedDecl *DeclToAdd = (TemplateParams
1474 ? cast<NamedDecl>(FunctionTemplate)
1475 : Method);
1476 if (isFriend)
1477 Record->makeDeclVisibleInContext(DeclToAdd);
1478 else
1479 Owner->addDecl(DeclToAdd);
1480 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001481
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001482 return Method;
1483}
1484
Douglas Gregor615c5d42009-03-24 16:43:20 +00001485Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001486 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001487}
1488
Douglas Gregor03b2b072009-03-24 00:15:49 +00001489Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001490 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001491}
1492
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001493Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001494 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001495}
1496
Douglas Gregor6477b692009-03-25 15:04:13 +00001497ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001498 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001499}
1500
John McCalle29ba202009-08-20 01:44:21 +00001501Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1502 TemplateTypeParmDecl *D) {
1503 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001504 const Type* T = D->getTypeForDecl();
1505 assert(T->isTemplateTypeParmType());
1506 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001507
John McCalle29ba202009-08-20 01:44:21 +00001508 TemplateTypeParmDecl *Inst =
1509 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001510 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001511 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001512 D->wasDeclaredWithTypename(),
1513 D->isParameterPack());
1514
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001515 if (D->hasDefaultArgument())
1516 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001517
Douglas Gregor550d9b22009-10-31 17:21:17 +00001518 // Introduce this template parameter's instantiation into the instantiation
1519 // scope.
1520 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1521
John McCalle29ba202009-08-20 01:44:21 +00001522 return Inst;
1523}
1524
Douglas Gregor33642df2009-10-23 23:25:44 +00001525Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1526 NonTypeTemplateParmDecl *D) {
1527 // Substitute into the type of the non-type template parameter.
1528 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001529 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001530 if (DI) {
1531 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1532 D->getDeclName());
1533 if (DI) T = DI->getType();
1534 } else {
1535 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1536 D->getDeclName());
1537 DI = 0;
1538 }
1539 if (T.isNull())
1540 return 0;
1541
1542 // Check that this type is acceptable for a non-type template parameter.
1543 bool Invalid = false;
1544 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1545 if (T.isNull()) {
1546 T = SemaRef.Context.IntTy;
1547 Invalid = true;
1548 }
1549
1550 NonTypeTemplateParmDecl *Param
1551 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001552 D->getDepth() - TemplateArgs.getNumLevels(),
1553 D->getPosition(), D->getIdentifier(), T,
1554 DI);
Douglas Gregor33642df2009-10-23 23:25:44 +00001555 if (Invalid)
1556 Param->setInvalidDecl();
1557
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001558 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001559
1560 // Introduce this template parameter's instantiation into the instantiation
1561 // scope.
1562 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001563 return Param;
1564}
1565
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001566Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001567TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1568 TemplateTemplateParmDecl *D) {
1569 // Instantiate the template parameter list of the template template parameter.
1570 TemplateParameterList *TempParams = D->getTemplateParameters();
1571 TemplateParameterList *InstParams;
1572 {
1573 // Perform the actual substitution of template parameters within a new,
1574 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001575 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001576 InstParams = SubstTemplateParams(TempParams);
1577 if (!InstParams)
1578 return NULL;
1579 }
1580
1581 // Build the template template parameter.
1582 TemplateTemplateParmDecl *Param
1583 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001584 D->getDepth() - TemplateArgs.getNumLevels(),
1585 D->getPosition(), D->getIdentifier(),
1586 InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001587 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001588
Douglas Gregor9106ef72009-11-11 16:58:32 +00001589 // Introduce this template parameter's instantiation into the instantiation
1590 // scope.
1591 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1592
1593 return Param;
1594}
1595
Douglas Gregor48c32a72009-11-17 06:07:40 +00001596Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1597 // Using directives are never dependent, so they require no explicit
1598
1599 UsingDirectiveDecl *Inst
1600 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1601 D->getNamespaceKeyLocation(),
1602 D->getQualifierRange(), D->getQualifier(),
1603 D->getIdentLocation(),
1604 D->getNominatedNamespace(),
1605 D->getCommonAncestor());
1606 Owner->addDecl(Inst);
1607 return Inst;
1608}
1609
John McCalled976492009-12-04 22:46:56 +00001610Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001611
1612 // The nested name specifier may be dependent, for example
1613 // template <typename T> struct t {
1614 // struct s1 { T f1(); };
1615 // struct s2 : s1 { using s1::f1; };
1616 // };
1617 // template struct t<int>;
1618 // Here, in using s1::f1, s1 refers to t<T>::s1;
1619 // we need to substitute for t<int>::s1.
1620 NestedNameSpecifier *NNS =
1621 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
1622 D->getNestedNameRange(),
1623 TemplateArgs);
1624 if (!NNS)
1625 return 0;
1626
1627 // The name info is non-dependent, so no transformation
1628 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001629 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001630
John McCall9f54ad42009-12-10 09:41:52 +00001631 // We only need to do redeclaration lookups if we're in a class
1632 // scope (in fact, it's not really even possible in non-class
1633 // scopes).
1634 bool CheckRedeclaration = Owner->isRecord();
1635
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001636 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1637 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001638
John McCalled976492009-12-04 22:46:56 +00001639 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001640 D->getNestedNameRange(),
1641 D->getUsingLocation(),
Douglas Gregor1b398202010-09-29 17:58:28 +00001642 NNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001643 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001644 D->isTypeName());
1645
1646 CXXScopeSpec SS;
Douglas Gregor1b398202010-09-29 17:58:28 +00001647 SS.setScopeRep(NNS);
John McCalled976492009-12-04 22:46:56 +00001648 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001649
1650 if (CheckRedeclaration) {
1651 Prev.setHideTags(false);
1652 SemaRef.LookupQualifiedName(Prev, Owner);
1653
1654 // Check for invalid redeclarations.
1655 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1656 D->isTypeName(), SS,
1657 D->getLocation(), Prev))
1658 NewUD->setInvalidDecl();
1659
1660 }
1661
1662 if (!NewUD->isInvalidDecl() &&
1663 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001664 D->getLocation()))
1665 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001666
John McCalled976492009-12-04 22:46:56 +00001667 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1668 NewUD->setAccess(D->getAccess());
1669 Owner->addDecl(NewUD);
1670
John McCall9f54ad42009-12-10 09:41:52 +00001671 // Don't process the shadow decls for an invalid decl.
1672 if (NewUD->isInvalidDecl())
1673 return NewUD;
1674
John McCall323c3102009-12-22 22:26:37 +00001675 bool isFunctionScope = Owner->isFunctionOrMethod();
1676
John McCall9f54ad42009-12-10 09:41:52 +00001677 // Process the shadow decls.
1678 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1679 I != E; ++I) {
1680 UsingShadowDecl *Shadow = *I;
1681 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001682 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1683 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001684 TemplateArgs));
1685
1686 if (CheckRedeclaration &&
1687 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1688 continue;
1689
1690 UsingShadowDecl *InstShadow
1691 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1692 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001693
1694 if (isFunctionScope)
1695 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001696 }
John McCalled976492009-12-04 22:46:56 +00001697
1698 return NewUD;
1699}
1700
1701Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001702 // Ignore these; we handle them in bulk when processing the UsingDecl.
1703 return 0;
John McCalled976492009-12-04 22:46:56 +00001704}
1705
John McCall7ba107a2009-11-18 02:36:19 +00001706Decl * TemplateDeclInstantiator
1707 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001708 NestedNameSpecifier *NNS =
1709 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1710 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001711 TemplateArgs);
1712 if (!NNS)
1713 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001715 CXXScopeSpec SS;
1716 SS.setRange(D->getTargetNestedNameRange());
1717 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001719 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1720 // Hence, no tranformation is required for it.
1721 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001722 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001723 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001724 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001725 /*instantiation*/ true,
1726 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001727 if (UD)
John McCalled976492009-12-04 22:46:56 +00001728 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1729
John McCall7ba107a2009-11-18 02:36:19 +00001730 return UD;
1731}
1732
1733Decl * TemplateDeclInstantiator
1734 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1735 NestedNameSpecifier *NNS =
1736 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1737 D->getTargetNestedNameRange(),
1738 TemplateArgs);
1739 if (!NNS)
1740 return 0;
1741
1742 CXXScopeSpec SS;
1743 SS.setRange(D->getTargetNestedNameRange());
1744 SS.setScopeRep(NNS);
1745
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001746 DeclarationNameInfo NameInfo
1747 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1748
John McCall7ba107a2009-11-18 02:36:19 +00001749 NamedDecl *UD =
1750 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001751 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001752 /*instantiation*/ true,
1753 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001754 if (UD)
John McCalled976492009-12-04 22:46:56 +00001755 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1756
Anders Carlsson0d8df782009-08-29 19:37:28 +00001757 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001758}
1759
John McCallce3ff2b2009-08-25 22:02:44 +00001760Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001761 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001762 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001763 if (D->isInvalidDecl())
1764 return 0;
1765
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001766 return Instantiator.Visit(D);
1767}
1768
John McCalle29ba202009-08-20 01:44:21 +00001769/// \brief Instantiates a nested template parameter list in the current
1770/// instantiation context.
1771///
1772/// \param L The parameter list to instantiate
1773///
1774/// \returns NULL if there was an error
1775TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001776TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001777 // Get errors for all the parameters before bailing out.
1778 bool Invalid = false;
1779
1780 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001781 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001782 ParamVector Params;
1783 Params.reserve(N);
1784 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1785 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001786 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001787 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001788 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001789 }
1790
1791 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001792 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001793 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001794
1795 TemplateParameterList *InstL
1796 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1797 L->getLAngleLoc(), &Params.front(), N,
1798 L->getRAngleLoc());
1799 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001800}
John McCalle29ba202009-08-20 01:44:21 +00001801
Douglas Gregored9c0f92009-10-29 00:04:11 +00001802/// \brief Instantiate the declaration of a class template partial
1803/// specialization.
1804///
1805/// \param ClassTemplate the (instantiated) class template that is partially
1806// specialized by the instantiation of \p PartialSpec.
1807///
1808/// \param PartialSpec the (uninstantiated) class template partial
1809/// specialization that we are instantiating.
1810///
1811/// \returns true if there was an error, false otherwise.
1812bool
1813TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1814 ClassTemplateDecl *ClassTemplate,
1815 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001816 // Create a local instantiation scope for this class template partial
1817 // specialization, which will contain the instantiations of the template
1818 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001819 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001820
Douglas Gregored9c0f92009-10-29 00:04:11 +00001821 // Substitute into the template parameters of the class template partial
1822 // specialization.
1823 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1824 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1825 if (!InstParams)
1826 return true;
1827
1828 // Substitute into the template arguments of the class template partial
1829 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001830 const TemplateArgumentLoc *PartialSpecTemplateArgs
1831 = PartialSpec->getTemplateArgsAsWritten();
1832 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1833
John McCalld5532b62009-11-23 01:53:49 +00001834 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001835 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001836 TemplateArgumentLoc Loc;
1837 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001838 return true;
John McCalld5532b62009-11-23 01:53:49 +00001839 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001840 }
1841
1842
1843 // Check that the template argument list is well-formed for this
1844 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001845 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001846 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1847 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001848 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001849 false,
1850 Converted))
1851 return true;
1852
1853 // Figure out where to insert this class template partial specialization
1854 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001855 void *InsertPos = 0;
1856 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001857 = ClassTemplate->findPartialSpecialization(Converted.data(),
1858 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001859
1860 // Build the canonical type that describes the converted template
1861 // arguments of the class template partial specialization.
1862 QualType CanonType
1863 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001864 Converted.data(),
1865 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001866
1867 // Build the fully-sugared type for this class template
1868 // specialization as the user wrote in the specialization
1869 // itself. This means that we'll pretty-print the type retrieved
1870 // from the specialization's declaration the way that the user
1871 // actually wrote the specialization, rather than formatting the
1872 // name based on the "canonical" representation used to store the
1873 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001874 TypeSourceInfo *WrittenTy
1875 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1876 TemplateName(ClassTemplate),
1877 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001878 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001879 CanonType);
1880
1881 if (PrevDecl) {
1882 // We've already seen a partial specialization with the same template
1883 // parameters and template arguments. This can happen, for example, when
1884 // substituting the outer template arguments ends up causing two
1885 // class template partial specializations of a member class template
1886 // to have identical forms, e.g.,
1887 //
1888 // template<typename T, typename U>
1889 // struct Outer {
1890 // template<typename X, typename Y> struct Inner;
1891 // template<typename Y> struct Inner<T, Y>;
1892 // template<typename Y> struct Inner<U, Y>;
1893 // };
1894 //
1895 // Outer<int, int> outer; // error: the partial specializations of Inner
1896 // // have the same signature.
1897 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1898 << WrittenTy;
1899 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1900 << SemaRef.Context.getTypeDeclType(PrevDecl);
1901 return true;
1902 }
1903
1904
1905 // Create the class template partial specialization declaration.
1906 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001907 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1908 PartialSpec->getTagKind(),
1909 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001910 PartialSpec->getLocation(),
1911 InstParams,
1912 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001913 Converted.data(),
1914 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001915 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001916 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001917 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001918 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001919 // Substitute the nested name specifier, if any.
1920 if (SubstQualifier(PartialSpec, InstPartialSpec))
1921 return 0;
1922
Douglas Gregored9c0f92009-10-29 00:04:11 +00001923 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001924 InstPartialSpec->setTypeAsWritten(WrittenTy);
1925
Douglas Gregored9c0f92009-10-29 00:04:11 +00001926 // Add this partial specialization to the set of class template partial
1927 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001928 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001929 return false;
1930}
1931
John McCall21ef0fa2010-03-11 09:03:00 +00001932TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001933TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001934 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001935 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1936 assert(OldTInfo && "substituting function without type source info");
1937 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001938 TypeSourceInfo *NewTInfo
1939 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1940 D->getTypeSpecStartLoc(),
1941 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001942 if (!NewTInfo)
1943 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001944
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001945 if (NewTInfo != OldTInfo) {
1946 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001947 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001948 if (FunctionProtoTypeLoc *OldProtoLoc
1949 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1950 TypeLoc NewTL = NewTInfo->getTypeLoc();
1951 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1952 assert(NewProtoLoc && "Missing prototype?");
1953 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1954 // FIXME: Variadic templates will break this.
1955 Params.push_back(NewProtoLoc->getArg(i));
1956 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001957 OldProtoLoc->getArg(i),
1958 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001959 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001960 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001961 } else {
1962 // The function type itself was not dependent and therefore no
1963 // substitution occurred. However, we still need to instantiate
1964 // the function parameters themselves.
1965 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001966 if (FunctionProtoTypeLoc *OldProtoLoc
1967 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1968 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1969 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1970 if (!Parm)
1971 return 0;
1972 Params.push_back(Parm);
1973 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001974 }
1975 }
John McCall21ef0fa2010-03-11 09:03:00 +00001976 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001977}
1978
Mike Stump1eb44332009-09-09 15:08:12 +00001979/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001980/// declaration (New) from the corresponding fields of its template (Tmpl).
1981///
1982/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001983bool
1984TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001985 FunctionDecl *Tmpl) {
1986 if (Tmpl->isDeleted())
1987 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Douglas Gregorcca9e962009-07-01 22:01:06 +00001989 // If we are performing substituting explicitly-specified template arguments
1990 // or deduced template arguments into a function template and we reach this
1991 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001992 // to keeping the new function template specialization. We therefore
1993 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001994 // into a template instantiation for this specific function template
1995 // specialization, which is not a SFINAE context, so that we diagnose any
1996 // further errors in the declaration itself.
1997 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1998 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1999 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2000 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002001 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002002 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002003 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002004 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002005 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002006 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2007 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002008 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002009 }
2010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002012 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2013 assert(Proto && "Function template without prototype?");
2014
2015 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2016 Proto->getNoReturnAttr()) {
2017 // The function has an exception specification or a "noreturn"
2018 // attribute. Substitute into each of the exception types.
2019 llvm::SmallVector<QualType, 4> Exceptions;
2020 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2021 // FIXME: Poor location information!
2022 QualType T
2023 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2024 New->getLocation(), New->getDeclName());
2025 if (T.isNull() ||
2026 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2027 continue;
2028
2029 Exceptions.push_back(T);
2030 }
2031
2032 // Rebuild the function type
2033
2034 const FunctionProtoType *NewProto
2035 = New->getType()->getAs<FunctionProtoType>();
2036 assert(NewProto && "Template instantiation without function prototype?");
2037 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2038 NewProto->arg_type_begin(),
2039 NewProto->getNumArgs(),
2040 NewProto->isVariadic(),
2041 NewProto->getTypeQuals(),
2042 Proto->hasExceptionSpec(),
2043 Proto->hasAnyExceptionSpec(),
2044 Exceptions.size(),
2045 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00002046 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002047 }
2048
John McCall1d8d1cc2010-08-01 02:01:53 +00002049 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002050
Douglas Gregore53060f2009-06-25 22:08:12 +00002051 return false;
2052}
2053
Douglas Gregor5545e162009-03-24 00:38:23 +00002054/// \brief Initializes common fields of an instantiated method
2055/// declaration (New) from the corresponding fields of its template
2056/// (Tmpl).
2057///
2058/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002059bool
2060TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002061 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002062 if (InitFunctionInstantiation(New, Tmpl))
2063 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002064
Douglas Gregor5545e162009-03-24 00:38:23 +00002065 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002066 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002067 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002068
2069 // FIXME: attributes
2070 // FIXME: New needs a pointer to Tmpl
2071 return false;
2072}
Douglas Gregora58861f2009-05-13 20:28:22 +00002073
2074/// \brief Instantiate the definition of the given function from its
2075/// template.
2076///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002077/// \param PointOfInstantiation the point at which the instantiation was
2078/// required. Note that this is not precisely a "point of instantiation"
2079/// for the function, but it's close.
2080///
Douglas Gregora58861f2009-05-13 20:28:22 +00002081/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002082/// function template specialization or member function of a class template
2083/// specialization.
2084///
2085/// \param Recursive if true, recursively instantiates any functions that
2086/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002087///
2088/// \param DefinitionRequired if true, then we are performing an explicit
2089/// instantiation where the body of the function is required. Complain if
2090/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002091void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002092 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002093 bool Recursive,
2094 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002095 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002096 return;
2097
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002098 // Never instantiate an explicit specialization.
2099 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2100 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002101
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002102 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002103 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002104 Stmt *Pattern = 0;
2105 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002106 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002107
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002108 if (!Pattern) {
2109 if (DefinitionRequired) {
2110 if (Function->getPrimaryTemplate())
2111 Diag(PointOfInstantiation,
2112 diag::err_explicit_instantiation_undefined_func_template)
2113 << Function->getPrimaryTemplate();
2114 else
2115 Diag(PointOfInstantiation,
2116 diag::err_explicit_instantiation_undefined_member)
2117 << 1 << Function->getDeclName() << Function->getDeclContext();
2118
2119 if (PatternDecl)
2120 Diag(PatternDecl->getLocation(),
2121 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002122 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002123 } else if (Function->getTemplateSpecializationKind()
2124 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002125 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002126 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002127 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002128
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002129 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002130 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002131
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002132 // C++0x [temp.explicit]p9:
2133 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002134 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002135 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002136 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002137 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002138 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002139 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002141 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2142 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002143 return;
2144
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002145 // If we're performing recursive template instantiation, create our own
2146 // queue of pending implicit instantiations that we will instantiate later,
2147 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002148 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002149 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002150 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Douglas Gregor9679caf2010-05-12 17:27:19 +00002152 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002153 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002154 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002155
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002156 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002157 // recorded, unless we're actually a member function within a local
2158 // class, in which case we need to merge our results with the parent
2159 // scope (of the enclosing function).
2160 bool MergeWithParentScope = false;
2161 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2162 MergeWithParentScope = Rec->isLocalClass();
2163
2164 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002166 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002167 // instantiation scope, and set the parameter names to those used
2168 // in the template.
2169 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2170 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2171 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2172 FunctionParam->setDeclName(PatternParam->getDeclName());
2173 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2174 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002175
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002176 // Enter the scope of this instantiation. We don't use
2177 // PushDeclContext because we don't have a scope.
2178 DeclContext *PreviousContext = CurContext;
2179 CurContext = Function;
2180
Mike Stump1eb44332009-09-09 15:08:12 +00002181 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002182 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002183
2184 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002185 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002186 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2187 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2188 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002189 }
2190
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002191 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002192 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002193
Douglas Gregor52604ab2009-09-11 21:19:12 +00002194 if (Body.isInvalid())
2195 Function->setInvalidDecl();
2196
John McCall9ae2f072010-08-23 23:25:46 +00002197 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002198 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002199
John McCall0c01d182010-03-24 05:22:00 +00002200 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2201
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002202 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002203
2204 DeclGroupRef DG(Function);
2205 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002206
Douglas Gregor60406be2010-01-16 22:29:39 +00002207 // This class may have local implicit instantiations that need to be
2208 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002209 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002210 Scope.Exit();
2211
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002212 if (Recursive) {
2213 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002214 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002215 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002217 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002218 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002219 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002220}
2221
2222/// \brief Instantiate the definition of the given variable from its
2223/// template.
2224///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002225/// \param PointOfInstantiation the point at which the instantiation was
2226/// required. Note that this is not precisely a "point of instantiation"
2227/// for the function, but it's close.
2228///
2229/// \param Var the already-instantiated declaration of a static member
2230/// variable of a class template specialization.
2231///
2232/// \param Recursive if true, recursively instantiates any functions that
2233/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002234///
2235/// \param DefinitionRequired if true, then we are performing an explicit
2236/// instantiation where an out-of-line definition of the member variable
2237/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002238void Sema::InstantiateStaticDataMemberDefinition(
2239 SourceLocation PointOfInstantiation,
2240 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002241 bool Recursive,
2242 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002243 if (Var->isInvalidDecl())
2244 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Douglas Gregor7caa6822009-07-24 20:34:43 +00002246 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002247 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002248 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002249 assert(Def->isStaticDataMember() && "Not a static data member?");
2250 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Douglas Gregor0d035142009-10-27 18:42:08 +00002252 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002253 // We did not find an out-of-line definition of this static data member,
2254 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002255 // instantiate this definition (or provide a specialization for it) in
2256 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002257 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002258 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002259 Diag(PointOfInstantiation,
2260 diag::err_explicit_instantiation_undefined_member)
2261 << 2 << Var->getDeclName() << Var->getDeclContext();
2262 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002263 } else if (Var->getTemplateSpecializationKind()
2264 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002265 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002266 std::make_pair(Var, PointOfInstantiation));
2267 }
2268
Douglas Gregor7caa6822009-07-24 20:34:43 +00002269 return;
2270 }
2271
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002272 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002273 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002274 return;
2275
2276 // C++0x [temp.explicit]p9:
2277 // Except for inline functions, other explicit instantiation declarations
2278 // have the effect of suppressing the implicit instantiation of the entity
2279 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002280 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002281 == TSK_ExplicitInstantiationDeclaration)
2282 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Douglas Gregor7caa6822009-07-24 20:34:43 +00002284 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2285 if (Inst)
2286 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Douglas Gregor7caa6822009-07-24 20:34:43 +00002288 // If we're performing recursive template instantiation, create our own
2289 // queue of pending implicit instantiations that we will instantiate later,
2290 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002291 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002292 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002293 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002294
Douglas Gregor7caa6822009-07-24 20:34:43 +00002295 // Enter the scope of this instantiation. We don't use
2296 // PushDeclContext because we don't have a scope.
2297 DeclContext *PreviousContext = CurContext;
2298 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002299
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002300 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002301 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002302 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002303 CurContext = PreviousContext;
2304
2305 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002306 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2307 assert(MSInfo && "Missing member specialization information?");
2308 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2309 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002310 DeclGroupRef DG(Var);
2311 Consumer.HandleTopLevelDecl(DG);
2312 }
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Douglas Gregor7caa6822009-07-24 20:34:43 +00002314 if (Recursive) {
2315 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002316 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002317 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Douglas Gregor7caa6822009-07-24 20:34:43 +00002319 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002320 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002321 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002322}
Douglas Gregor815215d2009-05-27 05:35:12 +00002323
Anders Carlsson09025312009-08-29 05:16:22 +00002324void
2325Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2326 const CXXConstructorDecl *Tmpl,
2327 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002328
Anders Carlsson09025312009-08-29 05:16:22 +00002329 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002330 bool AnyErrors = false;
2331
Anders Carlsson09025312009-08-29 05:16:22 +00002332 // Instantiate all the initializers.
2333 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002334 InitsEnd = Tmpl->init_end();
2335 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002336 CXXBaseOrMemberInitializer *Init = *Inits;
2337
Chandler Carruth030ef472010-09-03 21:54:20 +00002338 // Only instantiate written initializers, let Sema re-construct implicit
2339 // ones.
2340 if (!Init->isWritten())
2341 continue;
2342
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002343 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002344 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002345
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002346 // Instantiate the initializer.
2347 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002348 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002349 AnyErrors = true;
2350 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002351 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002352
Anders Carlsson09025312009-08-29 05:16:22 +00002353 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002354 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002355 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002356 TemplateArgs,
2357 Init->getSourceLocation(),
2358 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002359 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002360 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002361 New->setInvalidDecl();
2362 continue;
2363 }
2364
John McCalla93c9342009-12-07 02:54:59 +00002365 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002366 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002367 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002368 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002369 Init->getRParenLoc(),
2370 New->getParent());
2371 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002372 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002373
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002374 // Is this an anonymous union?
2375 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002376 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2377 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002378 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002379 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2380 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002381 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002382
2383 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002384 NewArgs.size(),
2385 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002386 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002387 Init->getRParenLoc());
2388 }
2389
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002390 if (NewInit.isInvalid()) {
2391 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002392 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002393 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002394 // FIXME: It would be nice if ASTOwningVector had a release function.
2395 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Anders Carlsson09025312009-08-29 05:16:22 +00002397 NewInits.push_back((MemInitTy *)NewInit.get());
2398 }
2399 }
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Anders Carlsson09025312009-08-29 05:16:22 +00002401 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002402 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002403 /*FIXME: ColonLoc */
2404 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002405 NewInits.data(), NewInits.size(),
2406 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002407}
2408
John McCall52a575a2009-08-29 08:11:13 +00002409// TODO: this could be templated if the various decl types used the
2410// same method name.
2411static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2412 ClassTemplateDecl *Instance) {
2413 Pattern = Pattern->getCanonicalDecl();
2414
2415 do {
2416 Instance = Instance->getCanonicalDecl();
2417 if (Pattern == Instance) return true;
2418 Instance = Instance->getInstantiatedFromMemberTemplate();
2419 } while (Instance);
2420
2421 return false;
2422}
2423
Douglas Gregor0d696532009-09-28 06:34:35 +00002424static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2425 FunctionTemplateDecl *Instance) {
2426 Pattern = Pattern->getCanonicalDecl();
2427
2428 do {
2429 Instance = Instance->getCanonicalDecl();
2430 if (Pattern == Instance) return true;
2431 Instance = Instance->getInstantiatedFromMemberTemplate();
2432 } while (Instance);
2433
2434 return false;
2435}
2436
Douglas Gregored9c0f92009-10-29 00:04:11 +00002437static bool
2438isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2439 ClassTemplatePartialSpecializationDecl *Instance) {
2440 Pattern
2441 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2442 do {
2443 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2444 Instance->getCanonicalDecl());
2445 if (Pattern == Instance)
2446 return true;
2447 Instance = Instance->getInstantiatedFromMember();
2448 } while (Instance);
2449
2450 return false;
2451}
2452
John McCall52a575a2009-08-29 08:11:13 +00002453static bool isInstantiationOf(CXXRecordDecl *Pattern,
2454 CXXRecordDecl *Instance) {
2455 Pattern = Pattern->getCanonicalDecl();
2456
2457 do {
2458 Instance = Instance->getCanonicalDecl();
2459 if (Pattern == Instance) return true;
2460 Instance = Instance->getInstantiatedFromMemberClass();
2461 } while (Instance);
2462
2463 return false;
2464}
2465
2466static bool isInstantiationOf(FunctionDecl *Pattern,
2467 FunctionDecl *Instance) {
2468 Pattern = Pattern->getCanonicalDecl();
2469
2470 do {
2471 Instance = Instance->getCanonicalDecl();
2472 if (Pattern == Instance) return true;
2473 Instance = Instance->getInstantiatedFromMemberFunction();
2474 } while (Instance);
2475
2476 return false;
2477}
2478
2479static bool isInstantiationOf(EnumDecl *Pattern,
2480 EnumDecl *Instance) {
2481 Pattern = Pattern->getCanonicalDecl();
2482
2483 do {
2484 Instance = Instance->getCanonicalDecl();
2485 if (Pattern == Instance) return true;
2486 Instance = Instance->getInstantiatedFromMemberEnum();
2487 } while (Instance);
2488
2489 return false;
2490}
2491
John McCalled976492009-12-04 22:46:56 +00002492static bool isInstantiationOf(UsingShadowDecl *Pattern,
2493 UsingShadowDecl *Instance,
2494 ASTContext &C) {
2495 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2496}
2497
2498static bool isInstantiationOf(UsingDecl *Pattern,
2499 UsingDecl *Instance,
2500 ASTContext &C) {
2501 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2502}
2503
John McCall7ba107a2009-11-18 02:36:19 +00002504static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2505 UsingDecl *Instance,
2506 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002507 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002508}
2509
2510static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002511 UsingDecl *Instance,
2512 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002513 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002514}
2515
John McCall52a575a2009-08-29 08:11:13 +00002516static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2517 VarDecl *Instance) {
2518 assert(Instance->isStaticDataMember());
2519
2520 Pattern = Pattern->getCanonicalDecl();
2521
2522 do {
2523 Instance = Instance->getCanonicalDecl();
2524 if (Pattern == Instance) return true;
2525 Instance = Instance->getInstantiatedFromStaticDataMember();
2526 } while (Instance);
2527
2528 return false;
2529}
2530
John McCalled976492009-12-04 22:46:56 +00002531// Other is the prospective instantiation
2532// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002533static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002534 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002535 if (UnresolvedUsingTypenameDecl *UUD
2536 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2537 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2538 return isInstantiationOf(UUD, UD, Ctx);
2539 }
2540 }
2541
2542 if (UnresolvedUsingValueDecl *UUD
2543 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002544 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2545 return isInstantiationOf(UUD, UD, Ctx);
2546 }
2547 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002548
Anders Carlsson0d8df782009-08-29 19:37:28 +00002549 return false;
2550 }
Mike Stump1eb44332009-09-09 15:08:12 +00002551
John McCall52a575a2009-08-29 08:11:13 +00002552 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2553 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002554
John McCall52a575a2009-08-29 08:11:13 +00002555 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2556 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002557
John McCall52a575a2009-08-29 08:11:13 +00002558 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2559 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002560
Douglas Gregor7caa6822009-07-24 20:34:43 +00002561 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002562 if (Var->isStaticDataMember())
2563 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2564
2565 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2566 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002567
Douglas Gregor0d696532009-09-28 06:34:35 +00002568 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2569 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2570
Douglas Gregored9c0f92009-10-29 00:04:11 +00002571 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2572 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2573 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2574 PartialSpec);
2575
Anders Carlssond8b285f2009-09-01 04:26:58 +00002576 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2577 if (!Field->getDeclName()) {
2578 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002579 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002580 cast<FieldDecl>(D);
2581 }
2582 }
Mike Stump1eb44332009-09-09 15:08:12 +00002583
John McCalled976492009-12-04 22:46:56 +00002584 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2585 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2586
2587 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2588 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2589
Douglas Gregor815215d2009-05-27 05:35:12 +00002590 return D->getDeclName() && isa<NamedDecl>(Other) &&
2591 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2592}
2593
2594template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002595static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002596 NamedDecl *D,
2597 ForwardIterator first,
2598 ForwardIterator last) {
2599 for (; first != last; ++first)
2600 if (isInstantiationOf(Ctx, D, *first))
2601 return cast<NamedDecl>(*first);
2602
2603 return 0;
2604}
2605
John McCall02cace72009-08-28 07:59:38 +00002606/// \brief Finds the instantiation of the given declaration context
2607/// within the current instantiation.
2608///
2609/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002610DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002611 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002612 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002613 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002614 return cast_or_null<DeclContext>(ID);
2615 } else return DC;
2616}
2617
Douglas Gregored961e72009-05-27 17:54:46 +00002618/// \brief Find the instantiation of the given declaration within the
2619/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002620///
2621/// This routine is intended to be used when \p D is a declaration
2622/// referenced from within a template, that needs to mapped into the
2623/// corresponding declaration within an instantiation. For example,
2624/// given:
2625///
2626/// \code
2627/// template<typename T>
2628/// struct X {
2629/// enum Kind {
2630/// KnownValue = sizeof(T)
2631/// };
2632///
2633/// bool getKind() const { return KnownValue; }
2634/// };
2635///
2636/// template struct X<int>;
2637/// \endcode
2638///
2639/// In the instantiation of X<int>::getKind(), we need to map the
2640/// EnumConstantDecl for KnownValue (which refers to
2641/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002642/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2643/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002644NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002645 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002646 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002647 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002648 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002649 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002650 // D is a local of some kind. Look into the map of local
2651 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002652 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002653 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002654
Douglas Gregore95b4092009-09-16 18:34:49 +00002655 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2656 if (!Record->isDependentContext())
2657 return D;
2658
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002659 // If the RecordDecl is actually the injected-class-name or a
2660 // "templated" declaration for a class template, class template
2661 // partial specialization, or a member class of a class template,
2662 // substitute into the injected-class-name of the class template
2663 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002664 QualType T;
2665 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2666
2667 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002668 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002669 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2670 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002671 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002672
2673 // If we call SubstType with an InjectedClassNameType here we
2674 // can end up in an infinite loop.
2675 T = Context.getTypeDeclType(Record);
2676 assert(isa<InjectedClassNameType>(T) &&
2677 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002678 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002679 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002680
2681 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002682 // Substitute into the injected-class-name to get the type
2683 // corresponding to the instantiation we want, which may also be
2684 // the current instantiation (if we're in a template
2685 // definition). This substitution should never fail, since we
2686 // know we can instantiate the injected-class-name or we
2687 // wouldn't have gotten to the injected-class-name!
2688
2689 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2690 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002691 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2692 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2693
2694 if (!T->isDependentType()) {
2695 assert(T->isRecordType() && "Instantiation must produce a record type");
2696 return T->getAs<RecordType>()->getDecl();
2697 }
2698
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002699 // We are performing "partial" template instantiation to create
2700 // the member declarations for the members of a class template
2701 // specialization. Therefore, D is actually referring to something
2702 // in the current instantiation. Look through the current
2703 // context, which contains actual instantiations, to find the
2704 // instantiation of the "current instantiation" that D refers
2705 // to.
2706 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002707 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002708 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002709 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002710 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002711 if (isInstantiationOf(ClassTemplate,
2712 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002713 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002714
2715 if (!DC->isDependentContext())
2716 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002717 }
2718
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002719 // We're performing "instantiation" of a member of the current
2720 // instantiation while we are type-checking the
2721 // definition. Compute the declaration context and return that.
2722 assert(!SawNonDependentContext &&
2723 "No dependent context while instantiating record");
2724 DeclContext *DC = computeDeclContext(T);
2725 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002726 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002727 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002728 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002729
Douglas Gregore95b4092009-09-16 18:34:49 +00002730 // Fall through to deal with other dependent record types (e.g.,
2731 // anonymous unions in class templates).
2732 }
John McCall52a575a2009-08-29 08:11:13 +00002733
Douglas Gregore95b4092009-09-16 18:34:49 +00002734 if (!ParentDC->isDependentContext())
2735 return D;
2736
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002737 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002738 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002739 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002740
Douglas Gregor815215d2009-05-27 05:35:12 +00002741 if (ParentDC != D->getDeclContext()) {
2742 // We performed some kind of instantiation in the parent context,
2743 // so now we need to look into the instantiated parent context to
2744 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002745
John McCall3cb0ebd2010-03-10 03:28:59 +00002746 // If our context used to be dependent, we may need to instantiate
2747 // it before performing lookup into that context.
2748 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002749 if (!Spec->isDependentContext()) {
2750 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002751 const RecordType *Tag = T->getAs<RecordType>();
2752 assert(Tag && "type of non-dependent record is not a RecordType");
2753 if (!Tag->isBeingDefined() &&
2754 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2755 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002756
2757 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002758 }
2759 }
2760
Douglas Gregor815215d2009-05-27 05:35:12 +00002761 NamedDecl *Result = 0;
2762 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002763 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002764 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2765 } else {
2766 // Since we don't have a name for the entity we're looking for,
2767 // our only option is to walk through all of the declarations to
2768 // find that name. This will occur in a few cases:
2769 //
2770 // - anonymous struct/union within a template
2771 // - unnamed class/struct/union/enum within a template
2772 //
2773 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002774 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002775 ParentDC->decls_begin(),
2776 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002777 }
Mike Stump1eb44332009-09-09 15:08:12 +00002778
John McCall9f54ad42009-12-10 09:41:52 +00002779 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002780 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2781 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002782 && "Unable to find instantiation of declaration!");
2783
Douglas Gregor815215d2009-05-27 05:35:12 +00002784 D = Result;
2785 }
2786
Douglas Gregor815215d2009-05-27 05:35:12 +00002787 return D;
2788}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002789
Mike Stump1eb44332009-09-09 15:08:12 +00002790/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002791/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002792void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002793 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00002794 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002795 PendingImplicitInstantiation Inst;
2796
2797 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002798 Inst = PendingInstantiations.front();
2799 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00002800 } else {
2801 Inst = PendingLocalImplicitInstantiations.front();
2802 PendingLocalImplicitInstantiations.pop_front();
2803 }
Mike Stump1eb44332009-09-09 15:08:12 +00002804
Douglas Gregor7caa6822009-07-24 20:34:43 +00002805 // Instantiate function definitions
2806 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00002807 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2808 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00002809 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
2810 TSK_ExplicitInstantiationDefinition;
2811 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
2812 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002813 continue;
2814 }
Mike Stump1eb44332009-09-09 15:08:12 +00002815
Douglas Gregor7caa6822009-07-24 20:34:43 +00002816 // Instantiate static data member definitions.
2817 VarDecl *Var = cast<VarDecl>(Inst.first);
2818 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002819
Chandler Carruth291b4412010-02-13 10:17:50 +00002820 // Don't try to instantiate declarations if the most recent redeclaration
2821 // is invalid.
2822 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2823 continue;
2824
2825 // Check if the most recent declaration has changed the specialization kind
2826 // and removed the need for implicit instantiation.
2827 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2828 case TSK_Undeclared:
2829 assert(false && "Cannot instantitiate an undeclared specialization.");
2830 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00002831 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00002832 continue; // No longer need to instantiate this type.
2833 case TSK_ExplicitInstantiationDefinition:
2834 // We only need an instantiation if the pending instantiation *is* the
2835 // explicit instantiation.
2836 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00002837 case TSK_ImplicitInstantiation:
2838 break;
2839 }
2840
John McCallf312b1e2010-08-26 23:41:50 +00002841 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2842 "instantiating static data member "
2843 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Chandler Carruth58e390e2010-08-25 08:27:02 +00002845 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
2846 TSK_ExplicitInstantiationDefinition;
2847 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
2848 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002849 }
2850}
John McCall0c01d182010-03-24 05:22:00 +00002851
2852void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2853 const MultiLevelTemplateArgumentList &TemplateArgs) {
2854 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2855 E = Pattern->ddiag_end(); I != E; ++I) {
2856 DependentDiagnostic *DD = *I;
2857
2858 switch (DD->getKind()) {
2859 case DependentDiagnostic::Access:
2860 HandleDependentAccessCheck(*DD, TemplateArgs);
2861 break;
2862 }
2863 }
2864}