blob: 1f5856858042a57a34620a59cecc280a4b8e0970 [file] [log] [blame]
Douglas Gregor1b39ef42009-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 Gregordc18e892009-05-26 20:50:29 +000013#include "clang/AST/ASTConsumer.h"
Douglas Gregor1b39ef42009-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 Lattner5261d0c2009-03-28 19:18:32 +000024 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor1b39ef42009-03-17 21:15:40 +000025 Sema &SemaRef;
26 DeclContext *Owner;
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +000027 const TemplateArgumentList &TemplateArgs;
Douglas Gregor1b39ef42009-03-17 21:15:40 +000028
29 public:
30 typedef Sema::OwningExprResult OwningExprResult;
31
32 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +000033 const TemplateArgumentList &TemplateArgs)
34 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Douglas Gregor1b39ef42009-03-17 21:15:40 +000035
Mike Stumpe127ae32009-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 Gregor1818a0a2009-03-25 15:45:12 +000039 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
40 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor1b39ef42009-03-17 21:15:40 +000041 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorafb094a2009-03-25 23:32:15 +000042 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor1b39ef42009-03-17 21:15:40 +000043 Decl *VisitFieldDecl(FieldDecl *D);
44 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
45 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor7e96f802009-03-25 15:04:13 +000046 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorb60eb752009-06-25 22:08:12 +000047 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorcc887972009-03-25 21:17:03 +000048 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregora6897272009-03-23 23:06:20 +000049 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
Douglas Gregorad7d1812009-03-24 16:43:20 +000050 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor467b1c92009-03-24 00:15:49 +000051 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregora5dfe702009-03-25 00:34:44 +000052 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor7e96f802009-03-25 15:04:13 +000053 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregora6897272009-03-23 23:06:20 +000054 Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor03132d92009-03-24 00:38:23 +000055
Douglas Gregor1b39ef42009-03-17 21:15:40 +000056 // Base case. FIXME: Remove once we can instantiate everything.
57 Decl *VisitDecl(Decl *) {
Douglas Gregorafb094a2009-03-25 23:32:15 +000058 assert(false && "Template instantiation of unknown declaration kind!");
Douglas Gregor1b39ef42009-03-17 21:15:40 +000059 return 0;
60 }
Douglas Gregor03132d92009-03-24 00:38:23 +000061
62 // Helper functions for instantiating methods.
63 QualType InstantiateFunctionType(FunctionDecl *D,
64 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregorb60eb752009-06-25 22:08:12 +000065 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor03132d92009-03-24 00:38:23 +000066 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
Douglas Gregor1b39ef42009-03-17 21:15:40 +000067 };
68}
69
Douglas Gregor1818a0a2009-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 Gregor1b39ef42009-03-17 21:15:40 +000082Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
83 bool Invalid = false;
84 QualType T = D->getUnderlyingType();
85 if (T->isDependentType()) {
Douglas Gregor260059c2009-05-14 21:06:31 +000086 T = SemaRef.InstantiateType(T, TemplateArgs,
87 D->getLocation(), D->getDeclName());
Douglas Gregor1b39ef42009-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
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000101 Owner->addDecl(Typedef);
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000102
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000103 return Typedef;
104}
105
Douglas Gregorafb094a2009-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 Gregorafb094a2009-03-25 23:32:15 +0000109 D->getTypeSpecStartLoc(),
110 D->getDeclName());
111 if (T.isNull())
112 return 0;
113
Douglas Gregorb06585a2009-05-15 00:01:03 +0000114 // Build the instantiated declaration
Douglas Gregorafb094a2009-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 Stumpe127ae32009-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 Gregorafb094a2009-03-25 23:32:15 +0000125 bool Redeclaration = false;
Chris Lattner34c61332009-04-25 08:06:05 +0000126 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000127 Owner->addDecl(Var);
Douglas Gregorafb094a2009-03-25 23:32:15 +0000128
129 if (D->getInit()) {
130 OwningExprResult Init
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000131 = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs);
Douglas Gregorafb094a2009-03-25 23:32:15 +0000132 if (Init.isInvalid())
133 Var->setInvalidDecl();
134 else
Chris Lattner5261d0c2009-03-28 19:18:32 +0000135 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregorafb094a2009-03-25 23:32:15 +0000136 D->hasCXXDirectInitializer());
Douglas Gregor57420b42009-05-18 20:51:54 +0000137 } else {
138 // FIXME: Call ActOnUninitializedDecl? (Not always)
Douglas Gregorafb094a2009-03-25 23:32:15 +0000139 }
140
141 return Var;
142}
143
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000144Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
145 bool Invalid = false;
146 QualType T = D->getType();
147 if (T->isDependentType()) {
Douglas Gregor260059c2009-05-14 21:06:31 +0000148 T = SemaRef.InstantiateType(T, TemplateArgs,
149 D->getLocation(), D->getDeclName());
Douglas Gregor1b39ef42009-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 Gregora8b2fbf2009-06-22 20:57:11 +0000168 // The bit-width expression is not potentially evaluated.
169 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
170
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000171 OwningExprResult InstantiatedBitWidth
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000172 = SemaRef.InstantiateExpr(BitWidth, TemplateArgs);
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000173 if (InstantiatedBitWidth.isInvalid()) {
174 Invalid = true;
175 BitWidth = 0;
176 } else
Anders Carlsson39ecdcf2009-05-01 19:49:17 +0000177 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor1b39ef42009-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,
Steve Naroffb79574e2009-07-14 14:58:18 +0000185 D->getTypeSpecStartLoc(),
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000186 D->getAccess(),
187 0);
188 if (Field) {
189 if (Invalid)
190 Field->setInvalidDecl();
191
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000192 Owner->addDecl(Field);
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000193 }
194
195 return Field;
196}
197
198Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
199 Expr *AssertExpr = D->getAssertExpr();
200
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000201 // The expression in a static assertion is not potentially evaluated.
202 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
203
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000204 OwningExprResult InstantiatedAssertExpr
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000205 = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs);
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000206 if (InstantiatedAssertExpr.isInvalid())
207 return 0;
208
209 OwningExprResult Message = SemaRef.Clone(D->getMessage());
210 Decl *StaticAssert
Chris Lattner5261d0c2009-03-28 19:18:32 +0000211 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
212 move(InstantiatedAssertExpr),
213 move(Message)).getAs<Decl>();
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000214 return StaticAssert;
215}
216
217Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
218 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
219 D->getLocation(), D->getIdentifier(),
220 /*PrevDecl=*/0);
Douglas Gregor8d717432009-05-27 17:20:35 +0000221 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000222 Enum->setAccess(D->getAccess());
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000223 Owner->addDecl(Enum);
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000224 Enum->startDefinition();
225
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000226 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000227
228 EnumConstantDecl *LastEnumConst = 0;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000229 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
230 ECEnd = D->enumerator_end();
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000231 EC != ECEnd; ++EC) {
232 // The specified value for the enumerator.
233 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000234 if (Expr *UninstValue = EC->getInitExpr()) {
235 // The enumerator's value expression is not potentially evaluated.
236 EnterExpressionEvaluationContext Unevaluated(SemaRef,
237 Action::Unevaluated);
238
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000239 Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs);
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000240 }
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000241
242 // Drop the initial value and continue.
243 bool isInvalid = false;
244 if (Value.isInvalid()) {
245 Value = SemaRef.Owned((Expr *)0);
246 isInvalid = true;
247 }
248
249 EnumConstantDecl *EnumConst
250 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
251 EC->getLocation(), EC->getIdentifier(),
252 move(Value));
253
254 if (isInvalid) {
255 if (EnumConst)
256 EnumConst->setInvalidDecl();
257 Enum->setInvalidDecl();
258 }
259
260 if (EnumConst) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000261 Enum->addDecl(EnumConst);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000262 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000263 LastEnumConst = EnumConst;
264 }
265 }
266
Mike Stump155750e2009-05-16 07:06:02 +0000267 // FIXME: Fixup LBraceLoc and RBraceLoc
268 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
269 Sema::DeclPtrTy::make(Enum),
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000270 &Enumerators[0], Enumerators.size());
271
272 return Enum;
273}
274
Douglas Gregor7e96f802009-03-25 15:04:13 +0000275Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
276 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
277 return 0;
278}
279
Douglas Gregorcc887972009-03-25 21:17:03 +0000280Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
281 CXXRecordDecl *PrevDecl = 0;
282 if (D->isInjectedClassName())
283 PrevDecl = cast<CXXRecordDecl>(Owner);
284
285 CXXRecordDecl *Record
286 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
287 D->getLocation(), D->getIdentifier(), PrevDecl);
288 Record->setImplicit(D->isImplicit());
289 Record->setAccess(D->getAccess());
Douglas Gregorcc887972009-03-25 21:17:03 +0000290 if (!D->isInjectedClassName())
291 Record->setInstantiationOfMemberClass(D);
292
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000293 Owner->addDecl(Record);
Douglas Gregorcc887972009-03-25 21:17:03 +0000294 return Record;
295}
296
Douglas Gregorb60eb752009-06-25 22:08:12 +0000297Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000298 // Check whether there is already a function template specialization for
299 // this declaration.
300 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
301 void *InsertPos = 0;
302 if (FunctionTemplate) {
303 llvm::FoldingSetNodeID ID;
304 FunctionTemplateSpecializationInfo::Profile(ID,
305 TemplateArgs.getFlatArgumentList(),
306 TemplateArgs.flat_size());
307
308 FunctionTemplateSpecializationInfo *Info
309 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
310 InsertPos);
311
312 // If we already have a function template specialization, return it.
313 if (Info)
314 return Info->Function;
315 }
Douglas Gregorb60eb752009-06-25 22:08:12 +0000316
317 Sema::LocalInstantiationScope Scope(SemaRef);
318
319 llvm::SmallVector<ParmVarDecl *, 4> Params;
320 QualType T = InstantiateFunctionType(D, Params);
321 if (T.isNull())
Douglas Gregora6897272009-03-23 23:06:20 +0000322 return 0;
Douglas Gregorb60eb752009-06-25 22:08:12 +0000323
324 // Build the instantiated method declaration.
325 FunctionDecl *Function
326 = FunctionDecl::Create(SemaRef.Context, Owner, D->getLocation(),
327 D->getDeclName(), T, D->getStorageClass(),
328 D->isInline(), D->hasWrittenPrototype(),
329 D->getTypeSpecStartLoc());
330
331 // FIXME: friend functions
332
333 // Attach the parameters
334 for (unsigned P = 0; P < Params.size(); ++P)
335 Params[P]->setOwningFunction(Function);
336 Function->setParams(SemaRef.Context, Params.data(), Params.size());
337
338 if (InitFunctionInstantiation(Function, D))
339 Function->setInvalidDecl();
340
341 bool Redeclaration = false;
342 bool OverloadableAttrRequired = false;
343 NamedDecl *PrevDecl = 0;
344 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
345 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregora6897272009-03-23 23:06:20 +0000346
Douglas Gregor27b6d2b2009-06-29 20:59:39 +0000347 if (FunctionTemplate) {
348 // Record this function template specialization.
349 Function->setFunctionTemplateSpecialization(SemaRef.Context,
350 FunctionTemplate,
351 &TemplateArgs,
352 InsertPos);
353 }
354
Douglas Gregorb60eb752009-06-25 22:08:12 +0000355 return Function;
356}
357
358Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
359 // FIXME: Look for existing, explicit specializations.
Douglas Gregorf036aa72009-05-14 21:44:34 +0000360 Sema::LocalInstantiationScope Scope(SemaRef);
361
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000362 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor03132d92009-03-24 00:38:23 +0000363 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregora6897272009-03-23 23:06:20 +0000364 if (T.isNull())
365 return 0;
366
367 // Build the instantiated method declaration.
368 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
369 CXXMethodDecl *Method
370 = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
371 D->getDeclName(), T, D->isStatic(),
372 D->isInline());
Douglas Gregor260059c2009-05-14 21:06:31 +0000373 Method->setInstantiationOfMemberFunction(D);
Douglas Gregora6897272009-03-23 23:06:20 +0000374
Douglas Gregor03132d92009-03-24 00:38:23 +0000375 // Attach the parameters
376 for (unsigned P = 0; P < Params.size(); ++P)
377 Params[P]->setOwningFunction(Method);
Jay Foad9e6bef42009-05-21 09:52:38 +0000378 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor03132d92009-03-24 00:38:23 +0000379
380 if (InitMethodInstantiation(Method, D))
381 Method->setInvalidDecl();
Douglas Gregora6897272009-03-23 23:06:20 +0000382
383 NamedDecl *PrevDecl
384 = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(),
385 Sema::LookupOrdinaryName, true);
386 // In C++, the previous declaration we find might be a tag type
387 // (class or enum). In this case, the new declaration will hide the
388 // tag type. Note that this does does not apply if we're declaring a
389 // typedef (C++ [dcl.typedef]p4).
390 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
391 PrevDecl = 0;
392 bool Redeclaration = false;
393 bool OverloadableAttrRequired = false;
Chris Lattner34c61332009-04-25 08:06:05 +0000394 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
395 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregora6897272009-03-23 23:06:20 +0000396
397 if (!Method->isInvalidDecl() || !PrevDecl)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000398 Owner->addDecl(Method);
Douglas Gregora6897272009-03-23 23:06:20 +0000399 return Method;
400}
401
Douglas Gregorad7d1812009-03-24 16:43:20 +0000402Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregorb60eb752009-06-25 22:08:12 +0000403 // FIXME: Look for existing, explicit specializations.
Douglas Gregorf036aa72009-05-14 21:44:34 +0000404 Sema::LocalInstantiationScope Scope(SemaRef);
405
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000406 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregorad7d1812009-03-24 16:43:20 +0000407 QualType T = InstantiateFunctionType(D, Params);
408 if (T.isNull())
409 return 0;
410
411 // Build the instantiated method declaration.
412 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
413 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
414 DeclarationName Name
Douglas Gregor4850df62009-05-15 21:18:27 +0000415 = SemaRef.Context.DeclarationNames.getCXXConstructorName(
416 SemaRef.Context.getCanonicalType(ClassTy));
Douglas Gregorad7d1812009-03-24 16:43:20 +0000417 CXXConstructorDecl *Constructor
418 = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(),
419 Name, T, D->isExplicit(), D->isInline(),
420 false);
Douglas Gregor260059c2009-05-14 21:06:31 +0000421 Constructor->setInstantiationOfMemberFunction(D);
Douglas Gregorad7d1812009-03-24 16:43:20 +0000422
423 // Attach the parameters
424 for (unsigned P = 0; P < Params.size(); ++P)
425 Params[P]->setOwningFunction(Constructor);
Jay Foad9e6bef42009-05-21 09:52:38 +0000426 Constructor->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregorad7d1812009-03-24 16:43:20 +0000427
428 if (InitMethodInstantiation(Constructor, D))
429 Constructor->setInvalidDecl();
430
431 NamedDecl *PrevDecl
432 = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true);
433
434 // In C++, the previous declaration we find might be a tag type
435 // (class or enum). In this case, the new declaration will hide the
436 // tag type. Note that this does does not apply if we're declaring a
437 // typedef (C++ [dcl.typedef]p4).
438 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
439 PrevDecl = 0;
440 bool Redeclaration = false;
441 bool OverloadableAttrRequired = false;
Chris Lattner34c61332009-04-25 08:06:05 +0000442 SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration,
443 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregorad7d1812009-03-24 16:43:20 +0000444
Douglas Gregor4850df62009-05-15 21:18:27 +0000445 Record->addedConstructor(SemaRef.Context, Constructor);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000446 Owner->addDecl(Constructor);
Douglas Gregorad7d1812009-03-24 16:43:20 +0000447 return Constructor;
448}
449
Douglas Gregor467b1c92009-03-24 00:15:49 +0000450Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregorb60eb752009-06-25 22:08:12 +0000451 // FIXME: Look for existing, explicit specializations.
Douglas Gregorf036aa72009-05-14 21:44:34 +0000452 Sema::LocalInstantiationScope Scope(SemaRef);
453
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000454 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregor03132d92009-03-24 00:38:23 +0000455 QualType T = InstantiateFunctionType(D, Params);
Douglas Gregor467b1c92009-03-24 00:15:49 +0000456 if (T.isNull())
457 return 0;
Douglas Gregor03132d92009-03-24 00:38:23 +0000458 assert(Params.size() == 0 && "Destructor with parameters?");
459
Douglas Gregor467b1c92009-03-24 00:15:49 +0000460 // Build the instantiated destructor declaration.
461 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregor4850df62009-05-15 21:18:27 +0000462 QualType ClassTy =
463 SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record));
Douglas Gregor467b1c92009-03-24 00:15:49 +0000464 CXXDestructorDecl *Destructor
465 = CXXDestructorDecl::Create(SemaRef.Context, Record,
466 D->getLocation(),
467 SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy),
468 T, D->isInline(), false);
Douglas Gregor260059c2009-05-14 21:06:31 +0000469 Destructor->setInstantiationOfMemberFunction(D);
Douglas Gregor03132d92009-03-24 00:38:23 +0000470 if (InitMethodInstantiation(Destructor, D))
471 Destructor->setInvalidDecl();
Douglas Gregor467b1c92009-03-24 00:15:49 +0000472
473 bool Redeclaration = false;
474 bool OverloadableAttrRequired = false;
475 NamedDecl *PrevDecl = 0;
Chris Lattner34c61332009-04-25 08:06:05 +0000476 SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
477 /*FIXME:*/OverloadableAttrRequired);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000478 Owner->addDecl(Destructor);
Douglas Gregor467b1c92009-03-24 00:15:49 +0000479 return Destructor;
480}
481
Douglas Gregora5dfe702009-03-25 00:34:44 +0000482Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregorb60eb752009-06-25 22:08:12 +0000483 // FIXME: Look for existing, explicit specializations.
Douglas Gregorf036aa72009-05-14 21:44:34 +0000484 Sema::LocalInstantiationScope Scope(SemaRef);
485
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000486 llvm::SmallVector<ParmVarDecl *, 4> Params;
Douglas Gregora5dfe702009-03-25 00:34:44 +0000487 QualType T = InstantiateFunctionType(D, Params);
488 if (T.isNull())
489 return 0;
490 assert(Params.size() == 0 && "Destructor with parameters?");
491
492 // Build the instantiated conversion declaration.
493 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
494 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
495 QualType ConvTy
496 = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType());
497 CXXConversionDecl *Conversion
498 = CXXConversionDecl::Create(SemaRef.Context, Record,
499 D->getLocation(),
500 SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy),
501 T, D->isInline(), D->isExplicit());
Douglas Gregor260059c2009-05-14 21:06:31 +0000502 Conversion->setInstantiationOfMemberFunction(D);
Douglas Gregora5dfe702009-03-25 00:34:44 +0000503 if (InitMethodInstantiation(Conversion, D))
504 Conversion->setInvalidDecl();
505
506 bool Redeclaration = false;
507 bool OverloadableAttrRequired = false;
508 NamedDecl *PrevDecl = 0;
Chris Lattner34c61332009-04-25 08:06:05 +0000509 SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
510 /*FIXME:*/OverloadableAttrRequired);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000511 Owner->addDecl(Conversion);
Douglas Gregora5dfe702009-03-25 00:34:44 +0000512 return Conversion;
513}
514
Douglas Gregor7e96f802009-03-25 15:04:13 +0000515ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregora6897272009-03-23 23:06:20 +0000516 QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000517 D->getLocation(), D->getDeclName());
Douglas Gregora6897272009-03-23 23:06:20 +0000518 if (OrigT.isNull())
519 return 0;
520
521 QualType T = SemaRef.adjustParameterType(OrigT);
522
523 if (D->getDefaultArg()) {
524 // FIXME: Leave a marker for "uninstantiated" default
525 // arguments. They only get instantiated on demand at the call
526 // site.
527 unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning,
528 "sorry, dropping default argument during template instantiation");
529 SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID)
530 << D->getDefaultArg()->getSourceRange();
531 }
532
533 // Allocate the parameter
534 ParmVarDecl *Param = 0;
535 if (T == OrigT)
536 Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
537 D->getIdentifier(), T, D->getStorageClass(),
538 0);
539 else
540 Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
541 D->getLocation(), D->getIdentifier(),
542 T, OrigT, D->getStorageClass(), 0);
543
544 // Note: we don't try to instantiate function parameters until after
545 // we've instantiated the function's type. Therefore, we don't have
546 // to check for 'void' parameter types here.
Douglas Gregorf036aa72009-05-14 21:44:34 +0000547 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregora6897272009-03-23 23:06:20 +0000548 return Param;
549}
550
551Decl *
552TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
553 // Since parameter types can decay either before or after
554 // instantiation, we simply treat OriginalParmVarDecls as
555 // ParmVarDecls the same way, and create one or the other depending
556 // on what happens after template instantiation.
557 return VisitParmVarDecl(D);
558}
559
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000560Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000561 const TemplateArgumentList &TemplateArgs) {
562 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000563 return Instantiator.Visit(D);
564}
565
Douglas Gregor03132d92009-03-24 00:38:23 +0000566/// \brief Instantiates the type of the given function, including
567/// instantiating all of the function parameters.
568///
569/// \param D The function that we will be instantiated
570///
571/// \param Params the instantiated parameter declarations
572
573/// \returns the instantiated function's type if successfull, a NULL
574/// type if there was an error.
575QualType
576TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D,
577 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
578 bool InvalidDecl = false;
579
580 // Instantiate the function parameters
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000581 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000582 llvm::SmallVector<QualType, 4> ParamTys;
Douglas Gregor03132d92009-03-24 00:38:23 +0000583 for (FunctionDecl::param_iterator P = D->param_begin(),
584 PEnd = D->param_end();
585 P != PEnd; ++P) {
Douglas Gregor7e96f802009-03-25 15:04:13 +0000586 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor03132d92009-03-24 00:38:23 +0000587 if (PInst->getType()->isVoidType()) {
588 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
589 PInst->setInvalidDecl();
590 }
591 else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
592 PInst->getType(),
593 diag::err_abstract_type_in_decl,
Anders Carlsson412c3402009-03-24 01:19:16 +0000594 Sema::AbstractParamType))
Douglas Gregor03132d92009-03-24 00:38:23 +0000595 PInst->setInvalidDecl();
596
597 Params.push_back(PInst);
598 ParamTys.push_back(PInst->getType());
599
600 if (PInst->isInvalidDecl())
601 InvalidDecl = true;
602 } else
603 InvalidDecl = true;
604 }
605
606 // FIXME: Deallocate dead declarations.
607 if (InvalidDecl)
608 return QualType();
609
610 const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
611 assert(Proto && "Missing prototype?");
612 QualType ResultType
Douglas Gregor260059c2009-05-14 21:06:31 +0000613 = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs,
Douglas Gregor03132d92009-03-24 00:38:23 +0000614 D->getLocation(), D->getDeclName());
615 if (ResultType.isNull())
616 return QualType();
617
Jay Foad9e6bef42009-05-21 09:52:38 +0000618 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor03132d92009-03-24 00:38:23 +0000619 Proto->isVariadic(), Proto->getTypeQuals(),
620 D->getLocation(), D->getDeclName());
621}
622
Douglas Gregorb60eb752009-06-25 22:08:12 +0000623/// \brief Initializes the common fields of an instantiation function
624/// declaration (New) from the corresponding fields of its template (Tmpl).
625///
626/// \returns true if there was an error
627bool
628TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
629 FunctionDecl *Tmpl) {
630 if (Tmpl->isDeleted())
631 New->setDeleted();
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000632
633 // If we are performing substituting explicitly-specified template arguments
634 // or deduced template arguments into a function template and we reach this
635 // point, we are now past the point where SFINAE applies and have committed
636 // to keeping the new function template specialization. We therefore
637 // convert the active template instantiation for the function template
638 // into a template instantiation for this specific function template
639 // specialization, which is not a SFINAE context, so that we diagnose any
640 // further errors in the declaration itself.
641 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
642 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
643 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
644 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
645 if (FunctionTemplateDecl *FunTmpl
646 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
647 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
648 "Deduction from the wrong function template?");
649 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
650 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
651 }
652 }
653
Douglas Gregorb60eb752009-06-25 22:08:12 +0000654 return false;
655}
656
Douglas Gregor03132d92009-03-24 00:38:23 +0000657/// \brief Initializes common fields of an instantiated method
658/// declaration (New) from the corresponding fields of its template
659/// (Tmpl).
660///
661/// \returns true if there was an error
662bool
663TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
664 CXXMethodDecl *Tmpl) {
Douglas Gregorb60eb752009-06-25 22:08:12 +0000665 if (InitFunctionInstantiation(New, Tmpl))
666 return true;
667
Douglas Gregor03132d92009-03-24 00:38:23 +0000668 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
669 New->setAccess(Tmpl->getAccess());
Anders Carlssonf9855602009-05-14 22:15:41 +0000670 if (Tmpl->isVirtualAsWritten()) {
671 New->setVirtualAsWritten(true);
Douglas Gregor03132d92009-03-24 00:38:23 +0000672 Record->setAggregate(false);
673 Record->setPOD(false);
674 Record->setPolymorphic(true);
675 }
Douglas Gregor03132d92009-03-24 00:38:23 +0000676 if (Tmpl->isPure()) {
677 New->setPure();
678 Record->setAbstract(true);
679 }
680
681 // FIXME: attributes
682 // FIXME: New needs a pointer to Tmpl
683 return false;
684}
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000685
686/// \brief Instantiate the definition of the given function from its
687/// template.
688///
Douglas Gregordcdb3842009-06-30 17:20:14 +0000689/// \param PointOfInstantiation the point at which the instantiation was
690/// required. Note that this is not precisely a "point of instantiation"
691/// for the function, but it's close.
692///
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000693/// \param Function the already-instantiated declaration of a
Douglas Gregordcdb3842009-06-30 17:20:14 +0000694/// function template specialization or member function of a class template
695/// specialization.
696///
697/// \param Recursive if true, recursively instantiates any functions that
698/// are required by this instantiation.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000699void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregordcdb3842009-06-30 17:20:14 +0000700 FunctionDecl *Function,
701 bool Recursive) {
Douglas Gregor5f62c5e2009-05-14 23:26:13 +0000702 if (Function->isInvalidDecl())
703 return;
704
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000705 assert(!Function->getBody() && "Already instantiated!");
Douglas Gregorcad27f62009-06-22 23:06:13 +0000706
Douglas Gregor260059c2009-05-14 21:06:31 +0000707 // Find the function body that we'll be substituting.
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000708 const FunctionDecl *PatternDecl = 0;
709 if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate())
710 PatternDecl = Primary->getTemplatedDecl();
711 else
712 PatternDecl = Function->getInstantiatedFromMemberFunction();
Douglas Gregor260059c2009-05-14 21:06:31 +0000713 Stmt *Pattern = 0;
714 if (PatternDecl)
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000715 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor260059c2009-05-14 21:06:31 +0000716
717 if (!Pattern)
718 return;
719
Douglas Gregorb12249d2009-05-18 17:01:57 +0000720 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
721 if (Inst)
722 return;
Douglas Gregorb06585a2009-05-15 00:01:03 +0000723
Douglas Gregordcdb3842009-06-30 17:20:14 +0000724 // If we're performing recursive template instantiation, create our own
725 // queue of pending implicit instantiations that we will instantiate later,
726 // while we're still within our own instantiation context.
727 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
728 if (Recursive)
729 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
730
Douglas Gregor556d8c72009-05-15 17:59:04 +0000731 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
732
Douglas Gregor5f62c5e2009-05-14 23:26:13 +0000733 // Introduce a new scope where local variable instantiations will be
734 // recorded.
735 LocalInstantiationScope Scope(*this);
736
737 // Introduce the instantiated function parameters into the local
738 // instantiation scope.
739 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
740 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
741 Function->getParamDecl(I));
742
Douglas Gregorb06585a2009-05-15 00:01:03 +0000743 // Enter the scope of this instantiation. We don't use
744 // PushDeclContext because we don't have a scope.
745 DeclContext *PreviousContext = CurContext;
746 CurContext = Function;
747
Douglas Gregor5f62c5e2009-05-14 23:26:13 +0000748 // Instantiate the function body.
749 OwningStmtResult Body
750 = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function));
Douglas Gregor556d8c72009-05-15 17:59:04 +0000751
752 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
753 /*IsInstantiation=*/true);
Douglas Gregorb06585a2009-05-15 00:01:03 +0000754
755 CurContext = PreviousContext;
Douglas Gregordc18e892009-05-26 20:50:29 +0000756
757 DeclGroupRef DG(Function);
758 Consumer.HandleTopLevelDecl(DG);
Douglas Gregordcdb3842009-06-30 17:20:14 +0000759
760 if (Recursive) {
761 // Instantiate any pending implicit instantiations found during the
762 // instantiation of this template.
763 PerformPendingImplicitInstantiations();
764
765 // Restore the set of pending implicit instantiations.
766 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
767 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000768}
769
770/// \brief Instantiate the definition of the given variable from its
771/// template.
772///
773/// \param Var the already-instantiated declaration of a variable.
774void Sema::InstantiateVariableDefinition(VarDecl *Var) {
775 // FIXME: Implement this!
776}
Douglas Gregorf97986d2009-05-27 05:35:12 +0000777
778static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
779 if (D->getKind() != Other->getKind())
780 return false;
781
782 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
783 return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass())
784 == Ctx.getCanonicalDecl(D);
785
786 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
787 return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction())
788 == Ctx.getCanonicalDecl(D);
789
Douglas Gregor8d717432009-05-27 17:20:35 +0000790 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
791 return Ctx.getCanonicalDecl(Enum->getInstantiatedFromMemberEnum())
792 == Ctx.getCanonicalDecl(D);
Douglas Gregorf97986d2009-05-27 05:35:12 +0000793
794 // FIXME: How can we find instantiations of anonymous unions?
795
796 return D->getDeclName() && isa<NamedDecl>(Other) &&
797 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
798}
799
800template<typename ForwardIterator>
801static NamedDecl *findInstantiationOf(ASTContext &Ctx,
802 NamedDecl *D,
803 ForwardIterator first,
804 ForwardIterator last) {
805 for (; first != last; ++first)
806 if (isInstantiationOf(Ctx, D, *first))
807 return cast<NamedDecl>(*first);
808
809 return 0;
810}
811
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000812/// \brief Find the instantiation of the given declaration within the
813/// current instantiation.
Douglas Gregorf97986d2009-05-27 05:35:12 +0000814///
815/// This routine is intended to be used when \p D is a declaration
816/// referenced from within a template, that needs to mapped into the
817/// corresponding declaration within an instantiation. For example,
818/// given:
819///
820/// \code
821/// template<typename T>
822/// struct X {
823/// enum Kind {
824/// KnownValue = sizeof(T)
825/// };
826///
827/// bool getKind() const { return KnownValue; }
828/// };
829///
830/// template struct X<int>;
831/// \endcode
832///
833/// In the instantiation of X<int>::getKind(), we need to map the
834/// EnumConstantDecl for KnownValue (which refers to
835/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000836/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
837/// this mapping from within the instantiation of X<int>.
838NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000839 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregore919fb02009-05-27 17:07:49 +0000840 if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
841 // D is a local of some kind. Look into the map of local
842 // declarations to their instantiations.
843 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
844 }
Douglas Gregorf97986d2009-05-27 05:35:12 +0000845
Douglas Gregore919fb02009-05-27 17:07:49 +0000846 if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000847 ParentDecl = InstantiateCurrentDeclRef(ParentDecl);
Douglas Gregorf97986d2009-05-27 05:35:12 +0000848 if (!ParentDecl)
849 return 0;
850
851 ParentDC = cast<DeclContext>(ParentDecl);
852 }
853
Douglas Gregorf97986d2009-05-27 05:35:12 +0000854 if (ParentDC != D->getDeclContext()) {
855 // We performed some kind of instantiation in the parent context,
856 // so now we need to look into the instantiated parent context to
857 // find the instantiation of the declaration D.
858 NamedDecl *Result = 0;
859 if (D->getDeclName()) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000860 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregorf97986d2009-05-27 05:35:12 +0000861 Result = findInstantiationOf(Context, D, Found.first, Found.second);
862 } else {
863 // Since we don't have a name for the entity we're looking for,
864 // our only option is to walk through all of the declarations to
865 // find that name. This will occur in a few cases:
866 //
867 // - anonymous struct/union within a template
868 // - unnamed class/struct/union/enum within a template
869 //
870 // FIXME: Find a better way to find these instantiations!
871 Result = findInstantiationOf(Context, D,
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000872 ParentDC->decls_begin(),
873 ParentDC->decls_end());
Douglas Gregorf97986d2009-05-27 05:35:12 +0000874 }
875 assert(Result && "Unable to find instantiation of declaration!");
876 D = Result;
877 }
878
Douglas Gregorf97986d2009-05-27 05:35:12 +0000879 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000880 if (ClassTemplateDecl *ClassTemplate
881 = Record->getDescribedClassTemplate()) {
882 // When the declaration D was parsed, it referred to the current
883 // instantiation. Therefore, look through the current context,
884 // which contains actual instantiations, to find the
885 // instantiation of the "current instantiation" that D refers
886 // to. Alternatively, we could just instantiate the
887 // injected-class-name with the current template arguments, but
888 // such an instantiation is far more expensive.
889 for (DeclContext *DC = CurContext; !DC->isFileContext();
890 DC = DC->getParent()) {
891 if (ClassTemplateSpecializationDecl *Spec
892 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
893 if (Context.getCanonicalDecl(Spec->getSpecializedTemplate())
894 == Context.getCanonicalDecl(ClassTemplate))
895 return Spec;
896 }
897
898 assert(false &&
899 "Unable to find declaration for the current instantiation");
Douglas Gregorf97986d2009-05-27 05:35:12 +0000900 }
901
902 return D;
903}
Douglas Gregorcad27f62009-06-22 23:06:13 +0000904
905/// \brief Performs template instantiation for all implicit template
906/// instantiations we have seen until this point.
907void Sema::PerformPendingImplicitInstantiations() {
908 while (!PendingImplicitInstantiations.empty()) {
909 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregordcdb3842009-06-30 17:20:14 +0000910 PendingImplicitInstantiations.pop_front();
Douglas Gregorcad27f62009-06-22 23:06:13 +0000911
912 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first))
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000913 if (!Function->getBody())
Douglas Gregordcdb3842009-06-30 17:20:14 +0000914 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregorcad27f62009-06-22 23:06:13 +0000915
Douglas Gregor6ef403d2009-06-30 15:47:41 +0000916 // FIXME: instantiate static member variables
Douglas Gregorcad27f62009-06-22 23:06:13 +0000917 }
918}