blob: f59719992066606b8ee0e416f779712e6e4fc317 [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
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000101 Owner->addDecl(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);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000127 Owner->addDecl(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
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000191 Owner->addDecl(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());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000222 Owner->addDecl(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;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000228 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
229 ECEnd = D->enumerator_end();
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) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000260 Enum->addDecl(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
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000292 Owner->addDecl(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)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000397 Owner->addDecl(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);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000445 Owner->addDecl(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);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000477 Owner->addDecl(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);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000510 Owner->addDecl(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();
Douglas Gregorcca9e962009-07-01 22:01:06 +0000631
632 // If we are performing substituting explicitly-specified template arguments
633 // or deduced template arguments into a function template and we reach this
634 // point, we are now past the point where SFINAE applies and have committed
635 // to keeping the new function template specialization. We therefore
636 // convert the active template instantiation for the function template
637 // into a template instantiation for this specific function template
638 // specialization, which is not a SFINAE context, so that we diagnose any
639 // further errors in the declaration itself.
640 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
641 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
642 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
643 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
644 if (FunctionTemplateDecl *FunTmpl
645 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
646 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
647 "Deduction from the wrong function template?");
648 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
649 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
650 }
651 }
652
Douglas Gregore53060f2009-06-25 22:08:12 +0000653 return false;
654}
655
Douglas Gregor5545e162009-03-24 00:38:23 +0000656/// \brief Initializes common fields of an instantiated method
657/// declaration (New) from the corresponding fields of its template
658/// (Tmpl).
659///
660/// \returns true if there was an error
661bool
662TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
663 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000664 if (InitFunctionInstantiation(New, Tmpl))
665 return true;
666
Douglas Gregor5545e162009-03-24 00:38:23 +0000667 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
668 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000669 if (Tmpl->isVirtualAsWritten()) {
670 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +0000671 Record->setAggregate(false);
672 Record->setPOD(false);
673 Record->setPolymorphic(true);
674 }
Douglas Gregor5545e162009-03-24 00:38:23 +0000675 if (Tmpl->isPure()) {
676 New->setPure();
677 Record->setAbstract(true);
678 }
679
680 // FIXME: attributes
681 // FIXME: New needs a pointer to Tmpl
682 return false;
683}
Douglas Gregora58861f2009-05-13 20:28:22 +0000684
685/// \brief Instantiate the definition of the given function from its
686/// template.
687///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000688/// \param PointOfInstantiation the point at which the instantiation was
689/// required. Note that this is not precisely a "point of instantiation"
690/// for the function, but it's close.
691///
Douglas Gregora58861f2009-05-13 20:28:22 +0000692/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000693/// function template specialization or member function of a class template
694/// specialization.
695///
696/// \param Recursive if true, recursively instantiates any functions that
697/// are required by this instantiation.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000698void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000699 FunctionDecl *Function,
700 bool Recursive) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000701 if (Function->isInvalidDecl())
702 return;
703
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000704 assert(!Function->getBody() && "Already instantiated!");
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000705
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000706 // Find the function body that we'll be substituting.
Douglas Gregor1637be72009-06-26 00:10:03 +0000707 const FunctionDecl *PatternDecl = 0;
708 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate())
709 PatternDecl = Primary->getTemplatedDecl();
710 else
711 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000712 Stmt *Pattern = 0;
713 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000714 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000715
716 if (!Pattern)
717 return;
718
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000719 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
720 if (Inst)
721 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000722
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000723 // If we're performing recursive template instantiation, create our own
724 // queue of pending implicit instantiations that we will instantiate later,
725 // while we're still within our own instantiation context.
726 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
727 if (Recursive)
728 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
729
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000730 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
731
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000732 // Introduce a new scope where local variable instantiations will be
733 // recorded.
734 LocalInstantiationScope Scope(*this);
735
736 // Introduce the instantiated function parameters into the local
737 // instantiation scope.
738 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
739 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
740 Function->getParamDecl(I));
741
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000742 // Enter the scope of this instantiation. We don't use
743 // PushDeclContext because we don't have a scope.
744 DeclContext *PreviousContext = CurContext;
745 CurContext = Function;
746
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000747 // Instantiate the function body.
748 OwningStmtResult Body
749 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregore2c31ff2009-05-15 17:59:04 +0000750
751 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
752 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000753
754 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000755
756 DeclGroupRef DG(Function);
757 Consumer.HandleTopLevelDecl(DG);
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000758
759 if (Recursive) {
760 // Instantiate any pending implicit instantiations found during the
761 // instantiation of this template.
762 PerformPendingImplicitInstantiations();
763
764 // Restore the set of pending implicit instantiations.
765 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
766 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000767}
768
769/// \brief Instantiate the definition of the given variable from its
770/// template.
771///
772/// \param Var the already-instantiated declaration of a variable.
773void Sema::InstantiateVariableDefinition(VarDecl *Var) {
774 // FIXME: Implement this!
775}
Douglas Gregor815215d2009-05-27 05:35:12 +0000776
777static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
778 if (D->getKind() != Other->getKind())
779 return false;
780
781 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
782 return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass())
783 == Ctx.getCanonicalDecl(D);
784
785 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
786 return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction())
787 == Ctx.getCanonicalDecl(D);
788
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000789 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
790 return Ctx.getCanonicalDecl(Enum->getInstantiatedFromMemberEnum())
791 == Ctx.getCanonicalDecl(D);
Douglas Gregor815215d2009-05-27 05:35:12 +0000792
793 // FIXME: How can we find instantiations of anonymous unions?
794
795 return D->getDeclName() && isa<NamedDecl>(Other) &&
796 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
797}
798
799template<typename ForwardIterator>
800static NamedDecl *findInstantiationOf(ASTContext &Ctx,
801 NamedDecl *D,
802 ForwardIterator first,
803 ForwardIterator last) {
804 for (; first != last; ++first)
805 if (isInstantiationOf(Ctx, D, *first))
806 return cast<NamedDecl>(*first);
807
808 return 0;
809}
810
Douglas Gregored961e72009-05-27 17:54:46 +0000811/// \brief Find the instantiation of the given declaration within the
812/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +0000813///
814/// This routine is intended to be used when \p D is a declaration
815/// referenced from within a template, that needs to mapped into the
816/// corresponding declaration within an instantiation. For example,
817/// given:
818///
819/// \code
820/// template<typename T>
821/// struct X {
822/// enum Kind {
823/// KnownValue = sizeof(T)
824/// };
825///
826/// bool getKind() const { return KnownValue; }
827/// };
828///
829/// template struct X<int>;
830/// \endcode
831///
832/// In the instantiation of X<int>::getKind(), we need to map the
833/// EnumConstantDecl for KnownValue (which refers to
834/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +0000835/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
836/// this mapping from within the instantiation of X<int>.
837NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
Douglas Gregor815215d2009-05-27 05:35:12 +0000838 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000839 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
840 // D is a local of some kind. Look into the map of local
841 // declarations to their instantiations.
842 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
843 }
Douglas Gregor815215d2009-05-27 05:35:12 +0000844
Douglas Gregor2bba76b2009-05-27 17:07:49 +0000845 if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
Douglas Gregored961e72009-05-27 17:54:46 +0000846 ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
Douglas Gregor815215d2009-05-27 05:35:12 +0000847 if (!ParentDecl)
848 return 0;
849
850 ParentDC = cast<DeclContext>(ParentDecl);
851 }
852
Douglas Gregor815215d2009-05-27 05:35:12 +0000853 if (ParentDC != D->getDeclContext()) {
854 // We performed some kind of instantiation in the parent context,
855 // so now we need to look into the instantiated parent context to
856 // find the instantiation of the declaration D.
857 NamedDecl *Result = 0;
858 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000859 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +0000860 Result = findInstantiationOf(Context, D, Found.first, Found.second);
861 } else {
862 // Since we don't have a name for the entity we're looking for,
863 // our only option is to walk through all of the declarations to
864 // find that name. This will occur in a few cases:
865 //
866 // - anonymous struct/union within a template
867 // - unnamed class/struct/union/enum within a template
868 //
869 // FIXME: Find a better way to find these instantiations!
870 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000871 ParentDC->decls_begin(),
872 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +0000873 }
874 assert(Result && "Unable to find instantiation of declaration!");
875 D = Result;
876 }
877
Douglas Gregor815215d2009-05-27 05:35:12 +0000878 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregored961e72009-05-27 17:54:46 +0000879 if (ClassTemplateDecl *ClassTemplate
880 = Record->getDescribedClassTemplate()) {
881 // When the declaration D was parsed, it referred to the current
882 // instantiation. Therefore, look through the current context,
883 // which contains actual instantiations, to find the
884 // instantiation of the "current instantiation" that D refers
885 // to. Alternatively, we could just instantiate the
886 // injected-class-name with the current template arguments, but
887 // such an instantiation is far more expensive.
888 for (DeclContext *DC = CurContext; !DC->isFileContext();
889 DC = DC->getParent()) {
890 if (ClassTemplateSpecializationDecl *Spec
891 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
892 if (Context.getCanonicalDecl(Spec->getSpecializedTemplate())
893 == Context.getCanonicalDecl(ClassTemplate))
894 return Spec;
895 }
896
897 assert(false &&
898 "Unable to find declaration for the current instantiation");
Douglas Gregor815215d2009-05-27 05:35:12 +0000899 }
900
901 return D;
902}
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000903
904/// \brief Performs template instantiation for all implicit template
905/// instantiations we have seen until this point.
906void Sema::PerformPendingImplicitInstantiations() {
907 while (!PendingImplicitInstantiations.empty()) {
908 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000909 PendingImplicitInstantiations.pop_front();
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000910
911 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first))
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000912 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +0000913 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000914
Douglas Gregor751f9a42009-06-30 15:47:41 +0000915 // FIXME: instantiate static member variables
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000916 }
917}