blob: aea25c8dad026db7e9dec0903e579dbbbbb994b4 [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
Nick Lewycky37574f52010-11-08 23:29:42 +0000725 if (!isFriend && Pattern->getPreviousDeclaration()) {
726 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
727 if (Found.first != Found.second) {
728 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
729 if (PrevClassTemplate)
730 PrevDecl = PrevClassTemplate->getTemplatedDecl();
731 }
732 }
733
John McCall93ba8572010-03-25 06:39:04 +0000734 // If this isn't a friend, then it's a member template, in which
735 // case we just want to build the instantiation in the
736 // specialization. If it is a friend, we want to build it in
737 // the appropriate context.
738 DeclContext *DC = Owner;
739 if (isFriend) {
740 if (Qualifier) {
741 CXXScopeSpec SS;
742 SS.setScopeRep(Qualifier);
743 SS.setRange(Pattern->getQualifierRange());
744 DC = SemaRef.computeDeclContext(SS);
745 if (!DC) return 0;
746 } else {
747 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
748 Pattern->getDeclContext(),
749 TemplateArgs);
750 }
751
752 // Look for a previous declaration of the template in the owning
753 // context.
754 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
755 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
756 SemaRef.LookupQualifiedName(R, DC);
757
758 if (R.isSingleResult()) {
759 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
760 if (PrevClassTemplate)
761 PrevDecl = PrevClassTemplate->getTemplatedDecl();
762 }
763
764 if (!PrevClassTemplate && Qualifier) {
765 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000766 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
767 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000768 return 0;
769 }
770
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000771 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000772 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000773 bool Complain = true;
774
775 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
776 // template for struct std::tr1::__detail::_Map_base, where the
777 // template parameters of the friend declaration don't match the
778 // template parameters of the original declaration. In this one
779 // case, we don't complain about the ill-formed friend
780 // declaration.
781 if (isFriend && Pattern->getIdentifier() &&
782 Pattern->getIdentifier()->isStr("_Map_base") &&
783 DC->isNamespace() &&
784 cast<NamespaceDecl>(DC)->getIdentifier() &&
785 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
786 DeclContext *DCParent = DC->getParent();
787 if (DCParent->isNamespace() &&
788 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
789 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
790 DeclContext *DCParent2 = DCParent->getParent();
791 if (DCParent2->isNamespace() &&
792 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
793 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
794 DCParent2->getParent()->isTranslationUnit())
795 Complain = false;
796 }
797 }
798
John McCall93ba8572010-03-25 06:39:04 +0000799 TemplateParameterList *PrevParams
800 = PrevClassTemplate->getTemplateParameters();
801
802 // Make sure the parameter lists match.
803 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000804 Complain,
805 Sema::TPL_TemplateMatch)) {
806 if (Complain)
807 return 0;
808
809 AdoptedPreviousTemplateParams = true;
810 InstParams = PrevParams;
811 }
John McCall93ba8572010-03-25 06:39:04 +0000812
813 // Do some additional validation, then merge default arguments
814 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000815 if (!AdoptedPreviousTemplateParams &&
816 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000817 Sema::TPC_ClassTemplate))
818 return 0;
819 }
820 }
821
John McCalle29ba202009-08-20 01:44:21 +0000822 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000823 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000824 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000825 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000826 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000827
John McCall93ba8572010-03-25 06:39:04 +0000828 if (Qualifier)
829 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000830
John McCalle29ba202009-08-20 01:44:21 +0000831 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000832 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
833 D->getIdentifier(), InstParams, RecordInst,
834 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000835 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000836
John McCall93ba8572010-03-25 06:39:04 +0000837 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000838 if (PrevClassTemplate)
839 Inst->setAccess(PrevClassTemplate->getAccess());
840 else
841 Inst->setAccess(D->getAccess());
842
John McCall93ba8572010-03-25 06:39:04 +0000843 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
844 // TODO: do we want to track the instantiation progeny of this
845 // friend target decl?
846 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000847 Inst->setAccess(D->getAccess());
Nick Lewycky37574f52010-11-08 23:29:42 +0000848 if (!PrevClassTemplate)
849 Inst->setInstantiatedFromMemberTemplate(D);
John McCall93ba8572010-03-25 06:39:04 +0000850 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000851
852 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000853 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000854 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000855
Douglas Gregor259571e2009-10-30 22:42:42 +0000856 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000857 if (isFriend) {
858 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000859 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000860 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000861
John McCalle29ba202009-08-20 01:44:21 +0000862 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000863
Douglas Gregored9c0f92009-10-29 00:04:11 +0000864 // Instantiate all of the partial specializations of this member class
865 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000866 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
867 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000868 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
869 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
870
John McCalle29ba202009-08-20 01:44:21 +0000871 return Inst;
872}
873
Douglas Gregord60e1052009-08-27 16:57:43 +0000874Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000875TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
876 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000877 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
878
879 // Lookup the already-instantiated declaration in the instantiation
880 // of the class template and return that.
881 DeclContext::lookup_result Found
882 = Owner->lookup(ClassTemplate->getDeclName());
883 if (Found.first == Found.second)
884 return 0;
885
886 ClassTemplateDecl *InstClassTemplate
887 = dyn_cast<ClassTemplateDecl>(*Found.first);
888 if (!InstClassTemplate)
889 return 0;
890
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000891 return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000892}
893
894Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000895TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000896 // Create a local instantiation scope for this function template, which
897 // will contain the instantiations of the template parameters and then get
898 // merged with the local instantiation scope for the function template
899 // itself.
John McCall2a7fb272010-08-25 05:32:35 +0000900 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000901
Douglas Gregord60e1052009-08-27 16:57:43 +0000902 TemplateParameterList *TempParams = D->getTemplateParameters();
903 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000904 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000905 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000906
Douglas Gregora735b202009-10-13 14:39:41 +0000907 FunctionDecl *Instantiated = 0;
908 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
909 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
910 InstParams));
911 else
912 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
913 D->getTemplatedDecl(),
914 InstParams));
915
916 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000917 return 0;
918
John McCall46460a62010-01-20 21:53:11 +0000919 Instantiated->setAccess(D->getAccess());
920
Mike Stump1eb44332009-09-09 15:08:12 +0000921 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000922 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000923 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000924 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000925 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000926 assert(InstTemplate &&
927 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000928
John McCallb1a56e72010-03-26 23:10:15 +0000929 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
930
John McCalle976ffe2009-12-14 23:19:40 +0000931 // Link the instantiation back to the pattern *unless* this is a
932 // non-definition friend declaration.
933 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000934 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000935 InstTemplate->setInstantiatedFromMemberTemplate(D);
936
John McCallb1a56e72010-03-26 23:10:15 +0000937 // Make declarations visible in the appropriate context.
938 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000939 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000940
Douglas Gregord60e1052009-08-27 16:57:43 +0000941 return InstTemplate;
942}
943
Douglas Gregord475b8d2009-03-25 21:17:03 +0000944Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
945 CXXRecordDecl *PrevDecl = 0;
946 if (D->isInjectedClassName())
947 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000948 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000949 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
950 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000951 TemplateArgs);
952 if (!Prev) return 0;
953 PrevDecl = cast<CXXRecordDecl>(Prev);
954 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000955
956 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000957 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000958 D->getLocation(), D->getIdentifier(),
959 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000960
961 // Substitute the nested name specifier, if any.
962 if (SubstQualifier(D, Record))
963 return 0;
964
Douglas Gregord475b8d2009-03-25 21:17:03 +0000965 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000966 // FIXME: Check against AS_none is an ugly hack to work around the issue that
967 // the tag decls introduced by friend class declarations don't have an access
968 // specifier. Remove once this area of the code gets sorted out.
969 if (D->getAccess() != AS_none)
970 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000971 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000972 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000973
John McCall02cace72009-08-28 07:59:38 +0000974 // If the original function was part of a friend declaration,
975 // inherit its namespace state.
976 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
977 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
978
Douglas Gregor9901c572010-05-21 00:31:19 +0000979 // Make sure that anonymous structs and unions are recorded.
980 if (D->isAnonymousStructOrUnion()) {
981 Record->setAnonymousStructOrUnion(true);
Sebastian Redl7a126a42010-08-31 00:36:30 +0000982 if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
Douglas Gregor9901c572010-05-21 00:31:19 +0000983 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
984 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000985
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000986 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000987 return Record;
988}
989
John McCall02cace72009-08-28 07:59:38 +0000990/// Normal class members are of more specific types and therefore
991/// don't make it here. This function serves two purposes:
992/// 1) instantiating function templates
993/// 2) substituting friend declarations
994/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000995Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000996 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000997 // Check whether there is already a function template specialization for
998 // this declaration.
999 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1000 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +00001001 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +00001002 std::pair<const TemplateArgument *, unsigned> Innermost
1003 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001005 FunctionDecl *SpecFunc
1006 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1007 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregor127102b2009-06-29 20:59:39 +00001009 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001010 if (SpecFunc)
1011 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +00001012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
John McCallb0cb0222010-03-27 05:57:59 +00001014 bool isFriend;
1015 if (FunctionTemplate)
1016 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1017 else
1018 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1019
Douglas Gregor79c22782010-01-16 20:21:20 +00001020 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +00001021 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +00001022 !(isa<Decl>(Owner) &&
1023 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001024 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregore53060f2009-06-25 22:08:12 +00001026 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001027 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1028 TInfo = SubstFunctionType(D, Params);
1029 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001030 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001031 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +00001032
John McCalld325daa2010-03-26 04:53:08 +00001033 NestedNameSpecifier *Qualifier = D->getQualifier();
1034 if (Qualifier) {
1035 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1036 D->getQualifierRange(),
1037 TemplateArgs);
1038 if (!Qualifier) return 0;
1039 }
1040
John McCall68b6b872010-02-06 01:50:47 +00001041 // If we're instantiating a local function declaration, put the result
1042 // in the owner; otherwise we need to find the instantiated context.
1043 DeclContext *DC;
1044 if (D->getDeclContext()->isFunctionOrMethod())
1045 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001046 else if (isFriend && Qualifier) {
1047 CXXScopeSpec SS;
1048 SS.setScopeRep(Qualifier);
1049 SS.setRange(D->getQualifierRange());
1050 DC = SemaRef.computeDeclContext(SS);
1051 if (!DC) return 0;
1052 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001053 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1054 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001055 }
John McCall68b6b872010-02-06 01:50:47 +00001056
John McCall02cace72009-08-28 07:59:38 +00001057 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001058 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001059 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001060 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001061 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001062
John McCalld325daa2010-03-26 04:53:08 +00001063 if (Qualifier)
1064 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001065
John McCallb1a56e72010-03-26 23:10:15 +00001066 DeclContext *LexicalDC = Owner;
1067 if (!isFriend && D->isOutOfLine()) {
1068 assert(D->getDeclContext()->isFileContext());
1069 LexicalDC = D->getDeclContext();
1070 }
1071
1072 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Douglas Gregore53060f2009-06-25 22:08:12 +00001074 // Attach the parameters
1075 for (unsigned P = 0; P < Params.size(); ++P)
John McCall3019c442010-09-17 00:50:28 +00001076 if (Params[P])
1077 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001078 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001079
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001080 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001081 if (TemplateParams) {
1082 // Our resulting instantiation is actually a function template, since we
1083 // are substituting only the outer template parameters. For example, given
1084 //
1085 // template<typename T>
1086 // struct X {
1087 // template<typename U> friend void f(T, U);
1088 // };
1089 //
1090 // X<int> x;
1091 //
1092 // We are instantiating the friend function template "f" within X<int>,
1093 // which means substituting int for T, but leaving "f" as a friend function
1094 // template.
1095 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001096 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001097 Function->getLocation(),
1098 Function->getDeclName(),
1099 TemplateParams, Function);
1100 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001101
1102 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001103
1104 if (isFriend && D->isThisDeclarationADefinition()) {
1105 // TODO: should we remember this connection regardless of whether
1106 // the friend declaration provided a body?
1107 FunctionTemplate->setInstantiatedFromMemberTemplate(
1108 D->getDescribedFunctionTemplate());
1109 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001110 } else if (FunctionTemplate) {
1111 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001112 std::pair<const TemplateArgument *, unsigned> Innermost
1113 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001114 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001115 TemplateArgumentList::CreateCopy(SemaRef.Context,
Douglas Gregor24bae922010-07-08 18:37:38 +00001116 Innermost.first,
1117 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001118 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001119 } else if (isFriend && D->isThisDeclarationADefinition()) {
1120 // TODO: should we remember this connection regardless of whether
1121 // the friend declaration provided a body?
1122 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001123 }
Douglas Gregora735b202009-10-13 14:39:41 +00001124
Douglas Gregore53060f2009-06-25 22:08:12 +00001125 if (InitFunctionInstantiation(Function, D))
1126 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Douglas Gregore53060f2009-06-25 22:08:12 +00001128 bool Redeclaration = false;
1129 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001130 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001131
John McCall68263142009-11-18 22:49:29 +00001132 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1133 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1134
John McCallaf2094e2010-04-08 09:05:18 +00001135 if (DependentFunctionTemplateSpecializationInfo *Info
1136 = D->getDependentSpecializationInfo()) {
1137 assert(isFriend && "non-friend has dependent specialization info?");
1138
1139 // This needs to be set now for future sanity.
1140 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1141
1142 // Instantiate the explicit template arguments.
1143 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1144 Info->getRAngleLoc());
1145 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1146 TemplateArgumentLoc Loc;
1147 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1148 return 0;
1149
1150 ExplicitArgs.addArgument(Loc);
1151 }
1152
1153 // Map the candidate templates to their instantiations.
1154 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1155 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1156 Info->getTemplate(I),
1157 TemplateArgs);
1158 if (!Temp) return 0;
1159
1160 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1161 }
1162
1163 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1164 &ExplicitArgs,
1165 Previous))
1166 Function->setInvalidDecl();
1167
1168 isExplicitSpecialization = true;
1169
1170 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001171 // Look only into the namespace where the friend would be declared to
1172 // find a previous declaration. This is the innermost enclosing namespace,
1173 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001174 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001175
Douglas Gregora735b202009-10-13 14:39:41 +00001176 // In C++, the previous declaration we find might be a tag type
1177 // (class or enum). In this case, the new declaration will hide the
1178 // tag type. Note that this does does not apply if we're declaring a
1179 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001180 if (Previous.isSingleTagDecl())
1181 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001182 }
1183
John McCall9f54ad42009-12-10 09:41:52 +00001184 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001185 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001186 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001187
John McCall76d32642010-04-24 01:30:58 +00001188 NamedDecl *PrincipalDecl = (TemplateParams
1189 ? cast<NamedDecl>(FunctionTemplate)
1190 : Function);
1191
Douglas Gregora735b202009-10-13 14:39:41 +00001192 // If the original function was part of a friend declaration,
1193 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001194 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001195 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001196 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001197 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001198 else
Douglas Gregora735b202009-10-13 14:39:41 +00001199 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001200
1201 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1202 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Gabor Greifab297ac2010-08-30 21:10:05 +00001203
Gabor Greif77535df2010-08-30 22:25:56 +00001204 bool queuedInstantiation = false;
Gabor Greifab297ac2010-08-30 21:10:05 +00001205
Douglas Gregor238058c2010-05-18 05:45:02 +00001206 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1207 D->isThisDeclarationADefinition()) {
1208 // Check for a function body.
1209 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001210 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001211 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1212 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1213 << Function->getDeclName();
1214 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1215 Function->setInvalidDecl();
1216 }
1217 // Check for redefinitions due to other instantiations of this or
1218 // a similar friend function.
1219 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1220 REnd = Function->redecls_end();
1221 R != REnd; ++R) {
Gabor Greif13a8aff2010-08-28 15:42:30 +00001222 if (*R == Function)
1223 continue;
Gabor Greifab297ac2010-08-30 21:10:05 +00001224 switch (R->getFriendObjectKind()) {
1225 case Decl::FOK_None:
1226 if (!queuedInstantiation && R->isUsed(false)) {
1227 if (MemberSpecializationInfo *MSInfo
1228 = Function->getMemberSpecializationInfo()) {
1229 if (MSInfo->getPointOfInstantiation().isInvalid()) {
1230 SourceLocation Loc = R->getLocation(); // FIXME
1231 MSInfo->setPointOfInstantiation(Loc);
1232 SemaRef.PendingLocalImplicitInstantiations.push_back(
1233 std::make_pair(Function, Loc));
1234 queuedInstantiation = true;
1235 }
1236 }
1237 }
1238 break;
1239 default:
Douglas Gregor238058c2010-05-18 05:45:02 +00001240 if (const FunctionDecl *RPattern
Gabor Greif6a557d82010-08-28 15:46:56 +00001241 = R->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001242 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001243 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1244 << Function->getDeclName();
Gabor Greif6a557d82010-08-28 15:46:56 +00001245 SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
Douglas Gregor238058c2010-05-18 05:45:02 +00001246 Function->setInvalidDecl();
1247 break;
1248 }
1249 }
1250 }
1251 }
Douglas Gregora735b202009-10-13 14:39:41 +00001252 }
1253
John McCall76d32642010-04-24 01:30:58 +00001254 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1255 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1256 PrincipalDecl->setNonMemberOperator();
1257
Douglas Gregore53060f2009-06-25 22:08:12 +00001258 return Function;
1259}
1260
Douglas Gregord60e1052009-08-27 16:57:43 +00001261Decl *
1262TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1263 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001264 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1265 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001266 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001267 // We are creating a function template specialization from a function
1268 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001269 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001270 std::pair<const TemplateArgument *, unsigned> Innermost
1271 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001273 FunctionDecl *SpecFunc
1274 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1275 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Douglas Gregor6b906862009-08-21 00:16:32 +00001277 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001278 if (SpecFunc)
1279 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001280 }
1281
John McCallb0cb0222010-03-27 05:57:59 +00001282 bool isFriend;
1283 if (FunctionTemplate)
1284 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1285 else
1286 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1287
Douglas Gregor79c22782010-01-16 20:21:20 +00001288 bool MergeWithParentScope = (TemplateParams != 0) ||
1289 !(isa<Decl>(Owner) &&
1290 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
John McCall2a7fb272010-08-25 05:32:35 +00001291 LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001292
John McCall4eab39f2010-10-19 02:26:41 +00001293 // Instantiate enclosing template arguments for friends.
1294 llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
1295 unsigned NumTempParamLists = 0;
1296 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1297 TempParamLists.set_size(NumTempParamLists);
1298 for (unsigned I = 0; I != NumTempParamLists; ++I) {
1299 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1300 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1301 if (!InstParams)
1302 return NULL;
1303 TempParamLists[I] = InstParams;
1304 }
1305 }
1306
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001307 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001308 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1309 TInfo = SubstFunctionType(D, Params);
1310 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001311 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001312 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001313
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001314 // \brief If the type of this function is not *directly* a function
1315 // type, then we're instantiating the a function that was declared
1316 // via a typedef, e.g.,
1317 //
1318 // typedef int functype(int, int);
1319 // functype func;
1320 //
1321 // In this case, we'll just go instantiate the ParmVarDecls that we
1322 // synthesized in the method declaration.
1323 if (!isa<FunctionProtoType>(T)) {
1324 assert(!Params.size() && "Instantiating type could not yield parameters");
1325 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1326 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1327 TemplateArgs);
1328 if (!P)
1329 return 0;
1330
1331 Params.push_back(P);
1332 }
1333 }
1334
John McCallb0cb0222010-03-27 05:57:59 +00001335 NestedNameSpecifier *Qualifier = D->getQualifier();
1336 if (Qualifier) {
1337 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1338 D->getQualifierRange(),
1339 TemplateArgs);
1340 if (!Qualifier) return 0;
1341 }
1342
1343 DeclContext *DC = Owner;
1344 if (isFriend) {
1345 if (Qualifier) {
1346 CXXScopeSpec SS;
1347 SS.setScopeRep(Qualifier);
1348 SS.setRange(D->getQualifierRange());
1349 DC = SemaRef.computeDeclContext(SS);
John McCallc54d6882010-10-19 05:01:53 +00001350
1351 if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1352 return 0;
John McCallb0cb0222010-03-27 05:57:59 +00001353 } else {
1354 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1355 D->getDeclContext(),
1356 TemplateArgs);
1357 }
1358 if (!DC) return 0;
1359 }
1360
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001361 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001362 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001363 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Abramo Bagnara25777432010-08-11 22:01:17 +00001365 DeclarationNameInfo NameInfo
1366 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001367 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001368 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001369 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001370 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001371 Constructor->isInlineSpecified(),
1372 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001373 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001374 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001375 NameInfo, T, TInfo,
Abramo Bagnara25777432010-08-11 22:01:17 +00001376 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001377 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001378 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001379 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001380 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001381 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001382 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001383 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001384 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1385 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001386 D->isStatic(),
1387 D->getStorageClassAsWritten(),
1388 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001389 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001390
John McCallb0cb0222010-03-27 05:57:59 +00001391 if (Qualifier)
1392 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001393
Douglas Gregord60e1052009-08-27 16:57:43 +00001394 if (TemplateParams) {
1395 // Our resulting instantiation is actually a function template, since we
1396 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001397 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001398 // template<typename T>
1399 // struct X {
1400 // template<typename U> void f(T, U);
1401 // };
1402 //
1403 // X<int> x;
1404 //
1405 // We are instantiating the member template "f" within X<int>, which means
1406 // substituting int for T, but leaving "f" as a member function template.
1407 // Build the function template itself.
1408 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1409 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001410 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001411 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001412 if (isFriend) {
1413 FunctionTemplate->setLexicalDeclContext(Owner);
1414 FunctionTemplate->setObjectOfFriendDecl(true);
1415 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001416 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001417 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001418 } else if (FunctionTemplate) {
1419 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001420 std::pair<const TemplateArgument *, unsigned> Innermost
1421 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001422 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001423 TemplateArgumentList::CreateCopy(SemaRef.Context,
1424 Innermost.first,
1425 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001426 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001427 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001428 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001429 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001430 }
1431
Mike Stump1eb44332009-09-09 15:08:12 +00001432 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001433 // out-of-line, the instantiation will have the same lexical
1434 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001435 if (isFriend) {
John McCall4eab39f2010-10-19 02:26:41 +00001436 if (NumTempParamLists)
1437 Method->setTemplateParameterListsInfo(SemaRef.Context,
1438 NumTempParamLists,
1439 TempParamLists.data());
1440
John McCallb0cb0222010-03-27 05:57:59 +00001441 Method->setLexicalDeclContext(Owner);
1442 Method->setObjectOfFriendDecl(true);
1443 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001444 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Douglas Gregor5545e162009-03-24 00:38:23 +00001446 // Attach the parameters
1447 for (unsigned P = 0; P < Params.size(); ++P)
1448 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001449 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001450
1451 if (InitMethodInstantiation(Method, D))
1452 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001453
Abramo Bagnara25777432010-08-11 22:01:17 +00001454 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1455 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001456
John McCallb0cb0222010-03-27 05:57:59 +00001457 if (!FunctionTemplate || TemplateParams || isFriend) {
1458 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Douglas Gregordec06662009-08-21 18:42:58 +00001460 // In C++, the previous declaration we find might be a tag type
1461 // (class or enum). In this case, the new declaration will hide the
1462 // tag type. Note that this does does not apply if we're declaring a
1463 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001464 if (Previous.isSingleTagDecl())
1465 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001466 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001467
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001468 bool Redeclaration = false;
1469 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001470 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001471 /*FIXME:*/OverloadableAttrRequired);
1472
Douglas Gregor4ba31362009-12-01 17:24:26 +00001473 if (D->isPure())
1474 SemaRef.CheckPureMethod(Method, SourceRange());
1475
John McCall46460a62010-01-20 21:53:11 +00001476 Method->setAccess(D->getAccess());
1477
John McCallb0cb0222010-03-27 05:57:59 +00001478 if (FunctionTemplate) {
1479 // If there's a function template, let our caller handle it.
1480 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1481 // Don't hide a (potentially) valid declaration with an invalid one.
1482 } else {
1483 NamedDecl *DeclToAdd = (TemplateParams
1484 ? cast<NamedDecl>(FunctionTemplate)
1485 : Method);
1486 if (isFriend)
1487 Record->makeDeclVisibleInContext(DeclToAdd);
1488 else
1489 Owner->addDecl(DeclToAdd);
1490 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001491
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001492 return Method;
1493}
1494
Douglas Gregor615c5d42009-03-24 16:43:20 +00001495Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001496 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001497}
1498
Douglas Gregor03b2b072009-03-24 00:15:49 +00001499Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001500 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001501}
1502
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001503Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001504 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001505}
1506
Douglas Gregor6477b692009-03-25 15:04:13 +00001507ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001508 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001509}
1510
John McCalle29ba202009-08-20 01:44:21 +00001511Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1512 TemplateTypeParmDecl *D) {
1513 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001514 const Type* T = D->getTypeForDecl();
1515 assert(T->isTemplateTypeParmType());
1516 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001517
John McCalle29ba202009-08-20 01:44:21 +00001518 TemplateTypeParmDecl *Inst =
1519 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001520 TTPT->getDepth() - TemplateArgs.getNumLevels(),
Nick Lewycky61139c52010-10-30 06:48:20 +00001521 TTPT->getIndex(), D->getIdentifier(),
John McCalle29ba202009-08-20 01:44:21 +00001522 D->wasDeclaredWithTypename(),
1523 D->isParameterPack());
1524
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001525 if (D->hasDefaultArgument())
1526 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001527
Douglas Gregor550d9b22009-10-31 17:21:17 +00001528 // Introduce this template parameter's instantiation into the instantiation
1529 // scope.
1530 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1531
John McCalle29ba202009-08-20 01:44:21 +00001532 return Inst;
1533}
1534
Douglas Gregor33642df2009-10-23 23:25:44 +00001535Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1536 NonTypeTemplateParmDecl *D) {
1537 // Substitute into the type of the non-type template parameter.
1538 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001539 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001540 if (DI) {
1541 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1542 D->getDeclName());
1543 if (DI) T = DI->getType();
1544 } else {
1545 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1546 D->getDeclName());
1547 DI = 0;
1548 }
1549 if (T.isNull())
1550 return 0;
1551
1552 // Check that this type is acceptable for a non-type template parameter.
1553 bool Invalid = false;
1554 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1555 if (T.isNull()) {
1556 T = SemaRef.Context.IntTy;
1557 Invalid = true;
1558 }
1559
1560 NonTypeTemplateParmDecl *Param
1561 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001562 D->getDepth() - TemplateArgs.getNumLevels(),
1563 D->getPosition(), D->getIdentifier(), T,
1564 DI);
Douglas Gregor33642df2009-10-23 23:25:44 +00001565 if (Invalid)
1566 Param->setInvalidDecl();
1567
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001568 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001569
1570 // Introduce this template parameter's instantiation into the instantiation
1571 // scope.
1572 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001573 return Param;
1574}
1575
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001576Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001577TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1578 TemplateTemplateParmDecl *D) {
1579 // Instantiate the template parameter list of the template template parameter.
1580 TemplateParameterList *TempParams = D->getTemplateParameters();
1581 TemplateParameterList *InstParams;
1582 {
1583 // Perform the actual substitution of template parameters within a new,
1584 // local instantiation scope.
John McCall2a7fb272010-08-25 05:32:35 +00001585 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor9106ef72009-11-11 16:58:32 +00001586 InstParams = SubstTemplateParams(TempParams);
1587 if (!InstParams)
1588 return NULL;
1589 }
1590
1591 // Build the template template parameter.
1592 TemplateTemplateParmDecl *Param
1593 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor71b87e42010-08-30 23:23:59 +00001594 D->getDepth() - TemplateArgs.getNumLevels(),
1595 D->getPosition(), D->getIdentifier(),
1596 InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001597 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001598
Douglas Gregor9106ef72009-11-11 16:58:32 +00001599 // Introduce this template parameter's instantiation into the instantiation
1600 // scope.
1601 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1602
1603 return Param;
1604}
1605
Douglas Gregor48c32a72009-11-17 06:07:40 +00001606Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1607 // Using directives are never dependent, so they require no explicit
1608
1609 UsingDirectiveDecl *Inst
1610 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1611 D->getNamespaceKeyLocation(),
1612 D->getQualifierRange(), D->getQualifier(),
1613 D->getIdentLocation(),
1614 D->getNominatedNamespace(),
1615 D->getCommonAncestor());
1616 Owner->addDecl(Inst);
1617 return Inst;
1618}
1619
John McCalled976492009-12-04 22:46:56 +00001620Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
Douglas Gregor1b398202010-09-29 17:58:28 +00001621
1622 // The nested name specifier may be dependent, for example
1623 // template <typename T> struct t {
1624 // struct s1 { T f1(); };
1625 // struct s2 : s1 { using s1::f1; };
1626 // };
1627 // template struct t<int>;
1628 // Here, in using s1::f1, s1 refers to t<T>::s1;
1629 // we need to substitute for t<int>::s1.
1630 NestedNameSpecifier *NNS =
1631 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
1632 D->getNestedNameRange(),
1633 TemplateArgs);
1634 if (!NNS)
1635 return 0;
1636
1637 // The name info is non-dependent, so no transformation
1638 // is required.
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001639 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001640
John McCall9f54ad42009-12-10 09:41:52 +00001641 // We only need to do redeclaration lookups if we're in a class
1642 // scope (in fact, it's not really even possible in non-class
1643 // scopes).
1644 bool CheckRedeclaration = Owner->isRecord();
1645
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001646 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1647 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001648
John McCalled976492009-12-04 22:46:56 +00001649 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001650 D->getNestedNameRange(),
1651 D->getUsingLocation(),
Douglas Gregor1b398202010-09-29 17:58:28 +00001652 NNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001653 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001654 D->isTypeName());
1655
1656 CXXScopeSpec SS;
Douglas Gregor1b398202010-09-29 17:58:28 +00001657 SS.setScopeRep(NNS);
John McCalled976492009-12-04 22:46:56 +00001658 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001659
1660 if (CheckRedeclaration) {
1661 Prev.setHideTags(false);
1662 SemaRef.LookupQualifiedName(Prev, Owner);
1663
1664 // Check for invalid redeclarations.
1665 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1666 D->isTypeName(), SS,
1667 D->getLocation(), Prev))
1668 NewUD->setInvalidDecl();
1669
1670 }
1671
1672 if (!NewUD->isInvalidDecl() &&
1673 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001674 D->getLocation()))
1675 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001676
John McCalled976492009-12-04 22:46:56 +00001677 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1678 NewUD->setAccess(D->getAccess());
1679 Owner->addDecl(NewUD);
1680
John McCall9f54ad42009-12-10 09:41:52 +00001681 // Don't process the shadow decls for an invalid decl.
1682 if (NewUD->isInvalidDecl())
1683 return NewUD;
1684
John McCall323c3102009-12-22 22:26:37 +00001685 bool isFunctionScope = Owner->isFunctionOrMethod();
1686
John McCall9f54ad42009-12-10 09:41:52 +00001687 // Process the shadow decls.
1688 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1689 I != E; ++I) {
1690 UsingShadowDecl *Shadow = *I;
1691 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001692 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1693 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001694 TemplateArgs));
1695
1696 if (CheckRedeclaration &&
1697 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1698 continue;
1699
1700 UsingShadowDecl *InstShadow
1701 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1702 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001703
1704 if (isFunctionScope)
1705 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001706 }
John McCalled976492009-12-04 22:46:56 +00001707
1708 return NewUD;
1709}
1710
1711Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001712 // Ignore these; we handle them in bulk when processing the UsingDecl.
1713 return 0;
John McCalled976492009-12-04 22:46:56 +00001714}
1715
John McCall7ba107a2009-11-18 02:36:19 +00001716Decl * TemplateDeclInstantiator
1717 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001718 NestedNameSpecifier *NNS =
1719 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1720 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001721 TemplateArgs);
1722 if (!NNS)
1723 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001725 CXXScopeSpec SS;
1726 SS.setRange(D->getTargetNestedNameRange());
1727 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001729 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1730 // Hence, no tranformation is required for it.
1731 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001732 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001733 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001734 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001735 /*instantiation*/ true,
1736 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001737 if (UD)
John McCalled976492009-12-04 22:46:56 +00001738 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1739
John McCall7ba107a2009-11-18 02:36:19 +00001740 return UD;
1741}
1742
1743Decl * TemplateDeclInstantiator
1744 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1745 NestedNameSpecifier *NNS =
1746 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1747 D->getTargetNestedNameRange(),
1748 TemplateArgs);
1749 if (!NNS)
1750 return 0;
1751
1752 CXXScopeSpec SS;
1753 SS.setRange(D->getTargetNestedNameRange());
1754 SS.setScopeRep(NNS);
1755
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001756 DeclarationNameInfo NameInfo
1757 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1758
John McCall7ba107a2009-11-18 02:36:19 +00001759 NamedDecl *UD =
1760 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001761 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001762 /*instantiation*/ true,
1763 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001764 if (UD)
John McCalled976492009-12-04 22:46:56 +00001765 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1766
Anders Carlsson0d8df782009-08-29 19:37:28 +00001767 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001768}
1769
John McCallce3ff2b2009-08-25 22:02:44 +00001770Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001771 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001772 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001773 if (D->isInvalidDecl())
1774 return 0;
1775
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001776 return Instantiator.Visit(D);
1777}
1778
John McCalle29ba202009-08-20 01:44:21 +00001779/// \brief Instantiates a nested template parameter list in the current
1780/// instantiation context.
1781///
1782/// \param L The parameter list to instantiate
1783///
1784/// \returns NULL if there was an error
1785TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001786TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001787 // Get errors for all the parameters before bailing out.
1788 bool Invalid = false;
1789
1790 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001791 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001792 ParamVector Params;
1793 Params.reserve(N);
1794 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1795 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001796 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001797 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001798 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001799 }
1800
1801 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001802 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001803 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001804
1805 TemplateParameterList *InstL
1806 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1807 L->getLAngleLoc(), &Params.front(), N,
1808 L->getRAngleLoc());
1809 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001810}
John McCalle29ba202009-08-20 01:44:21 +00001811
Douglas Gregored9c0f92009-10-29 00:04:11 +00001812/// \brief Instantiate the declaration of a class template partial
1813/// specialization.
1814///
1815/// \param ClassTemplate the (instantiated) class template that is partially
1816// specialized by the instantiation of \p PartialSpec.
1817///
1818/// \param PartialSpec the (uninstantiated) class template partial
1819/// specialization that we are instantiating.
1820///
1821/// \returns true if there was an error, false otherwise.
1822bool
1823TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1824 ClassTemplateDecl *ClassTemplate,
1825 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001826 // Create a local instantiation scope for this class template partial
1827 // specialization, which will contain the instantiations of the template
1828 // parameters.
John McCall2a7fb272010-08-25 05:32:35 +00001829 LocalInstantiationScope Scope(SemaRef);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001830
Douglas Gregored9c0f92009-10-29 00:04:11 +00001831 // Substitute into the template parameters of the class template partial
1832 // specialization.
1833 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1834 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1835 if (!InstParams)
1836 return true;
1837
1838 // Substitute into the template arguments of the class template partial
1839 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001840 const TemplateArgumentLoc *PartialSpecTemplateArgs
1841 = PartialSpec->getTemplateArgsAsWritten();
1842 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1843
John McCalld5532b62009-11-23 01:53:49 +00001844 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001845 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001846 TemplateArgumentLoc Loc;
1847 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001848 return true;
John McCalld5532b62009-11-23 01:53:49 +00001849 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001850 }
1851
1852
1853 // Check that the template argument list is well-formed for this
1854 // class template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001855 llvm::SmallVector<TemplateArgument, 4> Converted;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001856 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1857 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001858 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001859 false,
1860 Converted))
1861 return true;
1862
1863 // Figure out where to insert this class template partial specialization
1864 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001865 void *InsertPos = 0;
1866 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00001867 = ClassTemplate->findPartialSpecialization(Converted.data(),
1868 Converted.size(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001869
1870 // Build the canonical type that describes the converted template
1871 // arguments of the class template partial specialization.
1872 QualType CanonType
1873 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
Douglas Gregor910f8002010-11-07 23:05:16 +00001874 Converted.data(),
1875 Converted.size());
Douglas Gregored9c0f92009-10-29 00:04:11 +00001876
1877 // Build the fully-sugared type for this class template
1878 // specialization as the user wrote in the specialization
1879 // itself. This means that we'll pretty-print the type retrieved
1880 // from the specialization's declaration the way that the user
1881 // actually wrote the specialization, rather than formatting the
1882 // name based on the "canonical" representation used to store the
1883 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001884 TypeSourceInfo *WrittenTy
1885 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1886 TemplateName(ClassTemplate),
1887 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001888 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001889 CanonType);
1890
1891 if (PrevDecl) {
1892 // We've already seen a partial specialization with the same template
1893 // parameters and template arguments. This can happen, for example, when
1894 // substituting the outer template arguments ends up causing two
1895 // class template partial specializations of a member class template
1896 // to have identical forms, e.g.,
1897 //
1898 // template<typename T, typename U>
1899 // struct Outer {
1900 // template<typename X, typename Y> struct Inner;
1901 // template<typename Y> struct Inner<T, Y>;
1902 // template<typename Y> struct Inner<U, Y>;
1903 // };
1904 //
1905 // Outer<int, int> outer; // error: the partial specializations of Inner
1906 // // have the same signature.
1907 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1908 << WrittenTy;
1909 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1910 << SemaRef.Context.getTypeDeclType(PrevDecl);
1911 return true;
1912 }
1913
1914
1915 // Create the class template partial specialization declaration.
1916 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001917 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1918 PartialSpec->getTagKind(),
1919 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001920 PartialSpec->getLocation(),
1921 InstParams,
1922 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00001923 Converted.data(),
1924 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00001925 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001926 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001927 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001928 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001929 // Substitute the nested name specifier, if any.
1930 if (SubstQualifier(PartialSpec, InstPartialSpec))
1931 return 0;
1932
Douglas Gregored9c0f92009-10-29 00:04:11 +00001933 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001934 InstPartialSpec->setTypeAsWritten(WrittenTy);
1935
Douglas Gregored9c0f92009-10-29 00:04:11 +00001936 // Add this partial specialization to the set of class template partial
1937 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001938 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001939 return false;
1940}
1941
John McCall21ef0fa2010-03-11 09:03:00 +00001942TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001943TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001944 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001945 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1946 assert(OldTInfo && "substituting function without type source info");
1947 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001948 TypeSourceInfo *NewTInfo
1949 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1950 D->getTypeSpecStartLoc(),
1951 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001952 if (!NewTInfo)
1953 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001954
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001955 if (NewTInfo != OldTInfo) {
1956 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001957 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001958 if (FunctionProtoTypeLoc *OldProtoLoc
1959 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1960 TypeLoc NewTL = NewTInfo->getTypeLoc();
1961 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1962 assert(NewProtoLoc && "Missing prototype?");
1963 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1964 // FIXME: Variadic templates will break this.
1965 Params.push_back(NewProtoLoc->getArg(i));
1966 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001967 OldProtoLoc->getArg(i),
1968 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001969 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001970 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001971 } else {
1972 // The function type itself was not dependent and therefore no
1973 // substitution occurred. However, we still need to instantiate
1974 // the function parameters themselves.
1975 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001976 if (FunctionProtoTypeLoc *OldProtoLoc
1977 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1978 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1979 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1980 if (!Parm)
1981 return 0;
1982 Params.push_back(Parm);
1983 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001984 }
1985 }
John McCall21ef0fa2010-03-11 09:03:00 +00001986 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001987}
1988
Mike Stump1eb44332009-09-09 15:08:12 +00001989/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001990/// declaration (New) from the corresponding fields of its template (Tmpl).
1991///
1992/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001993bool
1994TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001995 FunctionDecl *Tmpl) {
1996 if (Tmpl->isDeleted())
1997 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Douglas Gregorcca9e962009-07-01 22:01:06 +00001999 // If we are performing substituting explicitly-specified template arguments
2000 // or deduced template arguments into a function template and we reach this
2001 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00002002 // to keeping the new function template specialization. We therefore
2003 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00002004 // into a template instantiation for this specific function template
2005 // specialization, which is not a SFINAE context, so that we diagnose any
2006 // further errors in the declaration itself.
2007 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2008 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2009 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2010 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00002011 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00002012 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002013 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00002014 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00002015 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002016 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2017 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002018 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00002019 }
2020 }
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002022 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
2023 assert(Proto && "Function template without prototype?");
2024
2025 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
2026 Proto->getNoReturnAttr()) {
2027 // The function has an exception specification or a "noreturn"
2028 // attribute. Substitute into each of the exception types.
2029 llvm::SmallVector<QualType, 4> Exceptions;
2030 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2031 // FIXME: Poor location information!
2032 QualType T
2033 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2034 New->getLocation(), New->getDeclName());
2035 if (T.isNull() ||
2036 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2037 continue;
2038
2039 Exceptions.push_back(T);
2040 }
2041
2042 // Rebuild the function type
2043
2044 const FunctionProtoType *NewProto
2045 = New->getType()->getAs<FunctionProtoType>();
2046 assert(NewProto && "Template instantiation without function prototype?");
2047 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2048 NewProto->arg_type_begin(),
2049 NewProto->getNumArgs(),
2050 NewProto->isVariadic(),
2051 NewProto->getTypeQuals(),
2052 Proto->hasExceptionSpec(),
2053 Proto->hasAnyExceptionSpec(),
2054 Exceptions.size(),
2055 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00002056 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00002057 }
2058
John McCall1d8d1cc2010-08-01 02:01:53 +00002059 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00002060
Douglas Gregore53060f2009-06-25 22:08:12 +00002061 return false;
2062}
2063
Douglas Gregor5545e162009-03-24 00:38:23 +00002064/// \brief Initializes common fields of an instantiated method
2065/// declaration (New) from the corresponding fields of its template
2066/// (Tmpl).
2067///
2068/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00002069bool
2070TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00002071 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00002072 if (InitFunctionInstantiation(New, Tmpl))
2073 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Douglas Gregor5545e162009-03-24 00:38:23 +00002075 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00002076 if (Tmpl->isVirtualAsWritten())
Douglas Gregor85606eb2010-09-28 20:50:54 +00002077 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00002078
2079 // FIXME: attributes
2080 // FIXME: New needs a pointer to Tmpl
2081 return false;
2082}
Douglas Gregora58861f2009-05-13 20:28:22 +00002083
2084/// \brief Instantiate the definition of the given function from its
2085/// template.
2086///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002087/// \param PointOfInstantiation the point at which the instantiation was
2088/// required. Note that this is not precisely a "point of instantiation"
2089/// for the function, but it's close.
2090///
Douglas Gregora58861f2009-05-13 20:28:22 +00002091/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002092/// function template specialization or member function of a class template
2093/// specialization.
2094///
2095/// \param Recursive if true, recursively instantiates any functions that
2096/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002097///
2098/// \param DefinitionRequired if true, then we are performing an explicit
2099/// instantiation where the body of the function is required. Complain if
2100/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002101void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002102 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002103 bool Recursive,
2104 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002105 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002106 return;
2107
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002108 // Never instantiate an explicit specialization.
2109 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2110 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002111
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002112 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002113 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002114 Stmt *Pattern = 0;
2115 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002116 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002117
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002118 if (!Pattern) {
2119 if (DefinitionRequired) {
2120 if (Function->getPrimaryTemplate())
2121 Diag(PointOfInstantiation,
2122 diag::err_explicit_instantiation_undefined_func_template)
2123 << Function->getPrimaryTemplate();
2124 else
2125 Diag(PointOfInstantiation,
2126 diag::err_explicit_instantiation_undefined_member)
2127 << 1 << Function->getDeclName() << Function->getDeclContext();
2128
2129 if (PatternDecl)
2130 Diag(PatternDecl->getLocation(),
2131 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002132 Function->setInvalidDecl();
Chandler Carruth58e390e2010-08-25 08:27:02 +00002133 } else if (Function->getTemplateSpecializationKind()
2134 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002135 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002136 std::make_pair(Function, PointOfInstantiation));
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002137 }
Chandler Carruth58e390e2010-08-25 08:27:02 +00002138
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002139 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002140 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002141
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002142 // C++0x [temp.explicit]p9:
2143 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002144 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002145 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002146 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002147 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002148 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002149 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002151 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2152 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002153 return;
2154
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002155 // If we're performing recursive template instantiation, create our own
2156 // queue of pending implicit instantiations that we will instantiate later,
2157 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002158 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002159 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002160 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Douglas Gregor9679caf2010-05-12 17:27:19 +00002162 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00002163 Sema::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002164 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002165
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002166 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002167 // recorded, unless we're actually a member function within a local
2168 // class, in which case we need to merge our results with the parent
2169 // scope (of the enclosing function).
2170 bool MergeWithParentScope = false;
2171 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2172 MergeWithParentScope = Rec->isLocalClass();
2173
2174 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002176 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002177 // instantiation scope, and set the parameter names to those used
2178 // in the template.
2179 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2180 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2181 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2182 FunctionParam->setDeclName(PatternParam->getDeclName());
2183 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2184 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002185
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002186 // Enter the scope of this instantiation. We don't use
2187 // PushDeclContext because we don't have a scope.
2188 DeclContext *PreviousContext = CurContext;
2189 CurContext = Function;
2190
Mike Stump1eb44332009-09-09 15:08:12 +00002191 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002192 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002193
2194 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002195 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002196 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2197 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2198 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002199 }
2200
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002201 // Instantiate the function body.
John McCall60d7b3a2010-08-24 06:29:42 +00002202 StmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002203
Douglas Gregor52604ab2009-09-11 21:19:12 +00002204 if (Body.isInvalid())
2205 Function->setInvalidDecl();
2206
John McCall9ae2f072010-08-23 23:25:46 +00002207 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002208 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002209
John McCall0c01d182010-03-24 05:22:00 +00002210 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2211
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002212 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002213
2214 DeclGroupRef DG(Function);
2215 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Douglas Gregor60406be2010-01-16 22:29:39 +00002217 // This class may have local implicit instantiations that need to be
2218 // instantiation within this scope.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002219 PerformPendingInstantiations(/*LocalOnly=*/true);
Douglas Gregor60406be2010-01-16 22:29:39 +00002220 Scope.Exit();
2221
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002222 if (Recursive) {
2223 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002224 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002225 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002226
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002227 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002228 PendingInstantiations.swap(SavedPendingInstantiations);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002229 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002230}
2231
2232/// \brief Instantiate the definition of the given variable from its
2233/// template.
2234///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002235/// \param PointOfInstantiation the point at which the instantiation was
2236/// required. Note that this is not precisely a "point of instantiation"
2237/// for the function, but it's close.
2238///
2239/// \param Var the already-instantiated declaration of a static member
2240/// variable of a class template specialization.
2241///
2242/// \param Recursive if true, recursively instantiates any functions that
2243/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002244///
2245/// \param DefinitionRequired if true, then we are performing an explicit
2246/// instantiation where an out-of-line definition of the member variable
2247/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002248void Sema::InstantiateStaticDataMemberDefinition(
2249 SourceLocation PointOfInstantiation,
2250 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002251 bool Recursive,
2252 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002253 if (Var->isInvalidDecl())
2254 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Douglas Gregor7caa6822009-07-24 20:34:43 +00002256 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002257 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002258 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002259 assert(Def->isStaticDataMember() && "Not a static data member?");
2260 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Douglas Gregor0d035142009-10-27 18:42:08 +00002262 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002263 // We did not find an out-of-line definition of this static data member,
2264 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002265 // instantiate this definition (or provide a specialization for it) in
2266 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002267 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002268 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002269 Diag(PointOfInstantiation,
2270 diag::err_explicit_instantiation_undefined_member)
2271 << 2 << Var->getDeclName() << Var->getDeclContext();
2272 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
Chandler Carruth58e390e2010-08-25 08:27:02 +00002273 } else if (Var->getTemplateSpecializationKind()
2274 == TSK_ExplicitInstantiationDefinition) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002275 PendingInstantiations.push_back(
Chandler Carruth58e390e2010-08-25 08:27:02 +00002276 std::make_pair(Var, PointOfInstantiation));
2277 }
2278
Douglas Gregor7caa6822009-07-24 20:34:43 +00002279 return;
2280 }
2281
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002282 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002283 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002284 return;
2285
2286 // C++0x [temp.explicit]p9:
2287 // Except for inline functions, other explicit instantiation declarations
2288 // have the effect of suppressing the implicit instantiation of the entity
2289 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002290 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002291 == TSK_ExplicitInstantiationDeclaration)
2292 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Douglas Gregor7caa6822009-07-24 20:34:43 +00002294 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2295 if (Inst)
2296 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Douglas Gregor7caa6822009-07-24 20:34:43 +00002298 // If we're performing recursive template instantiation, create our own
2299 // queue of pending implicit instantiations that we will instantiate later,
2300 // while we're still within our own instantiation context.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002301 std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
Douglas Gregor7caa6822009-07-24 20:34:43 +00002302 if (Recursive)
Chandler Carruth62c78d52010-08-25 08:44:16 +00002303 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002304
Douglas Gregor7caa6822009-07-24 20:34:43 +00002305 // Enter the scope of this instantiation. We don't use
2306 // PushDeclContext because we don't have a scope.
2307 DeclContext *PreviousContext = CurContext;
2308 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002310 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002311 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002312 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002313 CurContext = PreviousContext;
2314
2315 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002316 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2317 assert(MSInfo && "Missing member specialization information?");
2318 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2319 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002320 DeclGroupRef DG(Var);
2321 Consumer.HandleTopLevelDecl(DG);
2322 }
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Douglas Gregor7caa6822009-07-24 20:34:43 +00002324 if (Recursive) {
2325 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002326 // instantiation of this template.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002327 PerformPendingInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002328
Douglas Gregor7caa6822009-07-24 20:34:43 +00002329 // Restore the set of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002330 PendingInstantiations.swap(SavedPendingInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002331 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002332}
Douglas Gregor815215d2009-05-27 05:35:12 +00002333
Anders Carlsson09025312009-08-29 05:16:22 +00002334void
2335Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2336 const CXXConstructorDecl *Tmpl,
2337 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002338
Anders Carlsson09025312009-08-29 05:16:22 +00002339 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002340 bool AnyErrors = false;
2341
Anders Carlsson09025312009-08-29 05:16:22 +00002342 // Instantiate all the initializers.
2343 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002344 InitsEnd = Tmpl->init_end();
2345 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002346 CXXBaseOrMemberInitializer *Init = *Inits;
2347
Chandler Carruth030ef472010-09-03 21:54:20 +00002348 // Only instantiate written initializers, let Sema re-construct implicit
2349 // ones.
2350 if (!Init->isWritten())
2351 continue;
2352
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002353 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002354 ASTOwningVector<Expr*> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002356 // Instantiate the initializer.
2357 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
Douglas Gregora1a04782010-09-09 16:33:13 +00002358 LParenLoc, NewArgs, RParenLoc)) {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002359 AnyErrors = true;
2360 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002361 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002362
Anders Carlsson09025312009-08-29 05:16:22 +00002363 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002364 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002365 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002366 TemplateArgs,
2367 Init->getSourceLocation(),
2368 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002369 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002370 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002371 New->setInvalidDecl();
2372 continue;
2373 }
2374
John McCalla93c9342009-12-07 02:54:59 +00002375 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002376 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002377 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002378 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002379 Init->getRParenLoc(),
2380 New->getParent());
2381 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002382 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002384 // Is this an anonymous union?
2385 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002386 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2387 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002388 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002389 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2390 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002391 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002392
2393 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002394 NewArgs.size(),
2395 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002396 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002397 Init->getRParenLoc());
2398 }
2399
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002400 if (NewInit.isInvalid()) {
2401 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002402 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002403 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002404 // FIXME: It would be nice if ASTOwningVector had a release function.
2405 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Anders Carlsson09025312009-08-29 05:16:22 +00002407 NewInits.push_back((MemInitTy *)NewInit.get());
2408 }
2409 }
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Anders Carlsson09025312009-08-29 05:16:22 +00002411 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002412 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002413 /*FIXME: ColonLoc */
2414 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002415 NewInits.data(), NewInits.size(),
2416 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002417}
2418
John McCall52a575a2009-08-29 08:11:13 +00002419// TODO: this could be templated if the various decl types used the
2420// same method name.
2421static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2422 ClassTemplateDecl *Instance) {
2423 Pattern = Pattern->getCanonicalDecl();
2424
2425 do {
2426 Instance = Instance->getCanonicalDecl();
2427 if (Pattern == Instance) return true;
2428 Instance = Instance->getInstantiatedFromMemberTemplate();
2429 } while (Instance);
2430
2431 return false;
2432}
2433
Douglas Gregor0d696532009-09-28 06:34:35 +00002434static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2435 FunctionTemplateDecl *Instance) {
2436 Pattern = Pattern->getCanonicalDecl();
2437
2438 do {
2439 Instance = Instance->getCanonicalDecl();
2440 if (Pattern == Instance) return true;
2441 Instance = Instance->getInstantiatedFromMemberTemplate();
2442 } while (Instance);
2443
2444 return false;
2445}
2446
Douglas Gregored9c0f92009-10-29 00:04:11 +00002447static bool
2448isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2449 ClassTemplatePartialSpecializationDecl *Instance) {
2450 Pattern
2451 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2452 do {
2453 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2454 Instance->getCanonicalDecl());
2455 if (Pattern == Instance)
2456 return true;
2457 Instance = Instance->getInstantiatedFromMember();
2458 } while (Instance);
2459
2460 return false;
2461}
2462
John McCall52a575a2009-08-29 08:11:13 +00002463static bool isInstantiationOf(CXXRecordDecl *Pattern,
2464 CXXRecordDecl *Instance) {
2465 Pattern = Pattern->getCanonicalDecl();
2466
2467 do {
2468 Instance = Instance->getCanonicalDecl();
2469 if (Pattern == Instance) return true;
2470 Instance = Instance->getInstantiatedFromMemberClass();
2471 } while (Instance);
2472
2473 return false;
2474}
2475
2476static bool isInstantiationOf(FunctionDecl *Pattern,
2477 FunctionDecl *Instance) {
2478 Pattern = Pattern->getCanonicalDecl();
2479
2480 do {
2481 Instance = Instance->getCanonicalDecl();
2482 if (Pattern == Instance) return true;
2483 Instance = Instance->getInstantiatedFromMemberFunction();
2484 } while (Instance);
2485
2486 return false;
2487}
2488
2489static bool isInstantiationOf(EnumDecl *Pattern,
2490 EnumDecl *Instance) {
2491 Pattern = Pattern->getCanonicalDecl();
2492
2493 do {
2494 Instance = Instance->getCanonicalDecl();
2495 if (Pattern == Instance) return true;
2496 Instance = Instance->getInstantiatedFromMemberEnum();
2497 } while (Instance);
2498
2499 return false;
2500}
2501
John McCalled976492009-12-04 22:46:56 +00002502static bool isInstantiationOf(UsingShadowDecl *Pattern,
2503 UsingShadowDecl *Instance,
2504 ASTContext &C) {
2505 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2506}
2507
2508static bool isInstantiationOf(UsingDecl *Pattern,
2509 UsingDecl *Instance,
2510 ASTContext &C) {
2511 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2512}
2513
John McCall7ba107a2009-11-18 02:36:19 +00002514static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2515 UsingDecl *Instance,
2516 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002517 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002518}
2519
2520static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002521 UsingDecl *Instance,
2522 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002523 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002524}
2525
John McCall52a575a2009-08-29 08:11:13 +00002526static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2527 VarDecl *Instance) {
2528 assert(Instance->isStaticDataMember());
2529
2530 Pattern = Pattern->getCanonicalDecl();
2531
2532 do {
2533 Instance = Instance->getCanonicalDecl();
2534 if (Pattern == Instance) return true;
2535 Instance = Instance->getInstantiatedFromStaticDataMember();
2536 } while (Instance);
2537
2538 return false;
2539}
2540
John McCalled976492009-12-04 22:46:56 +00002541// Other is the prospective instantiation
2542// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002543static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002544 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002545 if (UnresolvedUsingTypenameDecl *UUD
2546 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2547 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2548 return isInstantiationOf(UUD, UD, Ctx);
2549 }
2550 }
2551
2552 if (UnresolvedUsingValueDecl *UUD
2553 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002554 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2555 return isInstantiationOf(UUD, UD, Ctx);
2556 }
2557 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002558
Anders Carlsson0d8df782009-08-29 19:37:28 +00002559 return false;
2560 }
Mike Stump1eb44332009-09-09 15:08:12 +00002561
John McCall52a575a2009-08-29 08:11:13 +00002562 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2563 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002564
John McCall52a575a2009-08-29 08:11:13 +00002565 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2566 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002567
John McCall52a575a2009-08-29 08:11:13 +00002568 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2569 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002570
Douglas Gregor7caa6822009-07-24 20:34:43 +00002571 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002572 if (Var->isStaticDataMember())
2573 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2574
2575 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2576 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002577
Douglas Gregor0d696532009-09-28 06:34:35 +00002578 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2579 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2580
Douglas Gregored9c0f92009-10-29 00:04:11 +00002581 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2582 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2583 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2584 PartialSpec);
2585
Anders Carlssond8b285f2009-09-01 04:26:58 +00002586 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2587 if (!Field->getDeclName()) {
2588 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002589 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002590 cast<FieldDecl>(D);
2591 }
2592 }
Mike Stump1eb44332009-09-09 15:08:12 +00002593
John McCalled976492009-12-04 22:46:56 +00002594 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2595 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2596
2597 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2598 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2599
Douglas Gregor815215d2009-05-27 05:35:12 +00002600 return D->getDeclName() && isa<NamedDecl>(Other) &&
2601 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2602}
2603
2604template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002605static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002606 NamedDecl *D,
2607 ForwardIterator first,
2608 ForwardIterator last) {
2609 for (; first != last; ++first)
2610 if (isInstantiationOf(Ctx, D, *first))
2611 return cast<NamedDecl>(*first);
2612
2613 return 0;
2614}
2615
John McCall02cace72009-08-28 07:59:38 +00002616/// \brief Finds the instantiation of the given declaration context
2617/// within the current instantiation.
2618///
2619/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002620DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002621 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002622 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002623 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002624 return cast_or_null<DeclContext>(ID);
2625 } else return DC;
2626}
2627
Douglas Gregored961e72009-05-27 17:54:46 +00002628/// \brief Find the instantiation of the given declaration within the
2629/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002630///
2631/// This routine is intended to be used when \p D is a declaration
2632/// referenced from within a template, that needs to mapped into the
2633/// corresponding declaration within an instantiation. For example,
2634/// given:
2635///
2636/// \code
2637/// template<typename T>
2638/// struct X {
2639/// enum Kind {
2640/// KnownValue = sizeof(T)
2641/// };
2642///
2643/// bool getKind() const { return KnownValue; }
2644/// };
2645///
2646/// template struct X<int>;
2647/// \endcode
2648///
2649/// In the instantiation of X<int>::getKind(), we need to map the
2650/// EnumConstantDecl for KnownValue (which refers to
2651/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002652/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2653/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002654NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002655 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002656 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002657 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002658 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002659 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002660 // D is a local of some kind. Look into the map of local
2661 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002662 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002663 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002664
Douglas Gregore95b4092009-09-16 18:34:49 +00002665 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2666 if (!Record->isDependentContext())
2667 return D;
2668
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002669 // If the RecordDecl is actually the injected-class-name or a
2670 // "templated" declaration for a class template, class template
2671 // partial specialization, or a member class of a class template,
2672 // substitute into the injected-class-name of the class template
2673 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002674 QualType T;
2675 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2676
2677 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002678 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002679 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2680 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002681 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002682
2683 // If we call SubstType with an InjectedClassNameType here we
2684 // can end up in an infinite loop.
2685 T = Context.getTypeDeclType(Record);
2686 assert(isa<InjectedClassNameType>(T) &&
2687 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002688 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002689 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002690
2691 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002692 // Substitute into the injected-class-name to get the type
2693 // corresponding to the instantiation we want, which may also be
2694 // the current instantiation (if we're in a template
2695 // definition). This substitution should never fail, since we
2696 // know we can instantiate the injected-class-name or we
2697 // wouldn't have gotten to the injected-class-name!
2698
2699 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2700 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002701 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2702 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2703
2704 if (!T->isDependentType()) {
2705 assert(T->isRecordType() && "Instantiation must produce a record type");
2706 return T->getAs<RecordType>()->getDecl();
2707 }
2708
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002709 // We are performing "partial" template instantiation to create
2710 // the member declarations for the members of a class template
2711 // specialization. Therefore, D is actually referring to something
2712 // in the current instantiation. Look through the current
2713 // context, which contains actual instantiations, to find the
2714 // instantiation of the "current instantiation" that D refers
2715 // to.
2716 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002717 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002718 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002719 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002720 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002721 if (isInstantiationOf(ClassTemplate,
2722 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002723 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002724
2725 if (!DC->isDependentContext())
2726 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002727 }
2728
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002729 // We're performing "instantiation" of a member of the current
2730 // instantiation while we are type-checking the
2731 // definition. Compute the declaration context and return that.
2732 assert(!SawNonDependentContext &&
2733 "No dependent context while instantiating record");
2734 DeclContext *DC = computeDeclContext(T);
2735 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002736 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002737 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002738 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002739
Douglas Gregore95b4092009-09-16 18:34:49 +00002740 // Fall through to deal with other dependent record types (e.g.,
2741 // anonymous unions in class templates).
2742 }
John McCall52a575a2009-08-29 08:11:13 +00002743
Douglas Gregore95b4092009-09-16 18:34:49 +00002744 if (!ParentDC->isDependentContext())
2745 return D;
2746
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002747 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002748 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002749 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002750
Douglas Gregor815215d2009-05-27 05:35:12 +00002751 if (ParentDC != D->getDeclContext()) {
2752 // We performed some kind of instantiation in the parent context,
2753 // so now we need to look into the instantiated parent context to
2754 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002755
John McCall3cb0ebd2010-03-10 03:28:59 +00002756 // If our context used to be dependent, we may need to instantiate
2757 // it before performing lookup into that context.
2758 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002759 if (!Spec->isDependentContext()) {
2760 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002761 const RecordType *Tag = T->getAs<RecordType>();
2762 assert(Tag && "type of non-dependent record is not a RecordType");
2763 if (!Tag->isBeingDefined() &&
2764 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2765 return 0;
Douglas Gregora43064c2010-11-05 23:22:45 +00002766
2767 ParentDC = Tag->getDecl();
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002768 }
2769 }
2770
Douglas Gregor815215d2009-05-27 05:35:12 +00002771 NamedDecl *Result = 0;
2772 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002773 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002774 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2775 } else {
2776 // Since we don't have a name for the entity we're looking for,
2777 // our only option is to walk through all of the declarations to
2778 // find that name. This will occur in a few cases:
2779 //
2780 // - anonymous struct/union within a template
2781 // - unnamed class/struct/union/enum within a template
2782 //
2783 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002784 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002785 ParentDC->decls_begin(),
2786 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002787 }
Mike Stump1eb44332009-09-09 15:08:12 +00002788
John McCall9f54ad42009-12-10 09:41:52 +00002789 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002790 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2791 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002792 && "Unable to find instantiation of declaration!");
2793
Douglas Gregor815215d2009-05-27 05:35:12 +00002794 D = Result;
2795 }
2796
Douglas Gregor815215d2009-05-27 05:35:12 +00002797 return D;
2798}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002799
Mike Stump1eb44332009-09-09 15:08:12 +00002800/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002801/// instantiations we have seen until this point.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002802void Sema::PerformPendingInstantiations(bool LocalOnly) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002803 while (!PendingLocalImplicitInstantiations.empty() ||
Chandler Carruth62c78d52010-08-25 08:44:16 +00002804 (!LocalOnly && !PendingInstantiations.empty())) {
Douglas Gregor60406be2010-01-16 22:29:39 +00002805 PendingImplicitInstantiation Inst;
2806
2807 if (PendingLocalImplicitInstantiations.empty()) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002808 Inst = PendingInstantiations.front();
2809 PendingInstantiations.pop_front();
Douglas Gregor60406be2010-01-16 22:29:39 +00002810 } else {
2811 Inst = PendingLocalImplicitInstantiations.front();
2812 PendingLocalImplicitInstantiations.pop_front();
2813 }
Mike Stump1eb44332009-09-09 15:08:12 +00002814
Douglas Gregor7caa6822009-07-24 20:34:43 +00002815 // Instantiate function definitions
2816 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCallf312b1e2010-08-26 23:41:50 +00002817 PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2818 "instantiating function definition");
Chandler Carruth58e390e2010-08-25 08:27:02 +00002819 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
2820 TSK_ExplicitInstantiationDefinition;
2821 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
2822 DefinitionRequired);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002823 continue;
2824 }
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Douglas Gregor7caa6822009-07-24 20:34:43 +00002826 // Instantiate static data member definitions.
2827 VarDecl *Var = cast<VarDecl>(Inst.first);
2828 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002829
Chandler Carruth291b4412010-02-13 10:17:50 +00002830 // Don't try to instantiate declarations if the most recent redeclaration
2831 // is invalid.
2832 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2833 continue;
2834
2835 // Check if the most recent declaration has changed the specialization kind
2836 // and removed the need for implicit instantiation.
2837 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2838 case TSK_Undeclared:
2839 assert(false && "Cannot instantitiate an undeclared specialization.");
2840 case TSK_ExplicitInstantiationDeclaration:
Chandler Carruth291b4412010-02-13 10:17:50 +00002841 case TSK_ExplicitSpecialization:
Chandler Carruth58e390e2010-08-25 08:27:02 +00002842 continue; // No longer need to instantiate this type.
2843 case TSK_ExplicitInstantiationDefinition:
2844 // We only need an instantiation if the pending instantiation *is* the
2845 // explicit instantiation.
2846 if (Var != Var->getMostRecentDeclaration()) continue;
Chandler Carruth291b4412010-02-13 10:17:50 +00002847 case TSK_ImplicitInstantiation:
2848 break;
2849 }
2850
John McCallf312b1e2010-08-26 23:41:50 +00002851 PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2852 "instantiating static data member "
2853 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002854
Chandler Carruth58e390e2010-08-25 08:27:02 +00002855 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
2856 TSK_ExplicitInstantiationDefinition;
2857 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
2858 DefinitionRequired);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002859 }
2860}
John McCall0c01d182010-03-24 05:22:00 +00002861
2862void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2863 const MultiLevelTemplateArgumentList &TemplateArgs) {
2864 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2865 E = Pattern->ddiag_end(); I != E; ++I) {
2866 DependentDiagnostic *DD = *I;
2867
2868 switch (DD->getKind()) {
2869 case DependentDiagnostic::Access:
2870 HandleDependentAccessCheck(*DD, TemplateArgs);
2871 break;
2872 }
2873 }
2874}