blob: 24b83704eba9f30489d4bc9e4799923a060216ee [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template 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.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregor17c0d7b2009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregor4ea568f2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor01afeef2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregora654dd82009-08-28 17:37:35 +000031MultiLevelTemplateArgumentList
Douglas Gregorb4850462009-05-14 23:26:13 +000032Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregora654dd82009-08-28 17:37:35 +000033 // Accumulate the set of template argument lists in this structure.
34 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000035
Douglas Gregora654dd82009-08-28 17:37:35 +000036 DeclContext *Ctx = dyn_cast<DeclContext>(D);
37 if (!Ctx)
38 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000039
John McCall970d5302009-08-29 03:16:09 +000040 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000041 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000042 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000043 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
44 // We're done when we hit an explicit specialization.
45 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
46 break;
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregora654dd82009-08-28 17:37:35 +000048 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000049
50 // If this class template specialization was instantiated from a
51 // specialized member that is a class template, we're done.
52 assert(Spec->getSpecializedTemplate() && "No class template?");
53 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
54 break;
Mike Stump11289f42009-09-09 15:08:12 +000055 }
Douglas Gregora654dd82009-08-28 17:37:35 +000056 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000057 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorcf915552009-10-13 16:30:37 +000058 if (Function->getTemplateSpecializationKind()
59 == TSK_ExplicitSpecialization)
60 break;
61
Douglas Gregora654dd82009-08-28 17:37:35 +000062 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000063 = Function->getTemplateSpecializationArgs()) {
64 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000065 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000066
Douglas Gregorcf915552009-10-13 16:30:37 +000067 // If this function was instantiated from a specialized member that is
68 // a function template, we're done.
69 assert(Function->getPrimaryTemplate() && "No function template?");
70 if (Function->getPrimaryTemplate()->isMemberSpecialization())
71 break;
72 }
73
John McCall970d5302009-08-29 03:16:09 +000074 // If this is a friend declaration and it declares an entity at
75 // namespace scope, take arguments from its lexical parent
76 // instead of its semantic parent.
77 if (Function->getFriendObjectKind() &&
78 Function->getDeclContext()->isFileContext()) {
79 Ctx = Function->getLexicalDeclContext();
80 continue;
81 }
Douglas Gregora654dd82009-08-28 17:37:35 +000082 }
John McCall970d5302009-08-29 03:16:09 +000083
84 Ctx = Ctx->getParent();
Douglas Gregorb4850462009-05-14 23:26:13 +000085 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Douglas Gregora654dd82009-08-28 17:37:35 +000087 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +000088}
89
Douglas Gregorfcd5db32009-03-10 00:06:19 +000090Sema::InstantiatingTemplate::
91InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +000092 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +000093 SourceRange InstantiationRange)
94 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +000095
96 Invalid = CheckInstantiationDepth(PointOfInstantiation,
97 InstantiationRange);
98 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +000099 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000100 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000101 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000102 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000103 Inst.TemplateArgs = 0;
104 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000105 Inst.InstantiationRange = InstantiationRange;
106 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
107 Invalid = false;
108 }
109}
110
Mike Stump11289f42009-09-09 15:08:12 +0000111Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000112 SourceLocation PointOfInstantiation,
113 TemplateDecl *Template,
114 const TemplateArgument *TemplateArgs,
115 unsigned NumTemplateArgs,
116 SourceRange InstantiationRange)
117 : SemaRef(SemaRef) {
118
119 Invalid = CheckInstantiationDepth(PointOfInstantiation,
120 InstantiationRange);
121 if (!Invalid) {
122 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000123 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000124 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
125 Inst.PointOfInstantiation = PointOfInstantiation;
126 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
127 Inst.TemplateArgs = TemplateArgs;
128 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000129 Inst.InstantiationRange = InstantiationRange;
130 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
131 Invalid = false;
132 }
133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000136 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000137 FunctionTemplateDecl *FunctionTemplate,
138 const TemplateArgument *TemplateArgs,
139 unsigned NumTemplateArgs,
140 ActiveTemplateInstantiation::InstantiationKind Kind,
141 SourceRange InstantiationRange)
142: SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000143
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000144 Invalid = CheckInstantiationDepth(PointOfInstantiation,
145 InstantiationRange);
146 if (!Invalid) {
147 ActiveTemplateInstantiation Inst;
148 Inst.Kind = Kind;
149 Inst.PointOfInstantiation = PointOfInstantiation;
150 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
151 Inst.TemplateArgs = TemplateArgs;
152 Inst.NumTemplateArgs = NumTemplateArgs;
153 Inst.InstantiationRange = InstantiationRange;
154 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
155 Invalid = false;
156 }
157}
158
Mike Stump11289f42009-09-09 15:08:12 +0000159Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000160 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000161 ClassTemplatePartialSpecializationDecl *PartialSpec,
162 const TemplateArgument *TemplateArgs,
163 unsigned NumTemplateArgs,
164 SourceRange InstantiationRange)
165 : SemaRef(SemaRef) {
166
167 Invalid = CheckInstantiationDepth(PointOfInstantiation,
168 InstantiationRange);
169 if (!Invalid) {
170 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000171 Inst.Kind
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000172 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637d9982009-06-10 23:47:09 +0000173 Inst.PointOfInstantiation = PointOfInstantiation;
174 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
175 Inst.TemplateArgs = TemplateArgs;
176 Inst.NumTemplateArgs = NumTemplateArgs;
177 Inst.InstantiationRange = InstantiationRange;
178 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
179 Invalid = false;
180 }
181}
182
Mike Stump11289f42009-09-09 15:08:12 +0000183Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Anders Carlsson657bad42009-09-05 05:14:19 +0000184 SourceLocation PointOfInstantation,
185 ParmVarDecl *Param,
186 const TemplateArgument *TemplateArgs,
187 unsigned NumTemplateArgs,
188 SourceRange InstantiationRange)
189 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000190
Anders Carlsson657bad42009-09-05 05:14:19 +0000191 Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange);
192
193 if (!Invalid) {
194 ActiveTemplateInstantiation Inst;
195 Inst.Kind
196 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
197 Inst.PointOfInstantiation = PointOfInstantation;
198 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
199 Inst.TemplateArgs = TemplateArgs;
200 Inst.NumTemplateArgs = NumTemplateArgs;
201 Inst.InstantiationRange = InstantiationRange;
202 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
203 Invalid = false;
204 }
205}
206
Douglas Gregor85673582009-05-18 17:01:57 +0000207void Sema::InstantiatingTemplate::Clear() {
208 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000209 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000210 Invalid = true;
211 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000212}
213
Douglas Gregor79cf6032009-03-10 20:44:00 +0000214bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
215 SourceLocation PointOfInstantiation,
216 SourceRange InstantiationRange) {
Mike Stump11289f42009-09-09 15:08:12 +0000217 if (SemaRef.ActiveTemplateInstantiations.size()
Douglas Gregor79cf6032009-03-10 20:44:00 +0000218 <= SemaRef.getLangOptions().InstantiationDepth)
219 return false;
220
Mike Stump11289f42009-09-09 15:08:12 +0000221 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000222 diag::err_template_recursion_depth_exceeded)
223 << SemaRef.getLangOptions().InstantiationDepth
224 << InstantiationRange;
225 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
226 << SemaRef.getLangOptions().InstantiationDepth;
227 return true;
228}
229
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000230/// \brief Prints the current instantiation stack through a series of
231/// notes.
232void Sema::PrintInstantiationStack() {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000233 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000234 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
235 Active = ActiveTemplateInstantiations.rbegin(),
236 ActiveEnd = ActiveTemplateInstantiations.rend();
237 Active != ActiveEnd;
238 ++Active) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000239 switch (Active->Kind) {
240 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000241 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
242 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
243 unsigned DiagID = diag::note_template_member_class_here;
244 if (isa<ClassTemplateSpecializationDecl>(Record))
245 DiagID = diag::note_template_class_instantiation_here;
Mike Stump11289f42009-09-09 15:08:12 +0000246 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000247 DiagID)
248 << Context.getTypeDeclType(Record)
249 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000250 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000251 unsigned DiagID;
252 if (Function->getPrimaryTemplate())
253 DiagID = diag::note_function_template_spec_here;
254 else
255 DiagID = diag::note_template_member_function_here;
Mike Stump11289f42009-09-09 15:08:12 +0000256 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000257 DiagID)
258 << Function
259 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000260 } else {
261 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
262 diag::note_template_static_data_member_def_here)
263 << cast<VarDecl>(D)
264 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000265 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000266 break;
267 }
268
269 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
270 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
271 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000272 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000273 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000274 Active->NumTemplateArgs,
275 Context.PrintingPolicy);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000276 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
277 diag::note_default_arg_instantiation_here)
278 << (Template->getNameAsString() + TemplateArgsStr)
279 << Active->InstantiationRange;
280 break;
281 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000282
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000283 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000284 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000285 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000286 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000287 diag::note_explicit_template_arg_substitution_here)
288 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000289 break;
290 }
Mike Stump11289f42009-09-09 15:08:12 +0000291
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000292 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
293 if (ClassTemplatePartialSpecializationDecl *PartialSpec
294 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
295 (Decl *)Active->Entity)) {
296 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
297 diag::note_partial_spec_deduct_instantiation_here)
298 << Context.getTypeDeclType(PartialSpec)
299 << Active->InstantiationRange;
300 } else {
301 FunctionTemplateDecl *FnTmpl
302 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
303 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
304 diag::note_function_template_deduction_instantiation_here)
305 << FnTmpl << Active->InstantiationRange;
306 }
307 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000308
Anders Carlsson657bad42009-09-05 05:14:19 +0000309 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
310 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
311 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson657bad42009-09-05 05:14:19 +0000313 std::string TemplateArgsStr
314 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000315 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000316 Active->NumTemplateArgs,
317 Context.PrintingPolicy);
318 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
319 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000320 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000321 << Active->InstantiationRange;
322 break;
323 }
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregor79cf6032009-03-10 20:44:00 +0000325 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000326 }
327}
328
Douglas Gregor33834512009-06-14 07:33:30 +0000329bool Sema::isSFINAEContext() const {
330 using llvm::SmallVector;
331 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
332 Active = ActiveTemplateInstantiations.rbegin(),
333 ActiveEnd = ActiveTemplateInstantiations.rend();
334 Active != ActiveEnd;
335 ++Active) {
336
337 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000338 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000339 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
340
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000341 // This is a template instantiation, so there is no SFINAE.
342 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000343
Douglas Gregor33834512009-06-14 07:33:30 +0000344 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
345 // A default template argument instantiation may or may not be a
346 // SFINAE context; look further up the stack.
347 break;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000349 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
350 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
351 // We're either substitution explicitly-specified template arguments
352 // or deduced template arguments, so SFINAE applies.
353 return true;
Douglas Gregor33834512009-06-14 07:33:30 +0000354 }
355 }
356
357 return false;
358}
359
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000360//===----------------------------------------------------------------------===/
361// Template Instantiation for Types
362//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000363namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000364 class VISIBILITY_HIDDEN TemplateInstantiator
365 : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000366 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000367 SourceLocation Loc;
368 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000369
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000370 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000371 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000372
373 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000374 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000375 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000376 DeclarationName Entity)
377 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000378 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000379
Mike Stump11289f42009-09-09 15:08:12 +0000380 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000381 /// transformed.
382 ///
383 /// For the purposes of template instantiation, a type has already been
384 /// transformed if it is NULL or if it is not dependent.
385 bool AlreadyTransformed(QualType T) {
386 return T.isNull() || !T->isDependentType();
Douglas Gregorf61eca92009-05-13 18:28:20 +0000387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Douglas Gregord6ff3322009-08-04 16:50:30 +0000389 /// \brief Returns the location of the entity being instantiated, if known.
390 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Douglas Gregord6ff3322009-08-04 16:50:30 +0000392 /// \brief Returns the name of the entity being instantiated, if any.
393 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Douglas Gregord6ff3322009-08-04 16:50:30 +0000395 /// \brief Transform the given declaration by instantiating a reference to
396 /// this declaration.
397 Decl *TransformDecl(Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000398
Mike Stump11289f42009-09-09 15:08:12 +0000399 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000400 /// instantiating it.
401 Decl *TransformDefinition(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Douglas Gregorebe10102009-08-20 07:17:43 +0000403 /// \brief Rebuild the exception declaration and register the declaration
404 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000405 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000406 DeclaratorInfo *Declarator,
407 IdentifierInfo *Name,
408 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000409
John McCall7f41d982009-09-11 04:59:25 +0000410 /// \brief Check for tag mismatches when instantiating an
411 /// elaborated type.
412 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
413
Anders Carlsson0b209a82009-09-11 01:22:35 +0000414 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000415 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump11289f42009-09-09 15:08:12 +0000416
417 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000418 /// substitution of the corresponding template type argument.
419 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
420 };
Douglas Gregor04318252009-07-06 15:59:29 +0000421}
422
Douglas Gregord6ff3322009-08-04 16:50:30 +0000423Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000424 if (!D)
425 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000427 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000428 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
429 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
430 "Wrong kind of template template argument");
Mike Stump11289f42009-09-09 15:08:12 +0000431 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000432 TTP->getPosition()).getAsDecl());
433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
435 // If the corresponding template argument is NULL or non-existent, it's
436 // because we are performing instantiation from explicitly-specified
Douglas Gregor01afeef2009-08-28 20:31:08 +0000437 // template arguments in a function template, but there were some
438 // arguments left unspecified.
Mike Stump11289f42009-09-09 15:08:12 +0000439 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000440 TTP->getPosition()))
441 return D;
Mike Stump11289f42009-09-09 15:08:12 +0000442
Douglas Gregor01afeef2009-08-28 20:31:08 +0000443 // FIXME: Implement depth reduction of template template parameters
Mike Stump11289f42009-09-09 15:08:12 +0000444 assert(false &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000445 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor71dc5092009-08-06 06:41:21 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Douglas Gregor64621e62009-09-16 18:34:49 +0000448 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000449}
450
Douglas Gregorebe10102009-08-20 07:17:43 +0000451Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000452 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000453 if (!Inst)
454 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000455
Douglas Gregorebe10102009-08-20 07:17:43 +0000456 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
457 return Inst;
458}
459
460VarDecl *
461TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000462 QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000463 DeclaratorInfo *Declarator,
464 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000465 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000466 SourceRange TypeRange) {
467 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
468 Name, Loc, TypeRange);
469 if (Var && !Var->isInvalidDecl())
470 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
471 return Var;
472}
473
John McCall7f41d982009-09-11 04:59:25 +0000474QualType
475TemplateInstantiator::RebuildElaboratedType(QualType T,
476 ElaboratedType::TagKind Tag) {
477 if (const TagType *TT = T->getAs<TagType>()) {
478 TagDecl* TD = TT->getDecl();
479
480 // FIXME: this location is very wrong; we really need typelocs.
481 SourceLocation TagLocation = TD->getTagKeywordLoc();
482
483 // FIXME: type might be anonymous.
484 IdentifierInfo *Id = TD->getIdentifier();
485
486 // TODO: should we even warn on struct/class mismatches for this? Seems
487 // like it's likely to produce a lot of spurious errors.
488 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
489 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
490 << Id
491 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
492 TD->getKindName());
493 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
494 }
495 }
496
497 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
498}
499
500Sema::OwningExprResult
Anders Carlsson0b209a82009-09-11 01:22:35 +0000501TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
502 if (!E->isTypeDependent())
503 return SemaRef.Owned(E->Retain());
504
505 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
506 assert(currentDecl && "Must have current function declaration when "
507 "instantiating.");
508
509 PredefinedExpr::IdentType IT = E->getIdentType();
510
511 unsigned Length =
512 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
513
514 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000515 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000516 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
517 ArrayType::Normal, 0);
518 PredefinedExpr *PE =
519 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
520 return getSema().Owned(PE);
521}
522
523Sema::OwningExprResult
Douglas Gregora16548e2009-08-11 05:31:07 +0000524TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
525 // FIXME: Clean this up a bit
526 NamedDecl *D = E->getDecl();
527 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000528 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
529 assert(false && "Cannot reduce non-type template parameter depth yet");
530 return getSema().ExprError();
531 }
Mike Stump11289f42009-09-09 15:08:12 +0000532
533 // If the corresponding template argument is NULL or non-existent, it's
534 // because we are performing instantiation from explicitly-specified
Douglas Gregora16548e2009-08-11 05:31:07 +0000535 // template arguments in a function template, but there were some
536 // arguments left unspecified.
Mike Stump11289f42009-09-09 15:08:12 +0000537 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000538 NTTP->getPosition()))
539 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000540
541 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000542 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000543
Douglas Gregora16548e2009-08-11 05:31:07 +0000544 // The template argument itself might be an expression, in which
545 // case we just return that expression.
546 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregor01afeef2009-08-28 20:31:08 +0000547 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000548
Douglas Gregora16548e2009-08-11 05:31:07 +0000549 if (Arg.getKind() == TemplateArgument::Declaration) {
550 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000551
Douglas Gregor64621e62009-09-16 18:34:49 +0000552 VD = cast_or_null<ValueDecl>(
553 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor01afeef2009-08-28 20:31:08 +0000554 if (!VD)
555 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000556
557 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000558 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregora16548e2009-08-11 05:31:07 +0000559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Douglas Gregora16548e2009-08-11 05:31:07 +0000561 assert(Arg.getKind() == TemplateArgument::Integral);
562 QualType T = Arg.getIntegralType();
563 if (T->isCharType() || T->isWideCharType())
564 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregor01afeef2009-08-28 20:31:08 +0000565 Arg.getAsIntegral()->getZExtValue(),
566 T->isWideCharType(),
Mike Stump11289f42009-09-09 15:08:12 +0000567 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000568 E->getSourceRange().getBegin()));
Douglas Gregora16548e2009-08-11 05:31:07 +0000569 if (T->isBooleanType())
570 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregor01afeef2009-08-28 20:31:08 +0000571 Arg.getAsIntegral()->getBoolValue(),
Mike Stump11289f42009-09-09 15:08:12 +0000572 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000573 E->getSourceRange().getBegin()));
Mike Stump11289f42009-09-09 15:08:12 +0000574
Douglas Gregora16548e2009-08-11 05:31:07 +0000575 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
576 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregor01afeef2009-08-28 20:31:08 +0000577 *Arg.getAsIntegral(),
Mike Stump11289f42009-09-09 15:08:12 +0000578 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000579 E->getSourceRange().getBegin()));
Douglas Gregora16548e2009-08-11 05:31:07 +0000580 }
Mike Stump11289f42009-09-09 15:08:12 +0000581
Douglas Gregor64621e62009-09-16 18:34:49 +0000582 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregora16548e2009-08-11 05:31:07 +0000583 if (!InstD)
584 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000585
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000586 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000587 // we need to get the underlying decl.
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000588 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
589 InstD = InstD->getUnderlyingDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000590
Douglas Gregora16548e2009-08-11 05:31:07 +0000591 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
Mike Stump11289f42009-09-09 15:08:12 +0000592 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregora16548e2009-08-11 05:31:07 +0000593 /*FIXME:*/false,
Mike Stump11289f42009-09-09 15:08:12 +0000594 /*FIXME:*/0,
595 /*FIXME:*/false);
Douglas Gregora16548e2009-08-11 05:31:07 +0000596}
597
Mike Stump11289f42009-09-09 15:08:12 +0000598QualType
Douglas Gregord6ff3322009-08-04 16:50:30 +0000599TemplateInstantiator::TransformTemplateTypeParmType(
600 const TemplateTypeParmType *T) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000601 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000602 // Replace the template type parameter with its corresponding
603 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000604
605 // If the corresponding template argument is NULL or doesn't exist, it's
606 // because we are performing instantiation from explicitly-specified
607 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000608 // arguments left unspecified.
Douglas Gregor01afeef2009-08-28 20:31:08 +0000609 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
610 return QualType(T, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000611
612 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000613 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000614 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000615
616 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Mike Stump11289f42009-09-09 15:08:12 +0000617 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000618
619 // The template type parameter comes from an inner template (e.g.,
620 // the template parameter list of a member template inside the
621 // template we are instantiating). Create a new template type
622 // parameter with the template "level" reduced by one.
Douglas Gregor01afeef2009-08-28 20:31:08 +0000623 return getSema().Context.getTemplateTypeParmType(
624 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregord6ff3322009-08-04 16:50:30 +0000625 T->getIndex(),
626 T->isParameterPack(),
627 T->getName());
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000628}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000629
John McCall76d824f2009-08-25 22:02:44 +0000630/// \brief Perform substitution on the type T with a given set of template
631/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000632///
633/// This routine substitutes the given template arguments into the
634/// type T and produces the instantiated type.
635///
636/// \param T the type into which the template arguments will be
637/// substituted. If this type is not dependent, it will be returned
638/// immediately.
639///
640/// \param TemplateArgs the template arguments that will be
641/// substituted for the top-level template parameters within T.
642///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000643/// \param Loc the location in the source code where this substitution
644/// is being performed. It will typically be the location of the
645/// declarator (if we're instantiating the type of some declaration)
646/// or the location of the type in the source code (if, e.g., we're
647/// instantiating the type of a cast expression).
648///
649/// \param Entity the name of the entity associated with a declaration
650/// being instantiated (if any). May be empty to indicate that there
651/// is no such entity (if, e.g., this is a type that occurs as part of
652/// a cast expression) or that the entity has no name (e.g., an
653/// unnamed function parameter).
654///
655/// \returns If the instantiation succeeds, the instantiated
656/// type. Otherwise, produces diagnostics and returns a NULL type.
Mike Stump11289f42009-09-09 15:08:12 +0000657QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000658 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000659 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000660 assert(!ActiveTemplateInstantiations.empty() &&
661 "Cannot perform an instantiation without some context on the "
662 "instantiation stack");
663
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000664 // If T is not a dependent type, there is nothing to do.
665 if (!T->isDependentType())
666 return T;
667
Douglas Gregord6ff3322009-08-04 16:50:30 +0000668 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
669 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000670}
Douglas Gregor463421d2009-03-03 04:44:36 +0000671
John McCall76d824f2009-08-25 22:02:44 +0000672/// \brief Perform substitution on the base class specifiers of the
673/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +0000674///
675/// Produces a diagnostic and returns true on error, returns false and
676/// attaches the instantiated base classes to the class template
677/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +0000678bool
John McCall76d824f2009-08-25 22:02:44 +0000679Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
680 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000681 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000682 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +0000683 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +0000684 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000685 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000686 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000687 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +0000688 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +0000689 continue;
690 }
691
Mike Stump11289f42009-09-09 15:08:12 +0000692 QualType BaseType = SubstType(Base->getType(),
693 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000694 Base->getSourceRange().getBegin(),
695 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +0000696 if (BaseType.isNull()) {
697 Invalid = true;
698 continue;
699 }
700
701 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000702 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000703 Base->getSourceRange(),
704 Base->isVirtual(),
705 Base->getAccessSpecifierAsWritten(),
706 BaseType,
707 /*FIXME: Not totally accurate */
708 Base->getSourceRange().getBegin()))
709 InstantiatedBases.push_back(InstantiatedBase);
710 else
711 Invalid = true;
712 }
713
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000714 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +0000715 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +0000716 InstantiatedBases.size()))
717 Invalid = true;
718
719 return Invalid;
720}
721
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000722/// \brief Instantiate the definition of a class from a given pattern.
723///
724/// \param PointOfInstantiation The point of instantiation within the
725/// source code.
726///
727/// \param Instantiation is the declaration whose definition is being
728/// instantiated. This will be either a class template specialization
729/// or a member class of a class template specialization.
730///
731/// \param Pattern is the pattern from which the instantiation
732/// occurs. This will be either the declaration of a class template or
733/// the declaration of a member class of a class template.
734///
735/// \param TemplateArgs The template arguments to be substituted into
736/// the pattern.
737///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000738/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000739///
740/// \param Complain whether to complain if the class cannot be instantiated due
741/// to the lack of a definition.
742///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000743/// \returns true if an error occurred, false otherwise.
744bool
745Sema::InstantiateClass(SourceLocation PointOfInstantiation,
746 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000747 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000748 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000749 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000750 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +0000751
Mike Stump11289f42009-09-09 15:08:12 +0000752 CXXRecordDecl *PatternDef
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000753 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
754 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000755 if (!Complain) {
756 // Say nothing
757 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000758 Diag(PointOfInstantiation,
759 diag::err_implicit_instantiate_member_undefined)
760 << Context.getTypeDeclType(Instantiation);
761 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
762 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +0000763 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000764 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000765 << Context.getTypeDeclType(Instantiation);
766 Diag(Pattern->getLocation(), diag::note_template_decl_here);
767 }
768 return true;
769 }
770 Pattern = PatternDef;
771
Douglas Gregorf3430ae2009-03-25 21:23:52 +0000772 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000773 if (Inst)
774 return true;
775
776 // Enter the scope of this instantiation. We don't use
777 // PushDeclContext because we don't have a scope.
778 DeclContext *PreviousContext = CurContext;
779 CurContext = Instantiation;
780
781 // Start the definition of this instantiation.
782 Instantiation->startDefinition();
783
John McCall76d824f2009-08-25 22:02:44 +0000784 // Do substitution on the base class specifiers.
785 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000786 Invalid = true;
787
Douglas Gregor6181ded2009-05-29 18:27:38 +0000788 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000789 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000790 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000791 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +0000792 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000793 if (NewMember) {
794 if (NewMember->isInvalidDecl())
795 Invalid = true;
796 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +0000797 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000798 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
799 Instantiation->addDecl(UD);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000800 } else {
801 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +0000802 // instantiations was a semantic disaster, and we'll want to set Invalid =
803 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000804 }
805 }
806
807 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +0000808 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +0000809 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000810 0);
Douglas Gregor3c74d412009-10-14 20:14:33 +0000811 if (Instantiation->isInvalidDecl())
812 Invalid = true;
813
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000814 // Add any implicitly-declared members that we might need.
Douglas Gregor3c74d412009-10-14 20:14:33 +0000815 if (!Invalid)
816 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000817
818 // Exit the scope of this instantiation.
819 CurContext = PreviousContext;
820
Douglas Gregor28ad4b52009-05-26 20:50:29 +0000821 if (!Invalid)
822 Consumer.HandleTagDeclDefinition(Instantiation);
823
Douglas Gregorbbbb02d2009-05-13 20:28:22 +0000824 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000825 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregor85673582009-05-18 17:01:57 +0000826 Inst.Clear();
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000827 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
828 TSK);
Douglas Gregor85673582009-05-18 17:01:57 +0000829 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +0000830
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000831 return Invalid;
832}
833
Mike Stump11289f42009-09-09 15:08:12 +0000834bool
Douglas Gregor463421d2009-03-03 04:44:36 +0000835Sema::InstantiateClassTemplateSpecialization(
836 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000837 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000838 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000839 // Perform the actual instantiation on the canonical declaration.
840 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000841 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +0000842
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000843 // Check whether we have already instantiated or specialized this class
844 // template specialization.
845 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
846 if (ClassTemplateSpec->getSpecializationKind() ==
847 TSK_ExplicitInstantiationDeclaration &&
848 TSK == TSK_ExplicitInstantiationDefinition) {
849 // An explicit instantiation definition follows an explicit instantiation
850 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
851 // explicit instantiation.
852 ClassTemplateSpec->setSpecializationKind(TSK);
853 InstantiateClassTemplateSpecializationMembers(
854 /*FIXME?*/ClassTemplateSpec->getPointOfInstantiation(),
855 ClassTemplateSpec,
856 TSK);
857 return false;
858 }
859
860 // We can only instantiate something that hasn't already been
861 // instantiated or specialized. Fail without any diagnostics: our
862 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +0000863 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000864 }
Douglas Gregor463421d2009-03-03 04:44:36 +0000865
Douglas Gregor00a511f2009-09-15 16:51:42 +0000866 if (ClassTemplateSpec->isInvalidDecl())
867 return true;
868
Douglas Gregor463421d2009-03-03 04:44:36 +0000869 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000870 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +0000871
Douglas Gregor170bc422009-06-12 22:31:52 +0000872 // C++ [temp.class.spec.match]p1:
873 // When a class template is used in a context that requires an
874 // instantiation of the class, it is necessary to determine
875 // whether the instantiation is to be generated using the primary
876 // template or one of the partial specializations. This is done by
877 // matching the template arguments of the class template
878 // specialization with the template argument lists of the partial
879 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000880 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
881 TemplateArgumentList *> MatchResult;
882 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump11289f42009-09-09 15:08:12 +0000883 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor2373c592009-05-31 09:31:02 +0000884 Partial = Template->getPartialSpecializations().begin(),
885 PartialEnd = Template->getPartialSpecializations().end();
886 Partial != PartialEnd;
887 ++Partial) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000888 TemplateDeductionInfo Info(Context);
889 if (TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +0000890 = DeduceTemplateArguments(&*Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000891 ClassTemplateSpec->getTemplateArgs(),
892 Info)) {
893 // FIXME: Store the failed-deduction information for use in
894 // diagnostics, later.
895 (void)Result;
896 } else {
897 Matched.push_back(std::make_pair(&*Partial, Info.take()));
898 }
Douglas Gregor2373c592009-05-31 09:31:02 +0000899 }
900
901 if (Matched.size() == 1) {
Douglas Gregor170bc422009-06-12 22:31:52 +0000902 // -- If exactly one matching specialization is found, the
903 // instantiation is generated from that specialization.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000904 Pattern = Matched[0].first;
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000905 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor2373c592009-05-31 09:31:02 +0000906 } else if (Matched.size() > 1) {
Douglas Gregor170bc422009-06-12 22:31:52 +0000907 // -- If more than one matching specialization is found, the
908 // partial order rules (14.5.4.2) are used to determine
909 // whether one of the specializations is more specialized
910 // than the others. If none of the specializations is more
911 // specialized than all of the other matching
912 // specializations, then the use of the class template is
913 // ambiguous and the program is ill-formed.
Douglas Gregorbe999392009-09-15 16:23:51 +0000914 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
915 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
916 PEnd = Matched.end();
917 P != PEnd; ++P) {
918 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
919 == P->first)
920 Best = P;
921 }
922
923 // Determine if the best partial specialization is more specialized than
924 // the others.
925 bool Ambiguous = false;
926 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
927 PEnd = Matched.end();
928 P != PEnd; ++P) {
929 if (P != Best &&
930 getMoreSpecializedPartialSpecialization(P->first, Best->first)
931 != Best->first) {
932 Ambiguous = true;
933 break;
934 }
935 }
936
937 if (Ambiguous) {
938 // Partial ordering did not produce a clear winner. Complain.
939 ClassTemplateSpec->setInvalidDecl();
940 Diag(ClassTemplateSpec->getPointOfInstantiation(),
941 diag::err_partial_spec_ordering_ambiguous)
942 << ClassTemplateSpec;
943
944 // Print the matching partial specializations.
945 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
946 PEnd = Matched.end();
947 P != PEnd; ++P)
948 Diag(P->first->getLocation(), diag::note_partial_spec_match)
949 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
950 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +0000951
Douglas Gregorbe999392009-09-15 16:23:51 +0000952 return true;
953 }
954
955 // Instantiate using the best class template partial specialization.
956 Pattern = Best->first;
957 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +0000958 } else {
959 // -- If no matches are found, the instantiation is generated
960 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +0000961 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +0000962 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
963 // If we've found an explicit specialization of this class template,
964 // stop here and use that as the pattern.
965 if (OrigTemplate->isMemberSpecialization())
966 break;
967
Douglas Gregor01afeef2009-08-28 20:31:08 +0000968 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +0000969 }
970
Douglas Gregor01afeef2009-08-28 20:31:08 +0000971 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +0000972 }
Douglas Gregor463421d2009-03-03 04:44:36 +0000973
Douglas Gregor01afeef2009-08-28 20:31:08 +0000974 // Note that this is an instantiation.
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000975 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor463421d2009-03-03 04:44:36 +0000976
John McCall1806c272009-09-11 07:25:08 +0000977 bool Result = InstantiateClass(ClassTemplateSpec->getPointOfInstantiation(),
Mike Stump11289f42009-09-09 15:08:12 +0000978 ClassTemplateSpec, Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000979 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000980 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000981 Complain);
Mike Stump11289f42009-09-09 15:08:12 +0000982
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000983 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
984 // FIXME: Implement TemplateArgumentList::Destroy!
985 // if (Matched[I].first != Pattern)
986 // Matched[I].second->Destroy(Context);
987 }
Mike Stump11289f42009-09-09 15:08:12 +0000988
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000989 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +0000990}
Douglas Gregor90a1a652009-03-19 17:26:29 +0000991
John McCall76d824f2009-08-25 22:02:44 +0000992/// \brief Instantiates the definitions of all of the member
993/// of the given class, which is an instantiation of a class template
994/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +0000995void
996Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000997 CXXRecordDecl *Instantiation,
998 const MultiLevelTemplateArgumentList &TemplateArgs,
999 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001000 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1001 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001002 D != DEnd; ++D) {
1003 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001004 if (Function->getInstantiatedFromMemberFunction()) {
1005 // If this member was explicitly specialized, do nothing.
1006 if (Function->getTemplateSpecializationKind() ==
1007 TSK_ExplicitSpecialization)
1008 continue;
1009
Douglas Gregord801b062009-10-07 23:56:10 +00001010 Function->setTemplateSpecializationKind(TSK);
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001011 }
1012
1013 if (!Function->getBody() && TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregor85673582009-05-18 17:01:57 +00001014 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001015 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001016 if (Var->isStaticDataMember()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001017 // If this member was explicitly specialized, do nothing.
1018 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1019 continue;
1020
Douglas Gregor86d142a2009-10-08 07:24:58 +00001021 Var->setTemplateSpecializationKind(TSK);
1022
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001023 if (TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregor86d142a2009-10-08 07:24:58 +00001024 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
1025 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001026 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregord801b062009-10-07 23:56:10 +00001027 if (Record->isInjectedClassName())
1028 continue;
1029
1030 assert(Record->getInstantiatedFromMemberClass() &&
1031 "Missing instantiated-from-template information");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001032
1033 // If this member was explicitly specialized, do nothing.
1034 if (Record->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1035 continue;
1036
Douglas Gregord801b062009-10-07 23:56:10 +00001037 if (!Record->getDefinition(Context))
Douglas Gregor85673582009-05-18 17:01:57 +00001038 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001039 Record->getInstantiatedFromMemberClass(),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001040 TemplateArgs,
1041 TSK);
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001042
1043 InstantiateClassMembers(PointOfInstantiation, Record, TemplateArgs,
1044 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001045 }
1046 }
1047}
1048
1049/// \brief Instantiate the definitions of all of the members of the
1050/// given class template specialization, which was named as part of an
1051/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001052void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001053Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001054 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001055 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1056 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001057 // C++0x [temp.explicit]p7:
1058 // An explicit instantiation that names a class template
1059 // specialization is an explicit instantion of the same kind
1060 // (declaration or definition) of each of its members (not
1061 // including members inherited from base classes) that has not
1062 // been previously explicitly specialized in the translation unit
1063 // containing the explicit instantiation, except as described
1064 // below.
1065 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001066 getTemplateInstantiationArgs(ClassTemplateSpec),
1067 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001068}
1069
Mike Stump11289f42009-09-09 15:08:12 +00001070Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001071Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001072 if (!S)
1073 return Owned(S);
1074
1075 TemplateInstantiator Instantiator(*this, TemplateArgs,
1076 SourceLocation(),
1077 DeclarationName());
1078 return Instantiator.TransformStmt(S);
1079}
1080
Mike Stump11289f42009-09-09 15:08:12 +00001081Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001082Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001083 if (!E)
1084 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001085
Douglas Gregora16548e2009-08-11 05:31:07 +00001086 TemplateInstantiator Instantiator(*this, TemplateArgs,
1087 SourceLocation(),
1088 DeclarationName());
1089 return Instantiator.TransformExpr(E);
1090}
1091
John McCall76d824f2009-08-25 22:02:44 +00001092/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001093NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001094Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001095 SourceRange Range,
1096 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001097 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1098 DeclarationName());
1099 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001100}
Douglas Gregoraa594892009-03-31 18:38:02 +00001101
1102TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001103Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001104 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001105 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1106 DeclarationName());
1107 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001108}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001109
Mike Stump11289f42009-09-09 15:08:12 +00001110TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001111 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001112 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1113 DeclarationName());
1114 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001115}