blob: d48d5fc134fcb6cf9292aee50e0d15291d792057 [file] [log] [blame]
Douglas Gregor99ebf652009-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 Gregor577f75a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregoree1828a2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregord6350ae2009-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 Gregord1102432009-08-28 17:37:35 +000031MultiLevelTemplateArgumentList
Douglas Gregor54dabfc2009-05-14 23:26:13 +000032Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregord1102432009-08-28 17:37:35 +000033 // Accumulate the set of template argument lists in this structure.
34 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Douglas Gregord1102432009-08-28 17:37:35 +000036 DeclContext *Ctx = dyn_cast<DeclContext>(D);
37 if (!Ctx)
38 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000039
John McCallf181d8a2009-08-29 03:16:09 +000040 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000041 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000042 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-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 Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregord1102432009-08-28 17:37:35 +000048 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-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 Stump1eb44332009-09-09 15:08:12 +000055 }
Douglas Gregord1102432009-08-28 17:37:35 +000056 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000057 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +000058 if (Function->getTemplateSpecializationKind()
59 == TSK_ExplicitSpecialization)
60 break;
61
Douglas Gregord1102432009-08-28 17:37:35 +000062 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +000063 = Function->getTemplateSpecializationArgs()) {
64 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +000065 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +000066
Douglas Gregorfd056bc2009-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 McCallf181d8a2009-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 Gregord1102432009-08-28 17:37:35 +000082 }
John McCallf181d8a2009-08-29 03:16:09 +000083
84 Ctx = Ctx->getParent();
Douglas Gregor54dabfc2009-05-14 23:26:13 +000085 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregord1102432009-08-28 17:37:35 +000087 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +000088}
89
Douglas Gregor26dce442009-03-10 00:06:19 +000090Sema::InstantiatingTemplate::
91InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000092 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000093 SourceRange InstantiationRange)
94 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000095
96 Invalid = CheckInstantiationDepth(PointOfInstantiation,
97 InstantiationRange);
98 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000099 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000100 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000101 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000102 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000103 Inst.TemplateArgs = 0;
104 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000105 Inst.InstantiationRange = InstantiationRange;
106 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
107 Invalid = false;
108 }
109}
110
Mike Stump1eb44332009-09-09 15:08:12 +0000111Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-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 Stump1eb44332009-09-09 15:08:12 +0000123 Inst.Kind
Douglas Gregordf667e72009-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 Gregor26dce442009-03-10 00:06:19 +0000129 Inst.InstantiationRange = InstantiationRange;
130 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
131 Invalid = false;
132 }
133}
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000136 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-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 Stump1eb44332009-09-09 15:08:12 +0000143
Douglas Gregorcca9e962009-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 Stump1eb44332009-09-09 15:08:12 +0000159Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000160 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-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 Stump1eb44332009-09-09 15:08:12 +0000171 Inst.Kind
Douglas Gregorcca9e962009-07-01 22:01:06 +0000172 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637a4092009-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 Stump1eb44332009-09-09 15:08:12 +0000183Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Anders Carlsson25cae7f2009-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 Stump1eb44332009-09-09 15:08:12 +0000190
Anders Carlsson25cae7f2009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000207void Sema::InstantiatingTemplate::Clear() {
208 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000209 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000210 Invalid = true;
211 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000212}
213
Douglas Gregordf667e72009-03-10 20:44:00 +0000214bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
215 SourceLocation PointOfInstantiation,
216 SourceRange InstantiationRange) {
Mike Stump1eb44332009-09-09 15:08:12 +0000217 if (SemaRef.ActiveTemplateInstantiations.size()
Douglas Gregordf667e72009-03-10 20:44:00 +0000218 <= SemaRef.getLangOptions().InstantiationDepth)
219 return false;
220
Mike Stump1eb44332009-09-09 15:08:12 +0000221 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-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 Gregoree1828a2009-03-10 18:03:33 +0000230/// \brief Prints the current instantiation stack through a series of
231/// notes.
232void Sema::PrintInstantiationStack() {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000233 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregoree1828a2009-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 Gregordf667e72009-03-10 20:44:00 +0000239 switch (Active->Kind) {
240 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-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 Stump1eb44332009-09-09 15:08:12 +0000246 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000247 DiagID)
248 << Context.getTypeDeclType(Record)
249 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000250 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-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 Stump1eb44332009-09-09 15:08:12 +0000256 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000257 DiagID)
258 << Function
259 << Active->InstantiationRange;
Douglas Gregor7caa6822009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000265 }
Douglas Gregordf667e72009-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 Gregor7532dc62009-03-30 22:58:21 +0000272 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000273 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000274 Active->NumTemplateArgs,
275 Context.PrintingPolicy);
Douglas Gregordf667e72009-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 Gregor637a4092009-06-10 23:47:09 +0000282
Douglas Gregorcca9e962009-07-01 22:01:06 +0000283 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000284 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000285 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000286 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000287 diag::note_explicit_template_arg_substitution_here)
288 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000289 break;
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Douglas Gregorcca9e962009-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 Gregor637a4092009-06-10 23:47:09 +0000308
Anders Carlsson25cae7f2009-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 Stump1eb44332009-09-09 15:08:12 +0000312
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000313 std::string TemplateArgsStr
314 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000315 Active->TemplateArgs,
Anders Carlsson25cae7f2009-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 Carlsson6bc107b2009-09-05 05:38:54 +0000320 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000321 << Active->InstantiationRange;
322 break;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregordf667e72009-03-10 20:44:00 +0000325 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000326 }
327}
328
Douglas Gregor5e9f35c2009-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 Gregorcca9e962009-07-01 22:01:06 +0000338 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000339 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
340
Douglas Gregorcca9e962009-07-01 22:01:06 +0000341 // This is a template instantiation, so there is no SFINAE.
342 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Douglas Gregor5e9f35c2009-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 Stump1eb44332009-09-09 15:08:12 +0000348
Douglas Gregorcca9e962009-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 Gregor5e9f35c2009-06-14 07:33:30 +0000354 }
355 }
356
357 return false;
358}
359
Douglas Gregor99ebf652009-02-27 19:31:52 +0000360//===----------------------------------------------------------------------===/
361// Template Instantiation for Types
362//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000363namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000364 class VISIBILITY_HIDDEN TemplateInstantiator
365 : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000366 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000367 SourceLocation Loc;
368 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000369
Douglas Gregorcd281c32009-02-28 00:25:32 +0000370 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000371 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
373 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000374 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000375 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000376 DeclarationName Entity)
377 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000378 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000379
Mike Stump1eb44332009-09-09 15:08:12 +0000380 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-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 Gregorff668032009-05-13 18:28:20 +0000387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Douglas Gregor577f75a2009-08-04 16:50:30 +0000389 /// \brief Returns the location of the entity being instantiated, if known.
390 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor577f75a2009-08-04 16:50:30 +0000392 /// \brief Returns the name of the entity being instantiated, if any.
393 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Douglas Gregor577f75a2009-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 Gregorb98b1992009-08-11 05:31:07 +0000398
Mike Stump1eb44332009-09-09 15:08:12 +0000399 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000400 /// instantiating it.
401 Decl *TransformDefinition(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Douglas Gregor43959a92009-08-20 07:17:43 +0000403 /// \brief Rebuild the exception declaration and register the declaration
404 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000405 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000406 DeclaratorInfo *Declarator,
407 IdentifierInfo *Name,
408 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
John McCallc4e70192009-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 Carlsson773f3972009-09-11 01:22:35 +0000414 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000415 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000416
417 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000418 /// substitution of the corresponding template type argument.
419 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
420 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000421}
422
Douglas Gregor577f75a2009-08-04 16:50:30 +0000423Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000424 if (!D)
425 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregorc68afe22009-09-03 21:38:09 +0000427 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-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 Stump1eb44332009-09-09 15:08:12 +0000431 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000432 TTP->getPosition()).getAsDecl());
433 }
Mike Stump1eb44332009-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 Gregord6350ae2009-08-28 20:31:08 +0000437 // template arguments in a function template, but there were some
438 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000439 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000440 TTP->getPosition()))
441 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregord6350ae2009-08-28 20:31:08 +0000443 // FIXME: Implement depth reduction of template template parameters
Mike Stump1eb44332009-09-09 15:08:12 +0000444 assert(false &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000445 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregord1067e52009-08-06 06:41:21 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Douglas Gregore95b4092009-09-16 18:34:49 +0000448 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000449}
450
Douglas Gregor43959a92009-08-20 07:17:43 +0000451Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000452 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000453 if (!Inst)
454 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Douglas Gregor43959a92009-08-20 07:17:43 +0000456 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
457 return Inst;
458}
459
460VarDecl *
461TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000462 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000463 DeclaratorInfo *Declarator,
464 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000465 SourceLocation Loc,
Douglas Gregor43959a92009-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 McCallc4e70192009-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 Carlsson773f3972009-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 McCall0953e762009-09-24 19:53:00 +0000515 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-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 Gregorb98b1992009-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 Gregord6350ae2009-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 Stump1eb44332009-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 Gregorb98b1992009-08-11 05:31:07 +0000535 // template arguments in a function template, but there were some
536 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000537 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000538 NTTP->getPosition()))
539 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000540
541 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000542 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Douglas Gregorb98b1992009-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 Gregord6350ae2009-08-28 20:31:08 +0000547 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregorb98b1992009-08-11 05:31:07 +0000549 if (Arg.getKind() == TemplateArgument::Declaration) {
550 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Douglas Gregore95b4092009-09-16 18:34:49 +0000552 VD = cast_or_null<ValueDecl>(
553 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregord6350ae2009-08-28 20:31:08 +0000554 if (!VD)
555 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000556
557 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000558 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Douglas Gregorb98b1992009-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 Gregord6350ae2009-08-28 20:31:08 +0000565 Arg.getAsIntegral()->getZExtValue(),
566 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000567 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000568 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000569 if (T->isBooleanType())
570 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000571 Arg.getAsIntegral()->getBoolValue(),
Mike Stump1eb44332009-09-09 15:08:12 +0000572 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000573 E->getSourceRange().getBegin()));
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Douglas Gregorb98b1992009-08-11 05:31:07 +0000575 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
576 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000577 *Arg.getAsIntegral(),
Mike Stump1eb44332009-09-09 15:08:12 +0000578 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000579 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Douglas Gregore95b4092009-09-16 18:34:49 +0000582 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000583 if (!InstD)
584 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Anders Carlsson0d8df782009-08-29 19:37:28 +0000586 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000587 // we need to get the underlying decl.
Anders Carlsson0d8df782009-08-29 19:37:28 +0000588 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
589 InstD = InstD->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregorb98b1992009-08-11 05:31:07 +0000591 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +0000592 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregorb98b1992009-08-11 05:31:07 +0000593 /*FIXME:*/false,
Mike Stump1eb44332009-09-09 15:08:12 +0000594 /*FIXME:*/0,
595 /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000596}
597
Mike Stump1eb44332009-09-09 15:08:12 +0000598QualType
Douglas Gregor577f75a2009-08-04 16:50:30 +0000599TemplateInstantiator::TransformTemplateTypeParmType(
600 const TemplateTypeParmType *T) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000601 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000602 // Replace the template type parameter with its corresponding
603 // template argument.
Mike Stump1eb44332009-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 Gregor16134c62009-07-01 00:28:38 +0000608 // arguments left unspecified.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000609 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
610 return QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
612 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000613 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000614 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000615
616 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Mike Stump1eb44332009-09-09 15:08:12 +0000617 }
Douglas Gregor99ebf652009-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 Gregord6350ae2009-08-28 20:31:08 +0000623 return getSema().Context.getTemplateTypeParmType(
624 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor577f75a2009-08-04 16:50:30 +0000625 T->getIndex(),
626 T->isParameterPack(),
627 T->getName());
Douglas Gregorcd281c32009-02-28 00:25:32 +0000628}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000629
John McCallce3ff2b2009-08-25 22:02:44 +0000630/// \brief Perform substitution on the type T with a given set of template
631/// arguments.
Douglas Gregor99ebf652009-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 Gregor99ebf652009-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 Stump1eb44332009-09-09 15:08:12 +0000657QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000658 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000659 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000660 assert(!ActiveTemplateInstantiations.empty() &&
661 "Cannot perform an instantiation without some context on the "
662 "instantiation stack");
663
Douglas Gregor99ebf652009-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 Gregor577f75a2009-08-04 16:50:30 +0000668 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
669 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000670}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000671
John McCallce3ff2b2009-08-25 22:02:44 +0000672/// \brief Perform substitution on the base class specifiers of the
673/// given class template specialization.
Douglas Gregor2943aed2009-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 Stump1eb44332009-09-09 15:08:12 +0000678bool
John McCallce3ff2b2009-08-25 22:02:44 +0000679Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
680 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000681 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000682 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000683 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000684 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000685 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000686 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000687 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000688 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000689 continue;
690 }
691
Mike Stump1eb44332009-09-09 15:08:12 +0000692 QualType BaseType = SubstType(Base->getType(),
693 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000694 Base->getSourceRange().getBegin(),
695 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000696 if (BaseType.isNull()) {
697 Invalid = true;
698 continue;
699 }
700
701 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000702 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-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 Gregor27b152f2009-03-10 18:52:44 +0000714 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000715 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000716 InstantiatedBases.size()))
717 Invalid = true;
718
719 return Invalid;
720}
721
Douglas Gregord475b8d2009-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 Gregord0e3daf2009-09-04 22:48:11 +0000738/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-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 Gregord475b8d2009-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 Gregord6350ae2009-08-28 20:31:08 +0000747 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000748 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000749 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000750 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +0000751
Mike Stump1eb44332009-09-09 15:08:12 +0000752 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +0000753 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
754 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +0000755 if (!Complain) {
756 // Say nothing
757 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-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 Gregor93dfdb12009-05-13 00:25:59 +0000763 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000764 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-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 Gregor454885e2009-10-15 15:54:05 +0000772 // \brief Record the point of instantiation.
773 if (MemberSpecializationInfo *MSInfo
774 = Instantiation->getMemberSpecializationInfo()) {
775 MSInfo->setTemplateSpecializationKind(TSK);
776 MSInfo->setPointOfInstantiation(PointOfInstantiation);
777 }
778
Douglas Gregord048bb72009-03-25 21:23:52 +0000779 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000780 if (Inst)
781 return true;
782
783 // Enter the scope of this instantiation. We don't use
784 // PushDeclContext because we don't have a scope.
785 DeclContext *PreviousContext = CurContext;
786 CurContext = Instantiation;
787
788 // Start the definition of this instantiation.
789 Instantiation->startDefinition();
790
John McCallce3ff2b2009-08-25 22:02:44 +0000791 // Do substitution on the base class specifiers.
792 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000793 Invalid = true;
794
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000795 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000796 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000797 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000798 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +0000799 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000800 if (NewMember) {
801 if (NewMember->isInvalidDecl())
802 Invalid = true;
803 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000804 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +0000805 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
806 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000807 } else {
808 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000809 // instantiations was a semantic disaster, and we'll want to set Invalid =
810 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000811 }
812 }
813
814 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000815 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000816 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000817 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000818 if (Instantiation->isInvalidDecl())
819 Invalid = true;
820
Douglas Gregord475b8d2009-03-25 21:17:03 +0000821 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +0000822 if (!Invalid)
823 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000824
825 // Exit the scope of this instantiation.
826 CurContext = PreviousContext;
827
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000828 if (!Invalid)
829 Consumer.HandleTagDeclDefinition(Instantiation);
830
Douglas Gregora58861f2009-05-13 20:28:22 +0000831 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000832 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000833 Inst.Clear();
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000834 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
835 TSK);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000836 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000837
Douglas Gregord475b8d2009-03-25 21:17:03 +0000838 return Invalid;
839}
840
Mike Stump1eb44332009-09-09 15:08:12 +0000841bool
Douglas Gregor2943aed2009-03-03 04:44:36 +0000842Sema::InstantiateClassTemplateSpecialization(
843 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000844 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000845 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000846 // Perform the actual instantiation on the canonical declaration.
847 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000848 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000849
Douglas Gregor52604ab2009-09-11 21:19:12 +0000850 // Check whether we have already instantiated or specialized this class
851 // template specialization.
852 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
853 if (ClassTemplateSpec->getSpecializationKind() ==
854 TSK_ExplicitInstantiationDeclaration &&
855 TSK == TSK_ExplicitInstantiationDefinition) {
856 // An explicit instantiation definition follows an explicit instantiation
857 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
858 // explicit instantiation.
859 ClassTemplateSpec->setSpecializationKind(TSK);
860 InstantiateClassTemplateSpecializationMembers(
861 /*FIXME?*/ClassTemplateSpec->getPointOfInstantiation(),
862 ClassTemplateSpec,
863 TSK);
864 return false;
865 }
866
867 // We can only instantiate something that hasn't already been
868 // instantiated or specialized. Fail without any diagnostics: our
869 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000870 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +0000871 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000872
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000873 if (ClassTemplateSpec->isInvalidDecl())
874 return true;
875
Douglas Gregor2943aed2009-03-03 04:44:36 +0000876 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000877 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000878
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000879 // C++ [temp.class.spec.match]p1:
880 // When a class template is used in a context that requires an
881 // instantiation of the class, it is necessary to determine
882 // whether the instantiation is to be generated using the primary
883 // template or one of the partial specializations. This is done by
884 // matching the template arguments of the class template
885 // specialization with the template argument lists of the partial
886 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000887 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
888 TemplateArgumentList *> MatchResult;
889 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +0000890 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000891 Partial = Template->getPartialSpecializations().begin(),
892 PartialEnd = Template->getPartialSpecializations().end();
893 Partial != PartialEnd;
894 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000895 TemplateDeductionInfo Info(Context);
896 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000897 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000898 ClassTemplateSpec->getTemplateArgs(),
899 Info)) {
900 // FIXME: Store the failed-deduction information for use in
901 // diagnostics, later.
902 (void)Result;
903 } else {
904 Matched.push_back(std::make_pair(&*Partial, Info.take()));
905 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000906 }
907
908 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000909 // -- If exactly one matching specialization is found, the
910 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000911 Pattern = Matched[0].first;
Douglas Gregor37d93e92009-08-02 23:24:31 +0000912 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000913 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000914 // -- If more than one matching specialization is found, the
915 // partial order rules (14.5.4.2) are used to determine
916 // whether one of the specializations is more specialized
917 // than the others. If none of the specializations is more
918 // specialized than all of the other matching
919 // specializations, then the use of the class template is
920 // ambiguous and the program is ill-formed.
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000921 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
922 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
923 PEnd = Matched.end();
924 P != PEnd; ++P) {
925 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
926 == P->first)
927 Best = P;
928 }
929
930 // Determine if the best partial specialization is more specialized than
931 // the others.
932 bool Ambiguous = false;
933 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
934 PEnd = Matched.end();
935 P != PEnd; ++P) {
936 if (P != Best &&
937 getMoreSpecializedPartialSpecialization(P->first, Best->first)
938 != Best->first) {
939 Ambiguous = true;
940 break;
941 }
942 }
943
944 if (Ambiguous) {
945 // Partial ordering did not produce a clear winner. Complain.
946 ClassTemplateSpec->setInvalidDecl();
947 Diag(ClassTemplateSpec->getPointOfInstantiation(),
948 diag::err_partial_spec_ordering_ambiguous)
949 << ClassTemplateSpec;
950
951 // Print the matching partial specializations.
952 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
953 PEnd = Matched.end();
954 P != PEnd; ++P)
955 Diag(P->first->getLocation(), diag::note_partial_spec_match)
956 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
957 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +0000958
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000959 return true;
960 }
961
962 // Instantiate using the best class template partial specialization.
963 Pattern = Best->first;
964 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000965 } else {
966 // -- If no matches are found, the instantiation is generated
967 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000968 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000969 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
970 // If we've found an explicit specialization of this class template,
971 // stop here and use that as the pattern.
972 if (OrigTemplate->isMemberSpecialization())
973 break;
974
Douglas Gregord6350ae2009-08-28 20:31:08 +0000975 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000976 }
977
Douglas Gregord6350ae2009-08-28 20:31:08 +0000978 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000979 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000980
Douglas Gregord6350ae2009-08-28 20:31:08 +0000981 // Note that this is an instantiation.
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000982 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor2943aed2009-03-03 04:44:36 +0000983
John McCall9cc78072009-09-11 07:25:08 +0000984 bool Result = InstantiateClass(ClassTemplateSpec->getPointOfInstantiation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000985 ClassTemplateSpec, Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000986 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000987 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000988 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregor199d9912009-06-05 00:53:49 +0000990 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
991 // FIXME: Implement TemplateArgumentList::Destroy!
992 // if (Matched[I].first != Pattern)
993 // Matched[I].second->Destroy(Context);
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Douglas Gregor199d9912009-06-05 00:53:49 +0000996 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +0000997}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000998
John McCallce3ff2b2009-08-25 22:02:44 +0000999/// \brief Instantiates the definitions of all of the member
1000/// of the given class, which is an instantiation of a class template
1001/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001002void
1003Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001004 CXXRecordDecl *Instantiation,
1005 const MultiLevelTemplateArgumentList &TemplateArgs,
1006 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001007 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1008 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001009 D != DEnd; ++D) {
1010 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001011 if (Function->getInstantiatedFromMemberFunction()) {
1012 // If this member was explicitly specialized, do nothing.
1013 if (Function->getTemplateSpecializationKind() ==
1014 TSK_ExplicitSpecialization)
1015 continue;
1016
Douglas Gregor2db32322009-10-07 23:56:10 +00001017 Function->setTemplateSpecializationKind(TSK);
Douglas Gregorf6b11852009-10-08 15:14:33 +00001018 }
1019
1020 if (!Function->getBody() && TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001021 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +00001022 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001023 if (Var->isStaticDataMember()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001024 // If this member was explicitly specialized, do nothing.
1025 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1026 continue;
1027
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001028 Var->setTemplateSpecializationKind(TSK);
1029
Douglas Gregorf6b11852009-10-08 15:14:33 +00001030 if (TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001031 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
1032 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001033 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001034 if (Record->isInjectedClassName())
1035 continue;
1036
1037 assert(Record->getInstantiatedFromMemberClass() &&
1038 "Missing instantiated-from-template information");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001039
1040 // If this member was explicitly specialized, do nothing.
1041 if (Record->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1042 continue;
1043
Douglas Gregor2db32322009-10-07 23:56:10 +00001044 if (!Record->getDefinition(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001045 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +00001046 Record->getInstantiatedFromMemberClass(),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001047 TemplateArgs,
1048 TSK);
Douglas Gregore9374d52009-10-08 01:19:17 +00001049
1050 InstantiateClassMembers(PointOfInstantiation, Record, TemplateArgs,
1051 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001052 }
1053 }
1054}
1055
1056/// \brief Instantiate the definitions of all of the members of the
1057/// given class template specialization, which was named as part of an
1058/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001059void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001060Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001061 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001062 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1063 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001064 // C++0x [temp.explicit]p7:
1065 // An explicit instantiation that names a class template
1066 // specialization is an explicit instantion of the same kind
1067 // (declaration or definition) of each of its members (not
1068 // including members inherited from base classes) that has not
1069 // been previously explicitly specialized in the translation unit
1070 // containing the explicit instantiation, except as described
1071 // below.
1072 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001073 getTemplateInstantiationArgs(ClassTemplateSpec),
1074 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001075}
1076
Mike Stump1eb44332009-09-09 15:08:12 +00001077Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001078Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001079 if (!S)
1080 return Owned(S);
1081
1082 TemplateInstantiator Instantiator(*this, TemplateArgs,
1083 SourceLocation(),
1084 DeclarationName());
1085 return Instantiator.TransformStmt(S);
1086}
1087
Mike Stump1eb44332009-09-09 15:08:12 +00001088Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001089Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001090 if (!E)
1091 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Douglas Gregorb98b1992009-08-11 05:31:07 +00001093 TemplateInstantiator Instantiator(*this, TemplateArgs,
1094 SourceLocation(),
1095 DeclarationName());
1096 return Instantiator.TransformExpr(E);
1097}
1098
John McCallce3ff2b2009-08-25 22:02:44 +00001099/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001100NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001101Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001102 SourceRange Range,
1103 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001104 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1105 DeclarationName());
1106 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001107}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001108
1109TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001110Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001111 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001112 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1113 DeclarationName());
1114 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001115}
Douglas Gregor91333002009-06-11 00:06:24 +00001116
Mike Stump1eb44332009-09-09 15:08:12 +00001117TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001118 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001119 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1120 DeclarationName());
1121 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregor91333002009-06-11 00:06:24 +00001122}