blob: 213855741cb4ebdde3b773c487008cab42605ba0 [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);
501 }
Douglas Gregore53060f2009-06-25 22:08:12 +0000502
503 if (InitFunctionInstantiation(Function, D))
504 Function->setInvalidDecl();
505
506 bool Redeclaration = false;
507 bool OverloadableAttrRequired = false;
508 NamedDecl *PrevDecl = 0;
509 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
510 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000511
Douglas Gregor127102b2009-06-29 20:59:39 +0000512 if (FunctionTemplate) {
513 // Record this function template specialization.
514 Function->setFunctionTemplateSpecialization(SemaRef.Context,
515 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000516 &TemplateArgs.getInnermost(),
Douglas Gregor127102b2009-06-29 20:59:39 +0000517 InsertPos);
John McCallfd810b12009-08-14 02:03:10 +0000518 }
519
Douglas Gregore53060f2009-06-25 22:08:12 +0000520 return Function;
521}
522
Douglas Gregord60e1052009-08-27 16:57:43 +0000523Decl *
524TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
525 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000526 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
527 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000528 if (FunctionTemplate && !TemplateParams) {
529 // We are creating a function template specialization from a function
530 // template. Check whether there is already a function template
531 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000532 llvm::FoldingSetNodeID ID;
533 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000534 TemplateArgs.getInnermost().getFlatArgumentList(),
535 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000536 SemaRef.Context);
537
538 FunctionTemplateSpecializationInfo *Info
539 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
540 InsertPos);
541
542 // If we already have a function template specialization, return it.
543 if (Info)
544 return Info->Function;
545 }
546
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000547 Sema::LocalInstantiationScope Scope(SemaRef);
548
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000549 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000550 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000551 if (T.isNull())
552 return 0;
553
554 // Build the instantiated method declaration.
555 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000556 CXXMethodDecl *Method = 0;
557
558 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000559 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000560 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
561 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
562 SemaRef.Context.getCanonicalType(ClassTy));
563 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000564 Constructor->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000565 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000566 Constructor->getDeclaratorInfo(),
567 Constructor->isExplicit(),
568 Constructor->isInline(), false);
569 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
570 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
571 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
572 SemaRef.Context.getCanonicalType(ClassTy));
573 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
574 Destructor->getLocation(), Name,
575 T, Destructor->isInline(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000576 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
577 CanQualType ConvTy
578 = SemaRef.Context.getCanonicalType(
579 T->getAsFunctionType()->getResultType());
580 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
581 ConvTy);
582 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
583 Conversion->getLocation(), Name,
584 T, Conversion->getDeclaratorInfo(),
585 Conversion->isInline(),
586 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000587 } else {
588 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
589 D->getDeclName(), T, D->getDeclaratorInfo(),
590 D->isStatic(), D->isInline());
591 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000592
Douglas Gregord60e1052009-08-27 16:57:43 +0000593 if (TemplateParams) {
594 // Our resulting instantiation is actually a function template, since we
595 // are substituting only the outer template parameters. For example, given
596 //
597 // template<typename T>
598 // struct X {
599 // template<typename U> void f(T, U);
600 // };
601 //
602 // X<int> x;
603 //
604 // We are instantiating the member template "f" within X<int>, which means
605 // substituting int for T, but leaving "f" as a member function template.
606 // Build the function template itself.
607 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
608 Method->getLocation(),
609 Method->getDeclName(),
610 TemplateParams, Method);
611 if (D->isOutOfLine())
612 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
613 Method->setDescribedFunctionTemplate(FunctionTemplate);
614 } else if (!FunctionTemplate)
Douglas Gregor6b906862009-08-21 00:16:32 +0000615 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000616
Douglas Gregor7caa6822009-07-24 20:34:43 +0000617 // If we are instantiating a member function defined
618 // out-of-line, the instantiation will have the same lexical
619 // context (which will be a namespace scope) as the template.
620 if (D->isOutOfLine())
621 Method->setLexicalDeclContext(D->getLexicalDeclContext());
622
Douglas Gregor5545e162009-03-24 00:38:23 +0000623 // Attach the parameters
624 for (unsigned P = 0; P < Params.size(); ++P)
625 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000626 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000627
628 if (InitMethodInstantiation(Method, D))
629 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000630
Douglas Gregordec06662009-08-21 18:42:58 +0000631 NamedDecl *PrevDecl = 0;
632
Douglas Gregord60e1052009-08-27 16:57:43 +0000633 if (!FunctionTemplate || TemplateParams) {
Douglas Gregordec06662009-08-21 18:42:58 +0000634 PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
635 Sema::LookupOrdinaryName, true);
636
637 // In C++, the previous declaration we find might be a tag type
638 // (class or enum). In this case, the new declaration will hide the
639 // tag type. Note that this does does not apply if we're declaring a
640 // typedef (C++ [dcl.typedef]p4).
641 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
642 PrevDecl = 0;
643 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000644
Douglas Gregord60e1052009-08-27 16:57:43 +0000645 if (FunctionTemplate && !TemplateParams)
Douglas Gregor6b906862009-08-21 00:16:32 +0000646 // Record this function template specialization.
647 Method->setFunctionTemplateSpecialization(SemaRef.Context,
648 FunctionTemplate,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000649 &TemplateArgs.getInnermost(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000650 InsertPos);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000651
652 bool Redeclaration = false;
653 bool OverloadableAttrRequired = false;
654 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
655 /*FIXME:*/OverloadableAttrRequired);
656
657 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
Douglas Gregordec06662009-08-21 18:42:58 +0000658 Owner->addDecl(Method);
659
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000660 return Method;
661}
662
Douglas Gregor615c5d42009-03-24 16:43:20 +0000663Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000664 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000665}
666
Douglas Gregor03b2b072009-03-24 00:15:49 +0000667Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000668 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000669}
670
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000671Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000672 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000673}
674
Douglas Gregor6477b692009-03-25 15:04:13 +0000675ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000676 QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000677 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000678 if (OrigT.isNull())
679 return 0;
680
681 QualType T = SemaRef.adjustParameterType(OrigT);
682
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000683 // Allocate the parameter
684 ParmVarDecl *Param = 0;
685 if (T == OrigT)
686 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000687 D->getIdentifier(), T, D->getDeclaratorInfo(),
688 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000689 else
690 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
691 D->getLocation(), D->getIdentifier(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000692 T, D->getDeclaratorInfo(), OrigT,
693 D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000694
Anders Carlsson9351c172009-08-25 03:18:48 +0000695 // Mark the default argument as being uninstantiated.
696 if (Expr *Arg = D->getDefaultArg())
697 Param->setUninstantiatedDefaultArg(Arg);
698
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000699 // Note: we don't try to instantiate function parameters until after
700 // we've instantiated the function's type. Therefore, we don't have
701 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000702 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000703 return Param;
704}
705
706Decl *
707TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
708 // Since parameter types can decay either before or after
709 // instantiation, we simply treat OriginalParmVarDecls as
710 // ParmVarDecls the same way, and create one or the other depending
711 // on what happens after template instantiation.
712 return VisitParmVarDecl(D);
713}
714
John McCalle29ba202009-08-20 01:44:21 +0000715Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
716 TemplateTypeParmDecl *D) {
717 // TODO: don't always clone when decls are refcounted.
718 const Type* T = D->getTypeForDecl();
719 assert(T->isTemplateTypeParmType());
720 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
721
722 TemplateTypeParmDecl *Inst =
723 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
724 TTPT->getDepth(), TTPT->getIndex(),
725 TTPT->getName(),
726 D->wasDeclaredWithTypename(),
727 D->isParameterPack());
728
729 if (D->hasDefaultArgument()) {
730 QualType DefaultPattern = D->getDefaultArgument();
731 QualType DefaultInst
John McCallce3ff2b2009-08-25 22:02:44 +0000732 = SemaRef.SubstType(DefaultPattern, TemplateArgs,
733 D->getDefaultArgumentLoc(),
734 D->getDeclName());
John McCalle29ba202009-08-20 01:44:21 +0000735
736 Inst->setDefaultArgument(DefaultInst,
737 D->getDefaultArgumentLoc(),
738 D->defaultArgumentWasInherited() /* preserve? */);
739 }
740
741 return Inst;
742}
743
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000744Decl *
745TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
746 NestedNameSpecifier *NNS =
747 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
748 D->getTargetNestedNameRange(),
749 TemplateArgs);
750 if (!NNS)
751 return 0;
752
753 CXXScopeSpec SS;
754 SS.setRange(D->getTargetNestedNameRange());
755 SS.setScopeRep(NNS);
756
757 return SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
758 D->getTargetNameLocation(),
759 D->getTargetName(), 0, D->isTypeName());
760}
761
John McCallce3ff2b2009-08-25 22:02:44 +0000762Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000763 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000764 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000765 return Instantiator.Visit(D);
766}
767
John McCalle29ba202009-08-20 01:44:21 +0000768/// \brief Instantiates a nested template parameter list in the current
769/// instantiation context.
770///
771/// \param L The parameter list to instantiate
772///
773/// \returns NULL if there was an error
774TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000775TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +0000776 // Get errors for all the parameters before bailing out.
777 bool Invalid = false;
778
779 unsigned N = L->size();
780 typedef llvm::SmallVector<Decl*,8> ParamVector;
781 ParamVector Params;
782 Params.reserve(N);
783 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
784 PI != PE; ++PI) {
785 Decl *D = Visit(*PI);
786 Params.push_back(D);
787 Invalid = Invalid || !D;
788 }
789
790 // Clean up if we had an error.
791 if (Invalid) {
792 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
793 PI != PE; ++PI)
794 if (*PI)
795 (*PI)->Destroy(SemaRef.Context);
796 return NULL;
797 }
798
799 TemplateParameterList *InstL
800 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
801 L->getLAngleLoc(), &Params.front(), N,
802 L->getRAngleLoc());
803 return InstL;
804}
805
John McCallce3ff2b2009-08-25 22:02:44 +0000806/// \brief Does substitution on the type of the given function, including
807/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +0000808///
John McCallce3ff2b2009-08-25 22:02:44 +0000809/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +0000810///
811/// \param Params the instantiated parameter declarations
812
John McCallce3ff2b2009-08-25 22:02:44 +0000813/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +0000814/// type if there was an error.
815QualType
John McCallce3ff2b2009-08-25 22:02:44 +0000816TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +0000817 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
818 bool InvalidDecl = false;
819
John McCallce3ff2b2009-08-25 22:02:44 +0000820 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +0000821 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000822 llvm::SmallVector<QualType, 4> ParamTys;
Douglas Gregor5545e162009-03-24 00:38:23 +0000823 for (FunctionDecl::param_iterator P = D->param_begin(),
824 PEnd = D->param_end();
825 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000826 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000827 if (PInst->getType()->isVoidType()) {
828 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
829 PInst->setInvalidDecl();
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000830 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
831 PInst->getType(),
832 diag::err_abstract_type_in_decl,
833 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000834 PInst->setInvalidDecl();
835
836 Params.push_back(PInst);
837 ParamTys.push_back(PInst->getType());
838
839 if (PInst->isInvalidDecl())
840 InvalidDecl = true;
841 } else
842 InvalidDecl = true;
843 }
844
845 // FIXME: Deallocate dead declarations.
846 if (InvalidDecl)
847 return QualType();
848
849 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
850 assert(Proto && "Missing prototype?");
851 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +0000852 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
853 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +0000854 if (ResultType.isNull())
855 return QualType();
856
Jay Foadbeaaccd2009-05-21 09:52:38 +0000857 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000858 Proto->isVariadic(), Proto->getTypeQuals(),
859 D->getLocation(), D->getDeclName());
860}
861
Douglas Gregore53060f2009-06-25 22:08:12 +0000862/// \brief Initializes the common fields of an instantiation function
863/// declaration (New) from the corresponding fields of its template (Tmpl).
864///
865/// \returns true if there was an error
866bool
867TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
868 FunctionDecl *Tmpl) {
869 if (Tmpl->isDeleted())
870 New->setDeleted();
Douglas Gregorcca9e962009-07-01 22:01:06 +0000871
872 // If we are performing substituting explicitly-specified template arguments
873 // or deduced template arguments into a function template and we reach this
874 // point, we are now past the point where SFINAE applies and have committed
875 // to keeping the new function template specialization. We therefore
876 // convert the active template instantiation for the function template
877 // into a template instantiation for this specific function template
878 // specialization, which is not a SFINAE context, so that we diagnose any
879 // further errors in the declaration itself.
880 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
881 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
882 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
883 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
884 if (FunctionTemplateDecl *FunTmpl
885 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
886 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
887 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +0000888 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000889 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
890 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
891 }
892 }
893
Douglas Gregore53060f2009-06-25 22:08:12 +0000894 return false;
895}
896
Douglas Gregor5545e162009-03-24 00:38:23 +0000897/// \brief Initializes common fields of an instantiated method
898/// declaration (New) from the corresponding fields of its template
899/// (Tmpl).
900///
901/// \returns true if there was an error
902bool
903TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
904 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000905 if (InitFunctionInstantiation(New, Tmpl))
906 return true;
907
Douglas Gregor5545e162009-03-24 00:38:23 +0000908 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
909 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000910 if (Tmpl->isVirtualAsWritten()) {
911 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000912 Record->setAggregate(false);
913 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +0000914 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +0000915 Record->setPolymorphic(true);
916 }
Douglas Gregor5545e162009-03-24 00:38:23 +0000917 if (Tmpl->isPure()) {
918 New->setPure();
919 Record->setAbstract(true);
920 }
921
922 // FIXME: attributes
923 // FIXME: New needs a pointer to Tmpl
924 return false;
925}
Douglas Gregora58861f2009-05-13 20:28:22 +0000926
927/// \brief Instantiate the definition of the given function from its
928/// template.
929///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000930/// \param PointOfInstantiation the point at which the instantiation was
931/// required. Note that this is not precisely a "point of instantiation"
932/// for the function, but it's close.
933///
Douglas Gregora58861f2009-05-13 20:28:22 +0000934/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000935/// function template specialization or member function of a class template
936/// specialization.
937///
938/// \param Recursive if true, recursively instantiates any functions that
939/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000940void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000941 FunctionDecl *Function,
942 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000943 if (Function->isInvalidDecl())
944 return;
945
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000946 assert(!Function->getBody() && "Already instantiated!");
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000947
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000948 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +0000949 const FunctionDecl *PatternDecl = 0;
950 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate())
951 PatternDecl = Primary->getTemplatedDecl();
952 else
953 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000954 Stmt *Pattern = 0;
955 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000956 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000957
958 if (!Pattern)
959 return;
960
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000961 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
962 if (Inst)
963 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000964
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000965 // If we're performing recursive template instantiation, create our own
966 // queue of pending implicit instantiations that we will instantiate later,
967 // while we're still within our own instantiation context.
968 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
969 if (Recursive)
970 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
971
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000972 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
973
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000974 // Introduce a new scope where local variable instantiations will be
975 // recorded.
976 LocalInstantiationScope Scope(*this);
977
978 // Introduce the instantiated function parameters into the local
979 // instantiation scope.
980 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
981 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
982 Function->getParamDecl(I));
983
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000984 // Enter the scope of this instantiation. We don't use
985 // PushDeclContext because we don't have a scope.
986 DeclContext *PreviousContext = CurContext;
987 CurContext = Function;
988
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000989 // Instantiate the function body.
990 OwningStmtResult Body
John McCallce3ff2b2009-08-25 22:02:44 +0000991 = SubstStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000992
993 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
994 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000995
996 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000997
998 DeclGroupRef DG(Function);
999 Consumer.HandleTopLevelDecl(DG);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001000
1001 if (Recursive) {
1002 // Instantiate any pending implicit instantiations found during the
1003 // instantiation of this template.
1004 PerformPendingImplicitInstantiations();
1005
1006 // Restore the set of pending implicit instantiations.
1007 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1008 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001009}
1010
1011/// \brief Instantiate the definition of the given variable from its
1012/// template.
1013///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001014/// \param PointOfInstantiation the point at which the instantiation was
1015/// required. Note that this is not precisely a "point of instantiation"
1016/// for the function, but it's close.
1017///
1018/// \param Var the already-instantiated declaration of a static member
1019/// variable of a class template specialization.
1020///
1021/// \param Recursive if true, recursively instantiates any functions that
1022/// are required by this instantiation.
1023void Sema::InstantiateStaticDataMemberDefinition(
1024 SourceLocation PointOfInstantiation,
1025 VarDecl *Var,
1026 bool Recursive) {
1027 if (Var->isInvalidDecl())
1028 return;
1029
1030 // Find the out-of-line definition of this static data member.
1031 // FIXME: Do we have to look for specializations separately?
1032 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1033 bool FoundOutOfLineDef = false;
1034 assert(Def && "This data member was not instantiated from a template?");
1035 assert(Def->isStaticDataMember() && "Not a static data member?");
1036 for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
1037 RDEnd = Def->redecls_end();
1038 RD != RDEnd; ++RD) {
1039 if (RD->getLexicalDeclContext()->isFileContext()) {
1040 Def = *RD;
1041 FoundOutOfLineDef = true;
1042 }
1043 }
1044
1045 if (!FoundOutOfLineDef) {
1046 // We did not find an out-of-line definition of this static data member,
1047 // so we won't perform any instantiation. Rather, we rely on the user to
1048 // instantiate this definition (or provide a specialization for it) in
1049 // another translation unit.
1050 return;
1051 }
1052
1053 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1054 if (Inst)
1055 return;
1056
1057 // If we're performing recursive template instantiation, create our own
1058 // queue of pending implicit instantiations that we will instantiate later,
1059 // while we're still within our own instantiation context.
1060 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1061 if (Recursive)
1062 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1063
1064 // Enter the scope of this instantiation. We don't use
1065 // PushDeclContext because we don't have a scope.
1066 DeclContext *PreviousContext = CurContext;
1067 CurContext = Var->getDeclContext();
1068
1069#if 0
1070 // Instantiate the initializer of this static data member.
1071 OwningExprResult Init
1072 = InstantiateExpr(Def->getInit(), getTemplateInstantiationArgs(Var));
1073 if (Init.isInvalid()) {
1074 // If instantiation of the initializer failed, mark the declaration invalid
1075 // and don't instantiate anything else that was triggered by this
1076 // instantiation.
1077 Var->setInvalidDecl();
1078
1079 // Restore the set of pending implicit instantiations.
1080 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1081
1082 return;
1083 }
1084
1085 // Type-check the initializer.
1086 if (Init.get())
1087 AddInitializerToDecl(DeclPtrTy::make(Var), move(Init),
1088 Def->hasCXXDirectInitializer());
1089 else
1090 ActOnUninitializedDecl(DeclPtrTy::make(Var), false);
1091#else
John McCallce3ff2b2009-08-25 22:02:44 +00001092 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001093 getTemplateInstantiationArgs(Var)));
1094#endif
1095
1096 CurContext = PreviousContext;
1097
1098 if (Var) {
1099 DeclGroupRef DG(Var);
1100 Consumer.HandleTopLevelDecl(DG);
1101 }
1102
1103 if (Recursive) {
1104 // Instantiate any pending implicit instantiations found during the
1105 // instantiation of this template.
1106 PerformPendingImplicitInstantiations();
1107
1108 // Restore the set of pending implicit instantiations.
1109 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1110 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001111}
Douglas Gregor815215d2009-05-27 05:35:12 +00001112
1113static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
1114 if (D->getKind() != Other->getKind())
1115 return false;
1116
1117 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001118 return Record->getInstantiatedFromMemberClass()->getCanonicalDecl()
1119 == D->getCanonicalDecl();
Douglas Gregor815215d2009-05-27 05:35:12 +00001120
1121 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001122 return Function->getInstantiatedFromMemberFunction()->getCanonicalDecl()
1123 == D->getCanonicalDecl();
Douglas Gregor815215d2009-05-27 05:35:12 +00001124
Douglas Gregor8dbc3c62009-05-27 17:20:35 +00001125 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001126 return Enum->getInstantiatedFromMemberEnum()->getCanonicalDecl()
1127 == D->getCanonicalDecl();
Douglas Gregor815215d2009-05-27 05:35:12 +00001128
Douglas Gregor7caa6822009-07-24 20:34:43 +00001129 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
1130 if (Var->isStaticDataMember())
1131 return Var->getInstantiatedFromStaticDataMember()->getCanonicalDecl()
1132 == D->getCanonicalDecl();
1133
Douglas Gregor815215d2009-05-27 05:35:12 +00001134 // FIXME: How can we find instantiations of anonymous unions?
1135
1136 return D->getDeclName() && isa<NamedDecl>(Other) &&
1137 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1138}
1139
1140template<typename ForwardIterator>
1141static NamedDecl *findInstantiationOf(ASTContext &Ctx,
1142 NamedDecl *D,
1143 ForwardIterator first,
1144 ForwardIterator last) {
1145 for (; first != last; ++first)
1146 if (isInstantiationOf(Ctx, D, *first))
1147 return cast<NamedDecl>(*first);
1148
1149 return 0;
1150}
1151
John McCall02cace72009-08-28 07:59:38 +00001152/// \brief Finds the instantiation of the given declaration context
1153/// within the current instantiation.
1154///
1155/// \returns NULL if there was an error
1156DeclContext *Sema::FindInstantiatedContext(DeclContext* DC) {
1157 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
1158 Decl* ID = FindInstantiatedDecl(D);
1159 return cast_or_null<DeclContext>(ID);
1160 } else return DC;
1161}
1162
Douglas Gregored961e72009-05-27 17:54:46 +00001163/// \brief Find the instantiation of the given declaration within the
1164/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001165///
1166/// This routine is intended to be used when \p D is a declaration
1167/// referenced from within a template, that needs to mapped into the
1168/// corresponding declaration within an instantiation. For example,
1169/// given:
1170///
1171/// \code
1172/// template<typename T>
1173/// struct X {
1174/// enum Kind {
1175/// KnownValue = sizeof(T)
1176/// };
1177///
1178/// bool getKind() const { return KnownValue; }
1179/// };
1180///
1181/// template struct X<int>;
1182/// \endcode
1183///
1184/// In the instantiation of X<int>::getKind(), we need to map the
1185/// EnumConstantDecl for KnownValue (which refers to
1186/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001187/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1188/// this mapping from within the instantiation of X<int>.
John McCallce3ff2b2009-08-25 22:02:44 +00001189NamedDecl * Sema::FindInstantiatedDecl(NamedDecl *D) {
Douglas Gregor815215d2009-05-27 05:35:12 +00001190 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001191 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
1192 // D is a local of some kind. Look into the map of local
1193 // declarations to their instantiations.
1194 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1195 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001196
John McCall02cace72009-08-28 07:59:38 +00001197 ParentDC = FindInstantiatedContext(ParentDC);
1198 if (!ParentDC) return 0;
Douglas Gregor815215d2009-05-27 05:35:12 +00001199
Douglas Gregor815215d2009-05-27 05:35:12 +00001200 if (ParentDC != D->getDeclContext()) {
1201 // We performed some kind of instantiation in the parent context,
1202 // so now we need to look into the instantiated parent context to
1203 // find the instantiation of the declaration D.
1204 NamedDecl *Result = 0;
1205 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001206 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001207 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1208 } else {
1209 // Since we don't have a name for the entity we're looking for,
1210 // our only option is to walk through all of the declarations to
1211 // find that name. This will occur in a few cases:
1212 //
1213 // - anonymous struct/union within a template
1214 // - unnamed class/struct/union/enum within a template
1215 //
1216 // FIXME: Find a better way to find these instantiations!
1217 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001218 ParentDC->decls_begin(),
1219 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00001220 }
1221 assert(Result && "Unable to find instantiation of declaration!");
1222 D = Result;
1223 }
1224
Douglas Gregor815215d2009-05-27 05:35:12 +00001225 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregored961e72009-05-27 17:54:46 +00001226 if (ClassTemplateDecl *ClassTemplate
1227 = Record->getDescribedClassTemplate()) {
1228 // When the declaration D was parsed, it referred to the current
1229 // instantiation. Therefore, look through the current context,
1230 // which contains actual instantiations, to find the
1231 // instantiation of the "current instantiation" that D refers
1232 // to. Alternatively, we could just instantiate the
1233 // injected-class-name with the current template arguments, but
1234 // such an instantiation is far more expensive.
1235 for (DeclContext *DC = CurContext; !DC->isFileContext();
1236 DC = DC->getParent()) {
1237 if (ClassTemplateSpecializationDecl *Spec
1238 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001239 if (Spec->getSpecializedTemplate()->getCanonicalDecl()
1240 == ClassTemplate->getCanonicalDecl())
Douglas Gregored961e72009-05-27 17:54:46 +00001241 return Spec;
1242 }
1243
1244 assert(false &&
1245 "Unable to find declaration for the current instantiation");
Douglas Gregor815215d2009-05-27 05:35:12 +00001246 }
1247
1248 return D;
1249}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001250
1251/// \brief Performs template instantiation for all implicit template
1252/// instantiations we have seen until this point.
1253void Sema::PerformPendingImplicitInstantiations() {
1254 while (!PendingImplicitInstantiations.empty()) {
1255 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001256 PendingImplicitInstantiations.pop_front();
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001257
Douglas Gregor7caa6822009-07-24 20:34:43 +00001258 // Instantiate function definitions
1259 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001260 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001261 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001262 continue;
1263 }
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001264
Douglas Gregor7caa6822009-07-24 20:34:43 +00001265 // Instantiate static data member definitions.
1266 VarDecl *Var = cast<VarDecl>(Inst.first);
1267 assert(Var->isStaticDataMember() && "Not a static data member?");
1268 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001269 }
1270}