blob: ac0f46efe9e35679597a4ba4762c74e5539b878d [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"
18#include "llvm/Support/Compiler.h"
19
20using namespace clang;
21
22namespace {
23 class VISIBILITY_HIDDEN TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000024 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000025 Sema &SemaRef;
26 DeclContext *Owner;
Douglas Gregor7e063902009-05-11 23:53:27 +000027 const TemplateArgumentList &TemplateArgs;
Douglas Gregor8dbc2692009-03-17 21:15:40 +000028
29 public:
30 typedef Sema::OwningExprResult OwningExprResult;
31
32 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +000033 const TemplateArgumentList &TemplateArgs)
34 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Douglas Gregor8dbc2692009-03-17 21:15:40 +000035
Mike Stump390b4cc2009-05-16 07:39:55 +000036 // FIXME: Once we get closer to completion, replace these manually-written
37 // declarations with automatically-generated ones from
38 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000039 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
40 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000041 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000042 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000043 Decl *VisitFieldDecl(FieldDecl *D);
44 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
45 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000046 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregore53060f2009-06-25 22:08:12 +000047 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregord475b8d2009-03-25 21:17:03 +000048 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000049 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
Douglas Gregor615c5d42009-03-24 16:43:20 +000050 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000051 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000052 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000053 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +000054 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor5545e162009-03-24 00:38:23 +000055
Douglas Gregor8dbc2692009-03-17 21:15:40 +000056 // Base case. FIXME: Remove once we can instantiate everything.
57 Decl *VisitDecl(Decl *) {
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000058 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor8dbc2692009-03-17 21:15:40 +000059 return 0;
60 }
Douglas Gregor5545e162009-03-24 00:38:23 +000061
62 // Helper functions for instantiating methods.
63 QualType InstantiateFunctionType(FunctionDecl *D,
64 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000065 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000066 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000067 };
68}
69
Douglas Gregor4f722be2009-03-25 15:45:12 +000070Decl *
71TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
72 assert(false && "Translation units cannot be instantiated");
73 return D;
74}
75
76Decl *
77TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
78 assert(false && "Namespaces cannot be instantiated");
79 return D;
80}
81
Douglas Gregor8dbc2692009-03-17 21:15:40 +000082Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
83 bool Invalid = false;
84 QualType T = D->getUnderlyingType();
85 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +000086 T = SemaRef.InstantiateType(T, TemplateArgs,
87 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +000088 if (T.isNull()) {
89 Invalid = true;
90 T = SemaRef.Context.IntTy;
91 }
92 }
93
94 // Create the new typedef
95 TypedefDecl *Typedef
96 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
97 D->getIdentifier(), T);
98 if (Invalid)
99 Typedef->setInvalidDecl();
100
Douglas Gregor6ab35242009-04-09 21:40:53 +0000101 Owner->addDecl(SemaRef.Context, Typedef);
Douglas Gregorbc221632009-05-28 16:34:51 +0000102
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000103 return Typedef;
104}
105
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000106Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
107 // Instantiate the type of the declaration
108 QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs,
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000109 D->getTypeSpecStartLoc(),
110 D->getDeclName());
111 if (T.isNull())
112 return 0;
113
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000114 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000115 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
116 D->getLocation(), D->getIdentifier(),
117 T, D->getStorageClass(),
118 D->getTypeSpecStartLoc());
119 Var->setThreadSpecified(D->isThreadSpecified());
120 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
121 Var->setDeclaredInCondition(D->isDeclaredInCondition());
122
Mike Stump390b4cc2009-05-16 07:39:55 +0000123 // FIXME: In theory, we could have a previous declaration for variables that
124 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000125 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000126 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000127 Owner->addDecl(SemaRef.Context, Var);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000128
129 if (D->getInit()) {
130 OwningExprResult Init
Douglas Gregor7e063902009-05-11 23:53:27 +0000131 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000132 if (Init.isInvalid())
133 Var->setInvalidDecl();
134 else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000135 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000136 D->hasCXXDirectInitializer());
Douglas Gregord308e622009-05-18 20:51:54 +0000137 } else {
138 // FIXME: Call ActOnUninitializedDecl? (Not always)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000139 }
140
141 return Var;
142}
143
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000144Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
145 bool Invalid = false;
146 QualType T = D->getType();
147 if (T->isDependentType()) {
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000148 T = SemaRef.InstantiateType(T, TemplateArgs,
149 D->getLocation(), D->getDeclName());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000150 if (!T.isNull() && T->isFunctionType()) {
151 // C++ [temp.arg.type]p3:
152 // If a declaration acquires a function type through a type
153 // dependent on a template-parameter and this causes a
154 // declaration that does not use the syntactic form of a
155 // function declarator to have function type, the program is
156 // ill-formed.
157 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
158 << T;
159 T = QualType();
160 Invalid = true;
161 }
162 }
163
164 Expr *BitWidth = D->getBitWidth();
165 if (Invalid)
166 BitWidth = 0;
167 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000168 // The bit-width expression is not potentially evaluated.
169 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
170
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000171 OwningExprResult InstantiatedBitWidth
Douglas Gregor7e063902009-05-11 23:53:27 +0000172 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000173 if (InstantiatedBitWidth.isInvalid()) {
174 Invalid = true;
175 BitWidth = 0;
176 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000177 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000178 }
179
180 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
181 cast<RecordDecl>(Owner),
182 D->getLocation(),
183 D->isMutable(),
184 BitWidth,
185 D->getAccess(),
186 0);
187 if (Field) {
188 if (Invalid)
189 Field->setInvalidDecl();
190
Douglas Gregor6ab35242009-04-09 21:40:53 +0000191 Owner->addDecl(SemaRef.Context, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000192 }
193
194 return Field;
195}
196
197Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
198 Expr *AssertExpr = D->getAssertExpr();
199
Douglas Gregorac7610d2009-06-22 20:57:11 +0000200 // The expression in a static assertion is not potentially evaluated.
201 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
202
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000203 OwningExprResult InstantiatedAssertExpr
Douglas Gregor7e063902009-05-11 23:53:27 +0000204 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000205 if (InstantiatedAssertExpr.isInvalid())
206 return 0;
207
208 OwningExprResult Message = SemaRef.Clone(D->getMessage());
209 Decl *StaticAssert
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
211 move(InstantiatedAssertExpr),
212 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000213 return StaticAssert;
214}
215
216Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
217 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
218 D->getLocation(), D->getIdentifier(),
219 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000220 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000221 Enum->setAccess(D->getAccess());
Douglas Gregor6ab35242009-04-09 21:40:53 +0000222 Owner->addDecl(SemaRef.Context, Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000223 Enum->startDefinition();
224
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000225 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000226
227 EnumConstantDecl *LastEnumConst = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000228 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
229 ECEnd = D->enumerator_end(SemaRef.Context);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000230 EC != ECEnd; ++EC) {
231 // The specified value for the enumerator.
232 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000233 if (Expr *UninstValue = EC->getInitExpr()) {
234 // The enumerator's value expression is not potentially evaluated.
235 EnterExpressionEvaluationContext Unevaluated(SemaRef,
236 Action::Unevaluated);
237
Douglas Gregor7e063902009-05-11 23:53:27 +0000238 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000239 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000240
241 // Drop the initial value and continue.
242 bool isInvalid = false;
243 if (Value.isInvalid()) {
244 Value = SemaRef.Owned((Expr *)0);
245 isInvalid = true;
246 }
247
248 EnumConstantDecl *EnumConst
249 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
250 EC->getLocation(), EC->getIdentifier(),
251 move(Value));
252
253 if (isInvalid) {
254 if (EnumConst)
255 EnumConst->setInvalidDecl();
256 Enum->setInvalidDecl();
257 }
258
259 if (EnumConst) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000260 Enum->addDecl(SemaRef.Context, EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000261 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000262 LastEnumConst = EnumConst;
263 }
264 }
265
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000266 // FIXME: Fixup LBraceLoc and RBraceLoc
267 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
268 Sema::DeclPtrTy::make(Enum),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000269 &Enumerators[0], Enumerators.size());
270
271 return Enum;
272}
273
Douglas Gregor6477b692009-03-25 15:04:13 +0000274Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
275 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
276 return 0;
277}
278
Douglas Gregord475b8d2009-03-25 21:17:03 +0000279Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
280 CXXRecordDecl *PrevDecl = 0;
281 if (D->isInjectedClassName())
282 PrevDecl = cast<CXXRecordDecl>(Owner);
283
284 CXXRecordDecl *Record
285 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
286 D->getLocation(), D->getIdentifier(), PrevDecl);
287 Record->setImplicit(D->isImplicit());
288 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000289 if (!D->isInjectedClassName())
290 Record->setInstantiationOfMemberClass(D);
291
Douglas Gregor6ab35242009-04-09 21:40:53 +0000292 Owner->addDecl(SemaRef.Context, Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000293 return Record;
294}
295
Douglas Gregore53060f2009-06-25 22:08:12 +0000296Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000297 // Check whether there is already a function template specialization for
298 // this declaration.
299 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
300 void *InsertPos = 0;
301 if (FunctionTemplate) {
302 llvm::FoldingSetNodeID ID;
303 FunctionTemplateSpecializationInfo::Profile(ID,
304 TemplateArgs.getFlatArgumentList(),
305 TemplateArgs.flat_size());
306
307 FunctionTemplateSpecializationInfo *Info
308 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
309 InsertPos);
310
311 // If we already have a function template specialization, return it.
312 if (Info)
313 return Info->Function;
314 }
Douglas Gregore53060f2009-06-25 22:08:12 +0000315
316 Sema::LocalInstantiationScope Scope(SemaRef);
317
318 llvm::SmallVector<ParmVarDecl *, 4> Params;
319 QualType T = InstantiateFunctionType(D, Params);
320 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000321 return 0;
Douglas Gregore53060f2009-06-25 22:08:12 +0000322
323 // Build the instantiated method declaration.
324 FunctionDecl *Function
325 = FunctionDecl::Create(SemaRef.Context, Owner, D->getLocation(),
326 D->getDeclName(), T, D->getStorageClass(),
327 D->isInline(), D->hasWrittenPrototype(),
328 D->getTypeSpecStartLoc());
329
330 // FIXME: friend functions
331
332 // Attach the parameters
333 for (unsigned P = 0; P < Params.size(); ++P)
334 Params[P]->setOwningFunction(Function);
335 Function->setParams(SemaRef.Context, Params.data(), Params.size());
336
337 if (InitFunctionInstantiation(Function, D))
338 Function->setInvalidDecl();
339
340 bool Redeclaration = false;
341 bool OverloadableAttrRequired = false;
342 NamedDecl *PrevDecl = 0;
343 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
344 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000345
Douglas Gregor127102b2009-06-29 20:59:39 +0000346 if (FunctionTemplate) {
347 // Record this function template specialization.
348 Function->setFunctionTemplateSpecialization(SemaRef.Context,
349 FunctionTemplate,
350 &TemplateArgs,
351 InsertPos);
352 }
353
Douglas Gregore53060f2009-06-25 22:08:12 +0000354 return Function;
355}
356
357Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
358 // FIXME: Look for existing, explicit specializations.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000359 Sema::LocalInstantiationScope Scope(SemaRef);
360
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000361 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor5545e162009-03-24 00:38:23 +0000362 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000363 if (T.isNull())
364 return 0;
365
366 // Build the instantiated method declaration.
367 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
368 CXXMethodDecl *Method
369 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
370 D->getDeclName(), T, D->isStatic(),
371 D->isInline());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000372 Method->setInstantiationOfMemberFunction(D);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000373
Douglas Gregor5545e162009-03-24 00:38:23 +0000374 // Attach the parameters
375 for (unsigned P = 0; P < Params.size(); ++P)
376 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000377 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000378
379 if (InitMethodInstantiation(Method, D))
380 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000381
382 NamedDecl *PrevDecl
383 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
384 Sema::LookupOrdinaryName, true);
385 // In C++, the previous declaration we find might be a tag type
386 // (class or enum). In this case, the new declaration will hide the
387 // tag type. Note that this does does not apply if we're declaring a
388 // typedef (C++ [dcl.typedef]p4).
389 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
390 PrevDecl = 0;
391 bool Redeclaration = false;
392 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000393 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
394 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000395
396 if (!Method->isInvalidDecl() || !PrevDecl)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000397 Owner->addDecl(SemaRef.Context, Method);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000398 return Method;
399}
400
Douglas Gregor615c5d42009-03-24 16:43:20 +0000401Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000402 // FIXME: Look for existing, explicit specializations.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000403 Sema::LocalInstantiationScope Scope(SemaRef);
404
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000405 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor615c5d42009-03-24 16:43:20 +0000406 QualType T = InstantiateFunctionType(D, Params);
407 if (T.isNull())
408 return 0;
409
410 // Build the instantiated method declaration.
411 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
412 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
413 DeclarationName Name
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000414 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
415 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregor615c5d42009-03-24 16:43:20 +0000416 CXXConstructorDecl *Constructor
417 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
418 Name, T, D->isExplicit(), D->isInline(),
419 false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000420 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000421
422 // Attach the parameters
423 for (unsigned P = 0; P < Params.size(); ++P)
424 Params[P]->setOwningFunction(Constructor);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000425 Constructor->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor615c5d42009-03-24 16:43:20 +0000426
427 if (InitMethodInstantiation(Constructor, D))
428 Constructor->setInvalidDecl();
429
430 NamedDecl *PrevDecl
431 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
432
433 // In C++, the previous declaration we find might be a tag type
434 // (class or enum). In this case, the new declaration will hide the
435 // tag type. Note that this does does not apply if we're declaring a
436 // typedef (C++ [dcl.typedef]p4).
437 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
438 PrevDecl = 0;
439 bool Redeclaration = false;
440 bool OverloadableAttrRequired = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000441 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
442 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000443
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000444 Record->addedConstructor(SemaRef.Context, Constructor);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000445 Owner->addDecl(SemaRef.Context, Constructor);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000446 return Constructor;
447}
448
Douglas Gregor03b2b072009-03-24 00:15:49 +0000449Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000450 // FIXME: Look for existing, explicit specializations.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000451 Sema::LocalInstantiationScope Scope(SemaRef);
452
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000453 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor5545e162009-03-24 00:38:23 +0000454 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000455 if (T.isNull())
456 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +0000457 assert(Params.size() == 0 && "Destructor with parameters?");
458
Douglas Gregor03b2b072009-03-24 00:15:49 +0000459 // Build the instantiated destructor declaration.
460 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor49f25ec2009-05-15 21:18:27 +0000461 QualType ClassTy =
462 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor03b2b072009-03-24 00:15:49 +0000463 CXXDestructorDecl *Destructor
464 = CXXDestructorDecl::Create(SemaRef.Context, Record,
465 D->getLocation(),
466 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
467 T, D->isInline(), false);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000468 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor5545e162009-03-24 00:38:23 +0000469 if (InitMethodInstantiation(Destructor, D))
470 Destructor->setInvalidDecl();
Douglas Gregor03b2b072009-03-24 00:15:49 +0000471
472 bool Redeclaration = false;
473 bool OverloadableAttrRequired = false;
474 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000475 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
476 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000477 Owner->addDecl(SemaRef.Context, Destructor);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000478 return Destructor;
479}
480
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000481Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000482 // FIXME: Look for existing, explicit specializations.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000483 Sema::LocalInstantiationScope Scope(SemaRef);
484
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000485 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000486 QualType T = InstantiateFunctionType(D, Params);
487 if (T.isNull())
488 return 0;
489 assert(Params.size() == 0 && "Destructor with parameters?");
490
491 // Build the instantiated conversion declaration.
492 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
493 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
494 QualType ConvTy
495 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
496 CXXConversionDecl *Conversion
497 = CXXConversionDecl::Create(SemaRef.Context, Record,
498 D->getLocation(),
499 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
500 T, D->isInline(), D->isExplicit());
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000501 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000502 if (InitMethodInstantiation(Conversion, D))
503 Conversion->setInvalidDecl();
504
505 bool Redeclaration = false;
506 bool OverloadableAttrRequired = false;
507 NamedDecl *PrevDecl = 0;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000508 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
509 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000510 Owner->addDecl(SemaRef.Context, Conversion);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000511 return Conversion;
512}
513
Douglas Gregor6477b692009-03-25 15:04:13 +0000514ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000515 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregor7e063902009-05-11 23:53:27 +0000516 D->getLocation(), D->getDeclName());
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000517 if (OrigT.isNull())
518 return 0;
519
520 QualType T = SemaRef.adjustParameterType(OrigT);
521
522 if (D->getDefaultArg()) {
523 // FIXME: Leave a marker for "uninstantiated" default
524 // arguments. They only get instantiated on demand at the call
525 // site.
526 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
527 "sorry, dropping default argument during template instantiation");
528 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
529 << D->getDefaultArg()->getSourceRange();
530 }
531
532 // Allocate the parameter
533 ParmVarDecl *Param = 0;
534 if (T == OrigT)
535 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
536 D->getIdentifier(), T, D->getStorageClass(),
537 0);
538 else
539 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
540 D->getLocation(), D->getIdentifier(),
541 T, OrigT, D->getStorageClass(), 0);
542
543 // Note: we don't try to instantiate function parameters until after
544 // we've instantiated the function's type. Therefore, we don't have
545 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000546 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000547 return Param;
548}
549
550Decl *
551TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
552 // Since parameter types can decay either before or after
553 // instantiation, we simply treat OriginalParmVarDecls as
554 // ParmVarDecls the same way, and create one or the other depending
555 // on what happens after template instantiation.
556 return VisitParmVarDecl(D);
557}
558
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregor7e063902009-05-11 23:53:27 +0000560 const TemplateArgumentList &TemplateArgs) {
561 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000562 return Instantiator.Visit(D);
563}
564
Douglas Gregor5545e162009-03-24 00:38:23 +0000565/// \brief Instantiates the type of the given function, including
566/// instantiating all of the function parameters.
567///
568/// \param D The function that we will be instantiated
569///
570/// \param Params the instantiated parameter declarations
571
572/// \returns the instantiated function's type if successfull, a NULL
573/// type if there was an error.
574QualType
575TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
576 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
577 bool InvalidDecl = false;
578
579 // Instantiate the function parameters
Douglas Gregor7e063902009-05-11 23:53:27 +0000580 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000581 llvm::SmallVector<QualType, 4> ParamTys;
Douglas Gregor5545e162009-03-24 00:38:23 +0000582 for (FunctionDecl::param_iterator P = D->param_begin(),
583 PEnd = D->param_end();
584 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +0000585 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +0000586 if (PInst->getType()->isVoidType()) {
587 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
588 PInst->setInvalidDecl();
589 }
590 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
591 PInst->getType(),
592 diag::err_abstract_type_in_decl,
Anders Carlsson8211eff2009-03-24 01:19:16 +0000593 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +0000594 PInst->setInvalidDecl();
595
596 Params.push_back(PInst);
597 ParamTys.push_back(PInst->getType());
598
599 if (PInst->isInvalidDecl())
600 InvalidDecl = true;
601 } else
602 InvalidDecl = true;
603 }
604
605 // FIXME: Deallocate dead declarations.
606 if (InvalidDecl)
607 return QualType();
608
609 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
610 assert(Proto && "Missing prototype?");
611 QualType ResultType
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000612 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor5545e162009-03-24 00:38:23 +0000613 D->getLocation(), D->getDeclName());
614 if (ResultType.isNull())
615 return QualType();
616
Jay Foadbeaaccd2009-05-21 09:52:38 +0000617 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +0000618 Proto->isVariadic(), Proto->getTypeQuals(),
619 D->getLocation(), D->getDeclName());
620}
621
Douglas Gregore53060f2009-06-25 22:08:12 +0000622/// \brief Initializes the common fields of an instantiation function
623/// declaration (New) from the corresponding fields of its template (Tmpl).
624///
625/// \returns true if there was an error
626bool
627TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
628 FunctionDecl *Tmpl) {
629 if (Tmpl->isDeleted())
630 New->setDeleted();
631 return false;
632}
633
Douglas Gregor5545e162009-03-24 00:38:23 +0000634/// \brief Initializes common fields of an instantiated method
635/// declaration (New) from the corresponding fields of its template
636/// (Tmpl).
637///
638/// \returns true if there was an error
639bool
640TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
641 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000642 if (InitFunctionInstantiation(New, Tmpl))
643 return true;
644
Douglas Gregor5545e162009-03-24 00:38:23 +0000645 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
646 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000647 if (Tmpl->isVirtualAsWritten()) {
648 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000649 Record->setAggregate(false);
650 Record->setPOD(false);
651 Record->setPolymorphic(true);
652 }
Douglas Gregor5545e162009-03-24 00:38:23 +0000653 if (Tmpl->isPure()) {
654 New->setPure();
655 Record->setAbstract(true);
656 }
657
658 // FIXME: attributes
659 // FIXME: New needs a pointer to Tmpl
660 return false;
661}
Douglas Gregora58861f2009-05-13 20:28:22 +0000662
663/// \brief Instantiate the definition of the given function from its
664/// template.
665///
666/// \param Function the already-instantiated declaration of a
667/// function.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000668void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
669 FunctionDecl *Function) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000670 if (Function->isInvalidDecl())
671 return;
672
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000673 assert(!Function->getBody(Context) && "Already instantiated!");
674
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000675 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +0000676 const FunctionDecl *PatternDecl = 0;
677 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate())
678 PatternDecl = Primary->getTemplatedDecl();
679 else
680 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000681 Stmt *Pattern = 0;
682 if (PatternDecl)
683 Pattern = PatternDecl->getBody(Context, PatternDecl);
684
685 if (!Pattern)
686 return;
687
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000688 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
689 if (Inst)
690 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000691
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000692 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
693
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000694 // Introduce a new scope where local variable instantiations will be
695 // recorded.
696 LocalInstantiationScope Scope(*this);
697
698 // Introduce the instantiated function parameters into the local
699 // instantiation scope.
700 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
701 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
702 Function->getParamDecl(I));
703
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000704 // Enter the scope of this instantiation. We don't use
705 // PushDeclContext because we don't have a scope.
706 DeclContext *PreviousContext = CurContext;
707 CurContext = Function;
708
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000709 // Instantiate the function body.
710 OwningStmtResult Body
711 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000712
713 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
714 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000715
716 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000717
718 DeclGroupRef DG(Function);
719 Consumer.HandleTopLevelDecl(DG);
Douglas Gregora58861f2009-05-13 20:28:22 +0000720}
721
722/// \brief Instantiate the definition of the given variable from its
723/// template.
724///
725/// \param Var the already-instantiated declaration of a variable.
726void Sema::InstantiateVariableDefinition(VarDecl *Var) {
727 // FIXME: Implement this!
728}
Douglas Gregor815215d2009-05-27 05:35:12 +0000729
730static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
731 if (D->getKind() != Other->getKind())
732 return false;
733
734 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
735 return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass())
736 == Ctx.getCanonicalDecl(D);
737
738 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
739 return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction())
740 == Ctx.getCanonicalDecl(D);
741
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000742 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
743 return Ctx.getCanonicalDecl(Enum->getInstantiatedFromMemberEnum())
744 == Ctx.getCanonicalDecl(D);
Douglas Gregor815215d2009-05-27 05:35:12 +0000745
746 // FIXME: How can we find instantiations of anonymous unions?
747
748 return D->getDeclName() && isa<NamedDecl>(Other) &&
749 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
750}
751
752template<typename ForwardIterator>
753static NamedDecl *findInstantiationOf(ASTContext &Ctx,
754 NamedDecl *D,
755 ForwardIterator first,
756 ForwardIterator last) {
757 for (; first != last; ++first)
758 if (isInstantiationOf(Ctx, D, *first))
759 return cast<NamedDecl>(*first);
760
761 return 0;
762}
763
Douglas Gregored961e72009-05-27 17:54:46 +0000764/// \brief Find the instantiation of the given declaration within the
765/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +0000766///
767/// This routine is intended to be used when \p D is a declaration
768/// referenced from within a template, that needs to mapped into the
769/// corresponding declaration within an instantiation. For example,
770/// given:
771///
772/// \code
773/// template<typename T>
774/// struct X {
775/// enum Kind {
776/// KnownValue = sizeof(T)
777/// };
778///
779/// bool getKind() const { return KnownValue; }
780/// };
781///
782/// template struct X<int>;
783/// \endcode
784///
785/// In the instantiation of X<int>::getKind(), we need to map the
786/// EnumConstantDecl for KnownValue (which refers to
787/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +0000788/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
789/// this mapping from within the instantiation of X<int>.
790NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
Douglas Gregor815215d2009-05-27 05:35:12 +0000791 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000792 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
793 // D is a local of some kind. Look into the map of local
794 // declarations to their instantiations.
795 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
796 }
Douglas Gregor815215d2009-05-27 05:35:12 +0000797
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000798 if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
Douglas Gregored961e72009-05-27 17:54:46 +0000799 ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
Douglas Gregor815215d2009-05-27 05:35:12 +0000800 if (!ParentDecl)
801 return 0;
802
803 ParentDC = cast<DeclContext>(ParentDecl);
804 }
805
Douglas Gregor815215d2009-05-27 05:35:12 +0000806 if (ParentDC != D->getDeclContext()) {
807 // We performed some kind of instantiation in the parent context,
808 // so now we need to look into the instantiated parent context to
809 // find the instantiation of the declaration D.
810 NamedDecl *Result = 0;
811 if (D->getDeclName()) {
812 DeclContext::lookup_result Found
813 = ParentDC->lookup(Context, D->getDeclName());
814 Result = findInstantiationOf(Context, D, Found.first, Found.second);
815 } else {
816 // Since we don't have a name for the entity we're looking for,
817 // our only option is to walk through all of the declarations to
818 // find that name. This will occur in a few cases:
819 //
820 // - anonymous struct/union within a template
821 // - unnamed class/struct/union/enum within a template
822 //
823 // FIXME: Find a better way to find these instantiations!
824 Result = findInstantiationOf(Context, D,
825 ParentDC->decls_begin(Context),
826 ParentDC->decls_end(Context));
827 }
828 assert(Result && "Unable to find instantiation of declaration!");
829 D = Result;
830 }
831
Douglas Gregor815215d2009-05-27 05:35:12 +0000832 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregored961e72009-05-27 17:54:46 +0000833 if (ClassTemplateDecl *ClassTemplate
834 = Record->getDescribedClassTemplate()) {
835 // When the declaration D was parsed, it referred to the current
836 // instantiation. Therefore, look through the current context,
837 // which contains actual instantiations, to find the
838 // instantiation of the "current instantiation" that D refers
839 // to. Alternatively, we could just instantiate the
840 // injected-class-name with the current template arguments, but
841 // such an instantiation is far more expensive.
842 for (DeclContext *DC = CurContext; !DC->isFileContext();
843 DC = DC->getParent()) {
844 if (ClassTemplateSpecializationDecl *Spec
845 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
846 if (Context.getCanonicalDecl(Spec->getSpecializedTemplate())
847 == Context.getCanonicalDecl(ClassTemplate))
848 return Spec;
849 }
850
851 assert(false &&
852 "Unable to find declaration for the current instantiation");
Douglas Gregor815215d2009-05-27 05:35:12 +0000853 }
854
855 return D;
856}
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000857
858/// \brief Performs template instantiation for all implicit template
859/// instantiations we have seen until this point.
860void Sema::PerformPendingImplicitInstantiations() {
861 while (!PendingImplicitInstantiations.empty()) {
862 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
863 PendingImplicitInstantiations.pop();
864
865 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first))
866 if (!Function->getBody(Context))
867 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function);
868
869 // FIXME: instantiation static member variables
870 }
871}