blob: df4ebfaa93352a73564b738b42964d8e81ca8c9a [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//===----------------------------------------------------------------------===/
12#include "Sema.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000013#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/Expr.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000018#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000019#include "llvm/Support/Compiler.h"
20
21using namespace clang;
22
23namespace {
24 class VISIBILITY_HIDDEN TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000025 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000026 Sema &SemaRef;
27 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000028 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor8dbc2692009-03-17 21:15:40 +000029
30 public:
31 typedef Sema::OwningExprResult OwningExprResult;
32
33 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000034 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000035 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Douglas Gregor8dbc2692009-03-17 21:15:40 +000036
Mike Stump390b4cc2009-05-16 07:39:55 +000037 // FIXME: Once we get closer to completion, replace these manually-written
38 // declarations with automatically-generated ones from
39 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000040 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
41 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000042 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000043 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000044 Decl *VisitFieldDecl(FieldDecl *D);
45 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
46 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000047 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000048 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregore53060f2009-06-25 22:08:12 +000049 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregord475b8d2009-03-25 21:17:03 +000050 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000051 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
52 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000053 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000054 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000055 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000056 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000057 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000058 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000059 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000060 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Anders Carlsson0dde18e2009-08-28 15:18:15 +000061 Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
62
Douglas Gregor8dbc2692009-03-17 21:15:40 +000063 // Base case. FIXME: Remove once we can instantiate everything.
64 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000065 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000066 return 0;
67 }
Douglas Gregor5545e162009-03-24 00:38:23 +000068
John McCallfd810b12009-08-14 02:03:10 +000069 const LangOptions &getLangOptions() {
70 return SemaRef.getLangOptions();
71 }
72
Douglas Gregor5545e162009-03-24 00:38:23 +000073 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000074 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000075 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000076 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000077 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000078
79 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000080 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000081 };
82}
83
Douglas Gregor4f722be2009-03-25 15:45:12 +000084Decl *
85TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
86 assert(false && "Translation units cannot be instantiated");
87 return D;
88}
89
90Decl *
91TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
92 assert(false && "Namespaces cannot be instantiated");
93 return D;
94}
95
Douglas Gregor8dbc2692009-03-17 21:15:40 +000096Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
97 bool Invalid = false;
98 QualType T = D->getUnderlyingType();
99 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000100 T = SemaRef.SubstType(T, TemplateArgs,
101 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000102 if (T.isNull()) {
103 Invalid = true;
104 T = SemaRef.Context.IntTy;
105 }
106 }
107
108 // Create the new typedef
109 TypedefDecl *Typedef
110 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
111 D->getIdentifier(), T);
112 if (Invalid)
113 Typedef->setInvalidDecl();
114
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000115 Owner->addDecl(Typedef);
Douglas Gregorbc221632009-05-28 16:34:51 +0000116
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000117 return Typedef;
118}
119
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000120Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000121 // Do substitution on the type of the declaration
122 QualType T = SemaRef.SubstType(D->getType(), TemplateArgs,
123 D->getTypeSpecStartLoc(),
124 D->getDeclName());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000125 if (T.isNull())
126 return 0;
127
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000128 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000129 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
130 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000131 T, D->getDeclaratorInfo(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000132 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000133 Var->setThreadSpecified(D->isThreadSpecified());
134 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
135 Var->setDeclaredInCondition(D->isDeclaredInCondition());
136
Douglas Gregor7caa6822009-07-24 20:34:43 +0000137 // If we are instantiating a static data member defined
138 // out-of-line, the instantiation will have the same lexical
139 // context (which will be a namespace scope) as the template.
140 if (D->isOutOfLine())
141 Var->setLexicalDeclContext(D->getLexicalDeclContext());
142
Mike Stump390b4cc2009-05-16 07:39:55 +0000143 // FIXME: In theory, we could have a previous declaration for variables that
144 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000145 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000146 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000147
148 if (D->isOutOfLine()) {
149 D->getLexicalDeclContext()->addDecl(Var);
150 Owner->makeDeclVisibleInContext(Var);
151 } else {
152 Owner->addDecl(Var);
153 }
154
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000155 if (D->getInit()) {
156 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000157 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000158 if (Init.isInvalid())
159 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000160 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
161 // FIXME: We're faking all of the comma locations, which is suboptimal.
162 // Do we even need these comma locations?
163 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
164 if (PLE->getNumExprs() > 0) {
165 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
166 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
167 Expr *E = PLE->getExpr(I)->Retain();
168 FakeCommaLocs.push_back(
169 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
170 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000171 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000172 }
173
174 // Add the direct initializer to the declaration.
175 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
176 PLE->getLParenLoc(),
177 Sema::MultiExprArg(SemaRef,
178 (void**)PLE->getExprs(),
179 PLE->getNumExprs()),
180 FakeCommaLocs.data(),
181 PLE->getRParenLoc());
182
183 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
184 // we've explicitly retained all of its subexpressions already.
185 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000186 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000187 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000188 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
189 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000190
Douglas Gregor7caa6822009-07-24 20:34:43 +0000191 // Link instantiations of static data members back to the template from
192 // which they were instantiated.
193 if (Var->isStaticDataMember())
194 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D);
195
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000196 return Var;
197}
198
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000199Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
200 bool Invalid = false;
201 QualType T = D->getType();
202 if (T->isDependentType()) {
John McCallce3ff2b2009-08-25 22:02:44 +0000203 T = SemaRef.SubstType(T, TemplateArgs,
204 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000205 if (!T.isNull() && T->isFunctionType()) {
206 // C++ [temp.arg.type]p3:
207 // If a declaration acquires a function type through a type
208 // dependent on a template-parameter and this causes a
209 // declaration that does not use the syntactic form of a
210 // function declarator to have function type, the program is
211 // ill-formed.
212 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
213 << T;
214 T = QualType();
215 Invalid = true;
216 }
217 }
218
219 Expr *BitWidth = D->getBitWidth();
220 if (Invalid)
221 BitWidth = 0;
222 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000223 // The bit-width expression is not potentially evaluated.
224 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
225
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000226 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000227 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000228 if (InstantiatedBitWidth.isInvalid()) {
229 Invalid = true;
230 BitWidth = 0;
231 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000232 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000233 }
234
235 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000236 D->getDeclaratorInfo(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000237 cast<RecordDecl>(Owner),
238 D->getLocation(),
239 D->isMutable(),
240 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000241 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000242 D->getAccess(),
243 0);
244 if (Field) {
245 if (Invalid)
246 Field->setInvalidDecl();
247
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000248 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000249 }
250
251 return Field;
252}
253
John McCall02cace72009-08-28 07:59:38 +0000254Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
255 FriendDecl::FriendUnion FU;
256
257 // Handle friend type expressions by simply substituting template
258 // parameters into the pattern type.
259 if (Type *Ty = D->getFriendType()) {
260 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
261 D->getLocation(), DeclarationName());
262 if (T.isNull()) return 0;
263
264 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
265 FU = T.getTypePtr();
266
267 // Handle everything else by appropriate substitution.
268 } else {
269 NamedDecl *ND = D->getFriendDecl();
270 assert(ND && "friend decl must be a decl or a type!");
271
272 Decl *NewND = Visit(ND);
273 if (!NewND) return 0;
274
275 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000276 }
John McCall02cace72009-08-28 07:59:38 +0000277
278 FriendDecl *FD =
279 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
280 D->getFriendLoc());
281 Owner->addDecl(FD);
282 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000283}
284
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000285Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
286 Expr *AssertExpr = D->getAssertExpr();
287
Douglas Gregorac7610d2009-06-22 20:57:11 +0000288 // The expression in a static assertion is not potentially evaluated.
289 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
290
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000291 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000292 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000293 if (InstantiatedAssertExpr.isInvalid())
294 return 0;
295
Douglas Gregor43d9d922009-08-08 01:41:12 +0000296 OwningExprResult Message(SemaRef, D->getMessage());
297 D->getMessage()->Retain();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000298 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000299 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
300 move(InstantiatedAssertExpr),
301 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000302 return StaticAssert;
303}
304
305Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
306 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
307 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000308 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000309 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000310 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000311 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000312 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000313 Enum->startDefinition();
314
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000315 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000316
317 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000318 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
319 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000320 EC != ECEnd; ++EC) {
321 // The specified value for the enumerator.
322 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000323 if (Expr *UninstValue = EC->getInitExpr()) {
324 // The enumerator's value expression is not potentially evaluated.
325 EnterExpressionEvaluationContext Unevaluated(SemaRef,
326 Action::Unevaluated);
327
John McCallce3ff2b2009-08-25 22:02:44 +0000328 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000329 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000330
331 // Drop the initial value and continue.
332 bool isInvalid = false;
333 if (Value.isInvalid()) {
334 Value = SemaRef.Owned((Expr *)0);
335 isInvalid = true;
336 }
337
338 EnumConstantDecl *EnumConst
339 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
340 EC->getLocation(), EC->getIdentifier(),
341 move(Value));
342
343 if (isInvalid) {
344 if (EnumConst)
345 EnumConst->setInvalidDecl();
346 Enum->setInvalidDecl();
347 }
348
349 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000350 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000351 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000352 LastEnumConst = EnumConst;
353 }
354 }
355
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000356 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000357 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000358 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
359 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000360 &Enumerators[0], Enumerators.size(),
361 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000362
363 return Enum;
364}
365
Douglas Gregor6477b692009-03-25 15:04:13 +0000366Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
367 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
368 return 0;
369}
370
John McCalle29ba202009-08-20 01:44:21 +0000371Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
372 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000373 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Douglas Gregord60e1052009-08-27 16:57:43 +0000374 if (!InstParams)
375 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000376
377 CXXRecordDecl *Pattern = D->getTemplatedDecl();
378 CXXRecordDecl *RecordInst
379 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
380 Pattern->getLocation(), Pattern->getIdentifier(),
381 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
382
383 ClassTemplateDecl *Inst
384 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
385 D->getIdentifier(), InstParams, RecordInst, 0);
386 RecordInst->setDescribedClassTemplate(Inst);
387 Inst->setAccess(D->getAccess());
388 Inst->setInstantiatedFromMemberTemplate(D);
389
390 Owner->addDecl(Inst);
391 return Inst;
392}
393
Douglas Gregord60e1052009-08-27 16:57:43 +0000394Decl *
395TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
396 TemplateParameterList *TempParams = D->getTemplateParameters();
397 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
398 if (!InstParams)
399 return NULL;
400
401 // FIXME: Handle instantiation of nested function templates that aren't
402 // member function templates. This could happen inside a FriendDecl.
403 assert(isa<CXXMethodDecl>(D->getTemplatedDecl()));
404 CXXMethodDecl *InstMethod
405 = cast_or_null<CXXMethodDecl>(
406 VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()),
407 InstParams));
408 if (!InstMethod)
409 return 0;
410
411 // Link the instantiated function template declaration to the function
412 // template from which it was instantiated.
413 FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate();
414 assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!");
415 InstTemplate->setInstantiatedFromMemberTemplate(D);
416 Owner->addDecl(InstTemplate);
417 return InstTemplate;
418}
419
Douglas Gregord475b8d2009-03-25 21:17:03 +0000420Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
421 CXXRecordDecl *PrevDecl = 0;
422 if (D->isInjectedClassName())
423 PrevDecl = cast<CXXRecordDecl>(Owner);
424
425 CXXRecordDecl *Record
426 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000427 D->getLocation(), D->getIdentifier(),
428 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000429 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000430 // FIXME: Check against AS_none is an ugly hack to work around the issue that
431 // the tag decls introduced by friend class declarations don't have an access
432 // specifier. Remove once this area of the code gets sorted out.
433 if (D->getAccess() != AS_none)
434 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000435 if (!D->isInjectedClassName())
436 Record->setInstantiationOfMemberClass(D);
437
John McCall02cace72009-08-28 07:59:38 +0000438 // If the original function was part of a friend declaration,
439 // inherit its namespace state.
440 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
441 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
442
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000443 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000444 return Record;
445}
446
John McCall02cace72009-08-28 07:59:38 +0000447/// Normal class members are of more specific types and therefore
448/// don't make it here. This function serves two purposes:
449/// 1) instantiating function templates
450/// 2) substituting friend declarations
451/// FIXME: preserve function definitions in case #2
Douglas Gregore53060f2009-06-25 22:08:12 +0000452Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000453 // Check whether there is already a function template specialization for
454 // this declaration.
455 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
456 void *InsertPos = 0;
457 if (FunctionTemplate) {
458 llvm::FoldingSetNodeID ID;
459 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000460 TemplateArgs.getInnermost().getFlatArgumentList(),
461 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000462 SemaRef.Context);
Douglas Gregor127102b2009-06-29 20:59:39 +0000463
464 FunctionTemplateSpecializationInfo *Info
465 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
466 InsertPos);
467
468 // If we already have a function template specialization, return it.
469 if (Info)
470 return Info->Function;
471 }
Douglas Gregore53060f2009-06-25 22:08:12 +0000472
473 Sema::LocalInstantiationScope Scope(SemaRef);
474
475 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000476 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000477 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000478 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000479
Douglas Gregore53060f2009-06-25 22:08:12 +0000480 // Build the instantiated method declaration.
John McCall02cace72009-08-28 07:59:38 +0000481 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext());
482 FunctionDecl *Function =
483 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000484 D->getDeclName(), T, D->getDeclaratorInfo(),
485 D->getStorageClass(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000486 D->isInline(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000487 Function->setLexicalDeclContext(Owner);
488
Douglas Gregore53060f2009-06-25 22:08:12 +0000489 // Attach the parameters
490 for (unsigned P = 0; P < Params.size(); ++P)
491 Params[P]->setOwningFunction(Function);
492 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000493
494 // If the original function was part of a friend declaration,
495 // inherit its namespace state and add it to the owner.
496 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) {
497 bool WasDeclared = (FOK == Decl::FOK_Declared);
498 Function->setObjectOfFriendDecl(WasDeclared);
499 if (!Owner->isDependentContext())
500 DC->makeDeclVisibleInContext(Function);
John McCallf181d8a2009-08-29 03:16:09 +0000501
502 Function->setInstantiationOfMemberFunction(D);
John McCall02cace72009-08-28 07:59:38 +0000503 }
Douglas Gregore53060f2009-06-25 22:08:12 +0000504
505 if (InitFunctionInstantiation(Function, D))
506 Function->setInvalidDecl();
507
508 bool Redeclaration = false;
509 bool OverloadableAttrRequired = false;
510 NamedDecl *PrevDecl = 0;
511 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
512 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000513
Douglas Gregor127102b2009-06-29 20:59:39 +0000514 if (FunctionTemplate) {
515 // Record this function template specialization.
516 Function->setFunctionTemplateSpecialization(SemaRef.Context,
517 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000518 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000519 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000520 }
521
Douglas Gregore53060f2009-06-25 22:08:12 +0000522 return Function;
523}
524
Douglas Gregord60e1052009-08-27 16:57:43 +0000525Decl *
526TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
527 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000528 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
529 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000530 if (FunctionTemplate && !TemplateParams) {
531 // We are creating a function template specialization from a function
532 // template. Check whether there is already a function template
533 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000534 llvm::FoldingSetNodeID ID;
535 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000536 TemplateArgs.getInnermost().getFlatArgumentList(),
537 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000538 SemaRef.Context);
539
540 FunctionTemplateSpecializationInfo *Info
541 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
542 InsertPos);
543
544 // If we already have a function template specialization, return it.
545 if (Info)
546 return Info->Function;
547 }
548
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000549 Sema::LocalInstantiationScope Scope(SemaRef);
550
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000551 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000552 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000553 if (T.isNull())
554 return 0;
555
556 // Build the instantiated method declaration.
557 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000558 CXXMethodDecl *Method = 0;
559
560 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000561 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000562 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
563 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
564 SemaRef.Context.getCanonicalType(ClassTy));
565 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000566 Constructor->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000567 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000568 Constructor->getDeclaratorInfo(),
569 Constructor->isExplicit(),
570 Constructor->isInline(), false);
571 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
572 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
573 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
574 SemaRef.Context.getCanonicalType(ClassTy));
575 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
576 Destructor->getLocation(), Name,
577 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000578 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
579 CanQualType ConvTy
580 = SemaRef.Context.getCanonicalType(
581 T->getAsFunctionType()->getResultType());
582 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
583 ConvTy);
584 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
585 Conversion->getLocation(), Name,
586 T, Conversion->getDeclaratorInfo(),
587 Conversion->isInline(),
588 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000589 } else {
590 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
591 D->getDeclName(), T, D->getDeclaratorInfo(),
592 D->isStatic(), D->isInline());
593 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000594
Douglas Gregord60e1052009-08-27 16:57:43 +0000595 if (TemplateParams) {
596 // Our resulting instantiation is actually a function template, since we
597 // are substituting only the outer template parameters. For example, given
598 //
599 // template<typename T>
600 // struct X {
601 // template<typename U> void f(T, U);
602 // };
603 //
604 // X<int> x;
605 //
606 // We are instantiating the member template "f" within X<int>, which means
607 // substituting int for T, but leaving "f" as a member function template.
608 // Build the function template itself.
609 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
610 Method->getLocation(),
611 Method->getDeclName(),
612 TemplateParams, Method);
613 if (D->isOutOfLine())
614 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
615 Method->setDescribedFunctionTemplate(FunctionTemplate);
616 } else if (!FunctionTemplate)
Douglas Gregor6b906862009-08-21 00:16:32 +0000617 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000618
Douglas Gregor7caa6822009-07-24 20:34:43 +0000619 // If we are instantiating a member function defined
620 // out-of-line, the instantiation will have the same lexical
621 // context (which will be a namespace scope) as the template.
622 if (D->isOutOfLine())
623 Method->setLexicalDeclContext(D->getLexicalDeclContext());
624
Douglas Gregor5545e162009-03-24 00:38:23 +0000625 // Attach the parameters
626 for (unsigned P = 0; P < Params.size(); ++P)
627 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000628 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000629
630 if (InitMethodInstantiation(Method, D))
631 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000632
Douglas Gregordec06662009-08-21 18:42:58 +0000633 NamedDecl *PrevDecl = 0;
634
Douglas Gregord60e1052009-08-27 16:57:43 +0000635 if (!FunctionTemplate || TemplateParams) {
Douglas Gregordec06662009-08-21 18:42:58 +0000636 PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
637 Sema::LookupOrdinaryName, true);
638
639 // In C++, the previous declaration we find might be a tag type
640 // (class or enum). In this case, the new declaration will hide the
641 // tag type. Note that this does does not apply if we're declaring a
642 // typedef (C++ [dcl.typedef]p4).
643 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
644 PrevDecl = 0;
645 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000646
Douglas Gregord60e1052009-08-27 16:57:43 +0000647 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000648 // Record this function template specialization.
649 Method->setFunctionTemplateSpecialization(SemaRef.Context,
650 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000651 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000652 InsertPos);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000653
654 bool Redeclaration = false;
655 bool OverloadableAttrRequired = false;
656 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
657 /*FIXME:*/OverloadableAttrRequired);
658
659 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
Douglas Gregordec06662009-08-21 18:42:58 +0000660 Owner->addDecl(Method);
661
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000662 return Method;
663}
664
Douglas Gregor615c5d42009-03-24 16:43:20 +0000665Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000666 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000667}
668
Douglas Gregor03b2b072009-03-24 00:15:49 +0000669Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000670 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000671}
672
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000673Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000674 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000675}
676
Douglas Gregor6477b692009-03-25 15:04:13 +0000677ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000678 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000679 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000680 if (OrigT.isNull())
681 return 0;
682
683 QualType T = SemaRef.adjustParameterType(OrigT);
684
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000685 // Allocate the parameter
686 ParmVarDecl *Param = 0;
687 if (T == OrigT)
688 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000689 D->getIdentifier(), T, D->getDeclaratorInfo(),
690 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000691 else
692 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
693 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000694 T, D->getDeclaratorInfo(), OrigT,
695 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000696
Anders Carlsson9351c172009-08-25 03:18:48 +0000697 // Mark the default argument as being uninstantiated.
698 if (Expr *Arg = D->getDefaultArg())
699 Param->setUninstantiatedDefaultArg(Arg);
700
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000701 // Note: we don't try to instantiate function parameters until after
702 // we've instantiated the function's type. Therefore, we don't have
703 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000704 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000705 return Param;
706}
707
708Decl *
709TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
710 // Since parameter types can decay either before or after
711 // instantiation, we simply treat OriginalParmVarDecls as
712 // ParmVarDecls the same way, and create one or the other depending
713 // on what happens after template instantiation.
714 return VisitParmVarDecl(D);
715}
716
John McCalle29ba202009-08-20 01:44:21 +0000717Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
718 TemplateTypeParmDecl *D) {
719 // TODO: don't always clone when decls are refcounted.
720 const Type* T = D->getTypeForDecl();
721 assert(T->isTemplateTypeParmType());
722 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
723
724 TemplateTypeParmDecl *Inst =
725 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
726 TTPT->getDepth(), TTPT->getIndex(),
727 TTPT->getName(),
728 D->wasDeclaredWithTypename(),
729 D->isParameterPack());
730
731 if (D->hasDefaultArgument()) {
732 QualType DefaultPattern = D->getDefaultArgument();
733 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000734 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
735 D->getDefaultArgumentLoc(),
736 D->getDeclName());
John McCalle29ba202009-08-20 01:44:21 +0000737
738 Inst->setDefaultArgument(DefaultInst,
739 D->getDefaultArgumentLoc(),
740 D->defaultArgumentWasInherited() /* preserve? */);
741 }
742
743 return Inst;
744}
745
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000746Decl *
747TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
748 NestedNameSpecifier *NNS =
749 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
750 D->getTargetNestedNameRange(),
751 TemplateArgs);
752 if (!NNS)
753 return 0;
754
755 CXXScopeSpec SS;
756 SS.setRange(D->getTargetNestedNameRange());
757 SS.setScopeRep(NNS);
758
759 return SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
760 D->getTargetNameLocation(),
761 D->getTargetName(), 0, D->isTypeName());
762}
763
John McCallce3ff2b2009-08-25 22:02:44 +0000764Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000765 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000766 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000767 return Instantiator.Visit(D);
768}
769
John McCalle29ba202009-08-20 01:44:21 +0000770/// \brief Instantiates a nested template parameter list in the current
771/// instantiation context.
772///
773/// \param L The parameter list to instantiate
774///
775/// \returns NULL if there was an error
776TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000777TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000778 // Get errors for all the parameters before bailing out.
779 bool Invalid = false;
780
781 unsigned N = L->size();
782 typedef llvm::SmallVector<Decl*,8> ParamVector;
783 ParamVector Params;
784 Params.reserve(N);
785 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
786 PI != PE; ++PI) {
787 Decl *D = Visit(*PI);
788 Params.push_back(D);
789 Invalid = Invalid || !D;
790 }
791
792 // Clean up if we had an error.
793 if (Invalid) {
794 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
795 PI != PE; ++PI)
796 if (*PI)
797 (*PI)->Destroy(SemaRef.Context);
798 return NULL;
799 }
800
801 TemplateParameterList *InstL
802 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
803 L->getLAngleLoc(), &Params.front(), N,
804 L->getRAngleLoc());
805 return InstL;
806}
807
John McCallce3ff2b2009-08-25 22:02:44 +0000808/// \brief Does substitution on the type of the given function, including
809/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000810///
John McCallce3ff2b2009-08-25 22:02:44 +0000811/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000812///
813/// \param Params the instantiated parameter declarations
814
John McCallce3ff2b2009-08-25 22:02:44 +0000815/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000816/// type if there was an error.
817QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000818TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000819 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
820 bool InvalidDecl = false;
821
John McCallce3ff2b2009-08-25 22:02:44 +0000822 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000823 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000824 llvm::SmallVector<QualType, 4> ParamTys;
Douglas Gregor5545e162009-03-24 00:38:23 +0000825 for (FunctionDecl::param_iterator P = D->param_begin(),
826 PEnd = D->param_end();
827 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000828 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000829 if (PInst->getType()->isVoidType()) {
830 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
831 PInst->setInvalidDecl();
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000832 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
833 PInst->getType(),
834 diag::err_abstract_type_in_decl,
835 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000836 PInst->setInvalidDecl();
837
838 Params.push_back(PInst);
839 ParamTys.push_back(PInst->getType());
840
841 if (PInst->isInvalidDecl())
842 InvalidDecl = true;
843 } else
844 InvalidDecl = true;
845 }
846
847 // FIXME: Deallocate dead declarations.
848 if (InvalidDecl)
849 return QualType();
850
851 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
852 assert(Proto && "Missing prototype?");
853 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000854 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
855 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000856 if (ResultType.isNull())
857 return QualType();
858
Jay Foadbeaaccd2009-05-21 09:52:38 +0000859 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000860 Proto->isVariadic(), Proto->getTypeQuals(),
861 D->getLocation(), D->getDeclName());
862}
863
Douglas Gregore53060f2009-06-25 22:08:12 +0000864/// \brief Initializes the common fields of an instantiation function
865/// declaration (New) from the corresponding fields of its template (Tmpl).
866///
867/// \returns true if there was an error
868bool
869TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
870 FunctionDecl *Tmpl) {
871 if (Tmpl->isDeleted())
872 New->setDeleted();
Douglas Gregorcca9e962009-07-01 22:01:06 +0000873
874 // If we are performing substituting explicitly-specified template arguments
875 // or deduced template arguments into a function template and we reach this
876 // point, we are now past the point where SFINAE applies and have committed
877 // to keeping the new function template specialization. We therefore
878 // convert the active template instantiation for the function template
879 // into a template instantiation for this specific function template
880 // specialization, which is not a SFINAE context, so that we diagnose any
881 // further errors in the declaration itself.
882 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
883 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
884 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
885 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
886 if (FunctionTemplateDecl *FunTmpl
887 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
888 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
889 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000890 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000891 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
892 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
893 }
894 }
895
Douglas Gregore53060f2009-06-25 22:08:12 +0000896 return false;
897}
898
Douglas Gregor5545e162009-03-24 00:38:23 +0000899/// \brief Initializes common fields of an instantiated method
900/// declaration (New) from the corresponding fields of its template
901/// (Tmpl).
902///
903/// \returns true if there was an error
904bool
905TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
906 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000907 if (InitFunctionInstantiation(New, Tmpl))
908 return true;
909
Douglas Gregor5545e162009-03-24 00:38:23 +0000910 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
911 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000912 if (Tmpl->isVirtualAsWritten()) {
913 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000914 Record->setAggregate(false);
915 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +0000916 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +0000917 Record->setPolymorphic(true);
918 }
Douglas Gregor5545e162009-03-24 00:38:23 +0000919 if (Tmpl->isPure()) {
920 New->setPure();
921 Record->setAbstract(true);
922 }
923
924 // FIXME: attributes
925 // FIXME: New needs a pointer to Tmpl
926 return false;
927}
Douglas Gregora58861f2009-05-13 20:28:22 +0000928
929/// \brief Instantiate the definition of the given function from its
930/// template.
931///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000932/// \param PointOfInstantiation the point at which the instantiation was
933/// required. Note that this is not precisely a "point of instantiation"
934/// for the function, but it's close.
935///
Douglas Gregora58861f2009-05-13 20:28:22 +0000936/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000937/// function template specialization or member function of a class template
938/// specialization.
939///
940/// \param Recursive if true, recursively instantiates any functions that
941/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000942void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000943 FunctionDecl *Function,
944 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000945 if (Function->isInvalidDecl())
946 return;
947
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000948 assert(!Function->getBody() && "Already instantiated!");
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000949
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000950 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +0000951 const FunctionDecl *PatternDecl = 0;
Douglas Gregor5ec178f2009-08-28 21:09:48 +0000952 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
953 while (Primary->getInstantiatedFromMemberTemplate())
954 Primary = Primary->getInstantiatedFromMemberTemplate();
955
Douglas Gregor1637be72009-06-26 00:10:03 +0000956 PatternDecl = Primary->getTemplatedDecl();
Douglas Gregor5ec178f2009-08-28 21:09:48 +0000957 } else
Douglas Gregor1637be72009-06-26 00:10:03 +0000958 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000959 Stmt *Pattern = 0;
960 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000961 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000962
963 if (!Pattern)
964 return;
965
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000966 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
967 if (Inst)
968 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000969
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000970 // If we're performing recursive template instantiation, create our own
971 // queue of pending implicit instantiations that we will instantiate later,
972 // while we're still within our own instantiation context.
973 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
974 if (Recursive)
975 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
976
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000977 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
978
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000979 // Introduce a new scope where local variable instantiations will be
980 // recorded.
981 LocalInstantiationScope Scope(*this);
982
983 // Introduce the instantiated function parameters into the local
984 // instantiation scope.
985 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
986 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
987 Function->getParamDecl(I));
988
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000989 // Enter the scope of this instantiation. We don't use
990 // PushDeclContext because we don't have a scope.
991 DeclContext *PreviousContext = CurContext;
992 CurContext = Function;
993
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000994 // Instantiate the function body.
995 OwningStmtResult Body
John McCallce3ff2b2009-08-25 22:02:44 +0000996 = SubstStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000997
998 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
999 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001000
1001 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001002
1003 DeclGroupRef DG(Function);
1004 Consumer.HandleTopLevelDecl(DG);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001005
1006 if (Recursive) {
1007 // Instantiate any pending implicit instantiations found during the
1008 // instantiation of this template.
1009 PerformPendingImplicitInstantiations();
1010
1011 // Restore the set of pending implicit instantiations.
1012 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1013 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001014}
1015
1016/// \brief Instantiate the definition of the given variable from its
1017/// template.
1018///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001019/// \param PointOfInstantiation the point at which the instantiation was
1020/// required. Note that this is not precisely a "point of instantiation"
1021/// for the function, but it's close.
1022///
1023/// \param Var the already-instantiated declaration of a static member
1024/// variable of a class template specialization.
1025///
1026/// \param Recursive if true, recursively instantiates any functions that
1027/// are required by this instantiation.
1028void Sema::InstantiateStaticDataMemberDefinition(
1029 SourceLocation PointOfInstantiation,
1030 VarDecl *Var,
1031 bool Recursive) {
1032 if (Var->isInvalidDecl())
1033 return;
1034
1035 // Find the out-of-line definition of this static data member.
1036 // FIXME: Do we have to look for specializations separately?
1037 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1038 bool FoundOutOfLineDef = false;
1039 assert(Def && "This data member was not instantiated from a template?");
1040 assert(Def->isStaticDataMember() && "Not a static data member?");
1041 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
1042 RDEnd = Def->redecls_end();
1043 RD != RDEnd; ++RD) {
1044 if (RD->getLexicalDeclContext()->isFileContext()) {
1045 Def = *RD;
1046 FoundOutOfLineDef = true;
1047 }
1048 }
1049
1050 if (!FoundOutOfLineDef) {
1051 // We did not find an out-of-line definition of this static data member,
1052 // so we won't perform any instantiation. Rather, we rely on the user to
1053 // instantiate this definition (or provide a specialization for it) in
1054 // another translation unit.
1055 return;
1056 }
1057
1058 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1059 if (Inst)
1060 return;
1061
1062 // If we're performing recursive template instantiation, create our own
1063 // queue of pending implicit instantiations that we will instantiate later,
1064 // while we're still within our own instantiation context.
1065 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1066 if (Recursive)
1067 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1068
1069 // Enter the scope of this instantiation. We don't use
1070 // PushDeclContext because we don't have a scope.
1071 DeclContext *PreviousContext = CurContext;
1072 CurContext = Var->getDeclContext();
1073
John McCallce3ff2b2009-08-25 22:02:44 +00001074 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001075 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001076
1077 CurContext = PreviousContext;
1078
1079 if (Var) {
1080 DeclGroupRef DG(Var);
1081 Consumer.HandleTopLevelDecl(DG);
1082 }
1083
1084 if (Recursive) {
1085 // Instantiate any pending implicit instantiations found during the
1086 // instantiation of this template.
1087 PerformPendingImplicitInstantiations();
1088
1089 // Restore the set of pending implicit instantiations.
1090 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1091 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001092}
Douglas Gregor815215d2009-05-27 05:35:12 +00001093
1094static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
1095 if (D->getKind() != Other->getKind())
1096 return false;
1097
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001098 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) {
1099 if (CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass())
1100 return Pattern->getCanonicalDecl() == D->getCanonicalDecl();
1101 else
1102 return false;
1103 }
1104
1105 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) {
1106 if (FunctionDecl *Pattern = Function->getInstantiatedFromMemberFunction())
1107 return Pattern->getCanonicalDecl() == D->getCanonicalDecl();
1108 else
1109 return false;
1110 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001111
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001112 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) {
1113 if (EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum())
1114 return Pattern->getCanonicalDecl() == D->getCanonicalDecl();
1115 else
1116 return false;
1117 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001118
Douglas Gregor7caa6822009-07-24 20:34:43 +00001119 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001120 if (Var->isStaticDataMember()) {
1121 if (VarDecl *Pattern = Var->getInstantiatedFromStaticDataMember())
1122 return Pattern->getCanonicalDecl() == D->getCanonicalDecl();
1123 else
1124 return false;
1125 }
1126
Douglas Gregor815215d2009-05-27 05:35:12 +00001127 // FIXME: How can we find instantiations of anonymous unions?
1128
1129 return D->getDeclName() && isa<NamedDecl>(Other) &&
1130 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1131}
1132
1133template<typename ForwardIterator>
1134static NamedDecl *findInstantiationOf(ASTContext &Ctx,
1135 NamedDecl *D,
1136 ForwardIterator first,
1137 ForwardIterator last) {
1138 for (; first != last; ++first)
1139 if (isInstantiationOf(Ctx, D, *first))
1140 return cast<NamedDecl>(*first);
1141
1142 return 0;
1143}
1144
John McCall02cace72009-08-28 07:59:38 +00001145/// \brief Finds the instantiation of the given declaration context
1146/// within the current instantiation.
1147///
1148/// \returns NULL if there was an error
1149DeclContext *Sema::FindInstantiatedContext(DeclContext* DC) {
1150 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
1151 Decl* ID = FindInstantiatedDecl(D);
1152 return cast_or_null<DeclContext>(ID);
1153 } else return DC;
1154}
1155
Douglas Gregored961e72009-05-27 17:54:46 +00001156/// \brief Find the instantiation of the given declaration within the
1157/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001158///
1159/// This routine is intended to be used when \p D is a declaration
1160/// referenced from within a template, that needs to mapped into the
1161/// corresponding declaration within an instantiation. For example,
1162/// given:
1163///
1164/// \code
1165/// template<typename T>
1166/// struct X {
1167/// enum Kind {
1168/// KnownValue = sizeof(T)
1169/// };
1170///
1171/// bool getKind() const { return KnownValue; }
1172/// };
1173///
1174/// template struct X<int>;
1175/// \endcode
1176///
1177/// In the instantiation of X<int>::getKind(), we need to map the
1178/// EnumConstantDecl for KnownValue (which refers to
1179/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001180/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1181/// this mapping from within the instantiation of X<int>.
John McCallce3ff2b2009-08-25 22:02:44 +00001182NamedDecl * Sema::FindInstantiatedDecl(NamedDecl *D) {
Douglas Gregor815215d2009-05-27 05:35:12 +00001183 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001184 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1185 // D is a local of some kind. Look into the map of local
1186 // declarations to their instantiations.
1187 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1188 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001189
John McCall02cace72009-08-28 07:59:38 +00001190 ParentDC = FindInstantiatedContext(ParentDC);
1191 if (!ParentDC) return 0;
Douglas Gregor815215d2009-05-27 05:35:12 +00001192
Douglas Gregor815215d2009-05-27 05:35:12 +00001193 if (ParentDC != D->getDeclContext()) {
1194 // We performed some kind of instantiation in the parent context,
1195 // so now we need to look into the instantiated parent context to
1196 // find the instantiation of the declaration D.
1197 NamedDecl *Result = 0;
1198 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001199 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001200 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1201 } else {
1202 // Since we don't have a name for the entity we're looking for,
1203 // our only option is to walk through all of the declarations to
1204 // find that name. This will occur in a few cases:
1205 //
1206 // - anonymous struct/union within a template
1207 // - unnamed class/struct/union/enum within a template
1208 //
1209 // FIXME: Find a better way to find these instantiations!
1210 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001211 ParentDC->decls_begin(),
1212 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001213 }
1214 assert(Result && "Unable to find instantiation of declaration!");
1215 D = Result;
1216 }
1217
Douglas Gregor815215d2009-05-27 05:35:12 +00001218 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregored961e72009-05-27 17:54:46 +00001219 if (ClassTemplateDecl *ClassTemplate
1220 = Record->getDescribedClassTemplate()) {
1221 // When the declaration D was parsed, it referred to the current
1222 // instantiation. Therefore, look through the current context,
1223 // which contains actual instantiations, to find the
1224 // instantiation of the "current instantiation" that D refers
1225 // to. Alternatively, we could just instantiate the
1226 // injected-class-name with the current template arguments, but
1227 // such an instantiation is far more expensive.
1228 for (DeclContext *DC = CurContext; !DC->isFileContext();
1229 DC = DC->getParent()) {
1230 if (ClassTemplateSpecializationDecl *Spec
1231 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001232 if (Spec->getSpecializedTemplate()->getCanonicalDecl()
1233 == ClassTemplate->getCanonicalDecl())
Douglas Gregored961e72009-05-27 17:54:46 +00001234 return Spec;
1235 }
1236
1237 assert(false &&
1238 "Unable to find declaration for the current instantiation");
Douglas Gregor815215d2009-05-27 05:35:12 +00001239 }
1240
1241 return D;
1242}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001243
1244/// \brief Performs template instantiation for all implicit template
1245/// instantiations we have seen until this point.
1246void Sema::PerformPendingImplicitInstantiations() {
1247 while (!PendingImplicitInstantiations.empty()) {
1248 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001249 PendingImplicitInstantiations.pop_front();
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001250
Douglas Gregor7caa6822009-07-24 20:34:43 +00001251 // Instantiate function definitions
1252 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001253 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001254 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001255 continue;
1256 }
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001257
Douglas Gregor7caa6822009-07-24 20:34:43 +00001258 // Instantiate static data member definitions.
1259 VarDecl *Var = cast<VarDecl>(Inst.first);
1260 assert(Var->isStaticDataMember() && "Not a static data member?");
1261 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001262 }
1263}