blob: 1790d27eeb34a61a66c727b9670648f9a66754c5 [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 Gregor6cd21982009-10-20 05:58:46 +0000403 /// \bried Transform the first qualifier within a scope by instantiating the
404 /// declaration.
405 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
406
Douglas Gregor43959a92009-08-20 07:17:43 +0000407 /// \brief Rebuild the exception declaration and register the declaration
408 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000409 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000410 DeclaratorInfo *Declarator,
411 IdentifierInfo *Name,
412 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000413
John McCallc4e70192009-09-11 04:59:25 +0000414 /// \brief Check for tag mismatches when instantiating an
415 /// elaborated type.
416 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
417
Anders Carlsson773f3972009-09-11 01:22:35 +0000418 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000419 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
421 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000422 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000423 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
424 TemplateTypeParmTypeLoc TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000425 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000426}
427
Douglas Gregor577f75a2009-08-04 16:50:30 +0000428Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000429 if (!D)
430 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregorc68afe22009-09-03 21:38:09 +0000432 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000433 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
434 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
435 "Wrong kind of template template argument");
Mike Stump1eb44332009-09-09 15:08:12 +0000436 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000437 TTP->getPosition()).getAsDecl());
438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
440 // If the corresponding template argument is NULL or non-existent, it's
441 // because we are performing instantiation from explicitly-specified
Douglas Gregord6350ae2009-08-28 20:31:08 +0000442 // template arguments in a function template, but there were some
443 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000444 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000445 TTP->getPosition()))
446 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Douglas Gregord6350ae2009-08-28 20:31:08 +0000448 // FIXME: Implement depth reduction of template template parameters
Mike Stump1eb44332009-09-09 15:08:12 +0000449 assert(false &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000450 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregord1067e52009-08-06 06:41:21 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregore95b4092009-09-16 18:34:49 +0000453 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000454}
455
Douglas Gregor43959a92009-08-20 07:17:43 +0000456Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000457 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000458 if (!Inst)
459 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Douglas Gregor43959a92009-08-20 07:17:43 +0000461 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
462 return Inst;
463}
464
Douglas Gregor6cd21982009-10-20 05:58:46 +0000465NamedDecl *
466TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
467 SourceLocation Loc) {
468 // If the first part of the nested-name-specifier was a template type
469 // parameter, instantiate that type parameter down to a tag type.
470 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
471 const TemplateTypeParmType *TTP
472 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
473 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
474 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
475 if (T.isNull())
476 return cast_or_null<NamedDecl>(TransformDecl(D));
477
478 if (const TagType *Tag = T->getAs<TagType>())
479 return Tag->getDecl();
480
481 // The resulting type is not a tag; complain.
482 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
483 return 0;
484 }
485 }
486
487 return cast_or_null<NamedDecl>(TransformDecl(D));
488}
489
Douglas Gregor43959a92009-08-20 07:17:43 +0000490VarDecl *
491TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000492 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000493 DeclaratorInfo *Declarator,
494 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000495 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000496 SourceRange TypeRange) {
497 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
498 Name, Loc, TypeRange);
499 if (Var && !Var->isInvalidDecl())
500 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
501 return Var;
502}
503
John McCallc4e70192009-09-11 04:59:25 +0000504QualType
505TemplateInstantiator::RebuildElaboratedType(QualType T,
506 ElaboratedType::TagKind Tag) {
507 if (const TagType *TT = T->getAs<TagType>()) {
508 TagDecl* TD = TT->getDecl();
509
510 // FIXME: this location is very wrong; we really need typelocs.
511 SourceLocation TagLocation = TD->getTagKeywordLoc();
512
513 // FIXME: type might be anonymous.
514 IdentifierInfo *Id = TD->getIdentifier();
515
516 // TODO: should we even warn on struct/class mismatches for this? Seems
517 // like it's likely to produce a lot of spurious errors.
518 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
519 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
520 << Id
521 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
522 TD->getKindName());
523 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
524 }
525 }
526
527 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
528}
529
530Sema::OwningExprResult
Anders Carlsson773f3972009-09-11 01:22:35 +0000531TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
532 if (!E->isTypeDependent())
533 return SemaRef.Owned(E->Retain());
534
535 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
536 assert(currentDecl && "Must have current function declaration when "
537 "instantiating.");
538
539 PredefinedExpr::IdentType IT = E->getIdentType();
540
541 unsigned Length =
542 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
543
544 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000545 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000546 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
547 ArrayType::Normal, 0);
548 PredefinedExpr *PE =
549 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
550 return getSema().Owned(PE);
551}
552
553Sema::OwningExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +0000554TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
555 // FIXME: Clean this up a bit
556 NamedDecl *D = E->getDecl();
557 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000558 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
559 assert(false && "Cannot reduce non-type template parameter depth yet");
560 return getSema().ExprError();
561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
563 // If the corresponding template argument is NULL or non-existent, it's
564 // because we are performing instantiation from explicitly-specified
Douglas Gregorb98b1992009-08-11 05:31:07 +0000565 // template arguments in a function template, but there were some
566 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000567 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000568 NTTP->getPosition()))
569 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000570
571 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000572 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Douglas Gregorb98b1992009-08-11 05:31:07 +0000574 // The template argument itself might be an expression, in which
575 // case we just return that expression.
576 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregord6350ae2009-08-28 20:31:08 +0000577 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Douglas Gregorb98b1992009-08-11 05:31:07 +0000579 if (Arg.getKind() == TemplateArgument::Declaration) {
580 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Douglas Gregore95b4092009-09-16 18:34:49 +0000582 VD = cast_or_null<ValueDecl>(
583 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregord6350ae2009-08-28 20:31:08 +0000584 if (!VD)
585 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000586
587 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000588 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregorb98b1992009-08-11 05:31:07 +0000591 assert(Arg.getKind() == TemplateArgument::Integral);
592 QualType T = Arg.getIntegralType();
593 if (T->isCharType() || T->isWideCharType())
594 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000595 Arg.getAsIntegral()->getZExtValue(),
596 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000597 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000598 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000599 if (T->isBooleanType())
600 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000601 Arg.getAsIntegral()->getBoolValue(),
Mike Stump1eb44332009-09-09 15:08:12 +0000602 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000603 E->getSourceRange().getBegin()));
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregorb98b1992009-08-11 05:31:07 +0000605 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
606 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000607 *Arg.getAsIntegral(),
Mike Stump1eb44332009-09-09 15:08:12 +0000608 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000609 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregore95b4092009-09-16 18:34:49 +0000612 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000613 if (!InstD)
614 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Anders Carlsson0d8df782009-08-29 19:37:28 +0000616 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000617 // we need to get the underlying decl.
Anders Carlsson0d8df782009-08-29 19:37:28 +0000618 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
619 InstD = InstD->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Douglas Gregora2813ce2009-10-23 18:54:35 +0000621 CXXScopeSpec SS;
622 NestedNameSpecifier *Qualifier = 0;
623 if (E->getQualifier()) {
624 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
625 E->getQualifierRange());
626 if (!Qualifier)
627 return SemaRef.ExprError();
628
629 SS.setScopeRep(Qualifier);
630 SS.setRange(E->getQualifierRange());
631 }
632
Mike Stump1eb44332009-09-09 15:08:12 +0000633 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregorb98b1992009-08-11 05:31:07 +0000634 /*FIXME:*/false,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000635 &SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000636 /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000637}
638
Mike Stump1eb44332009-09-09 15:08:12 +0000639QualType
John McCalla2becad2009-10-21 00:40:46 +0000640TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
641 TemplateTypeParmTypeLoc TL) {
642 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000643 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000644 // Replace the template type parameter with its corresponding
645 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000646
647 // If the corresponding template argument is NULL or doesn't exist, it's
648 // because we are performing instantiation from explicitly-specified
649 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000650 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000651 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
652 TemplateTypeParmTypeLoc NewTL
653 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
654 NewTL.setNameLoc(TL.getNameLoc());
655 return TL.getType();
656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
658 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000659 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000660 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000661
John McCall49a832b2009-10-18 09:09:24 +0000662 QualType Replacement
663 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
664
665 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000666 QualType Result
667 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
668 SubstTemplateTypeParmTypeLoc NewTL
669 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
670 NewTL.setNameLoc(TL.getNameLoc());
671 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000672 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000673
674 // The template type parameter comes from an inner template (e.g.,
675 // the template parameter list of a member template inside the
676 // template we are instantiating). Create a new template type
677 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000678 QualType Result
679 = getSema().Context.getTemplateTypeParmType(T->getDepth()
680 - TemplateArgs.getNumLevels(),
681 T->getIndex(),
682 T->isParameterPack(),
683 T->getName());
684 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
685 NewTL.setNameLoc(TL.getNameLoc());
686 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000687}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000688
John McCallce3ff2b2009-08-25 22:02:44 +0000689/// \brief Perform substitution on the type T with a given set of template
690/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000691///
692/// This routine substitutes the given template arguments into the
693/// type T and produces the instantiated type.
694///
695/// \param T the type into which the template arguments will be
696/// substituted. If this type is not dependent, it will be returned
697/// immediately.
698///
699/// \param TemplateArgs the template arguments that will be
700/// substituted for the top-level template parameters within T.
701///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000702/// \param Loc the location in the source code where this substitution
703/// is being performed. It will typically be the location of the
704/// declarator (if we're instantiating the type of some declaration)
705/// or the location of the type in the source code (if, e.g., we're
706/// instantiating the type of a cast expression).
707///
708/// \param Entity the name of the entity associated with a declaration
709/// being instantiated (if any). May be empty to indicate that there
710/// is no such entity (if, e.g., this is a type that occurs as part of
711/// a cast expression) or that the entity has no name (e.g., an
712/// unnamed function parameter).
713///
714/// \returns If the instantiation succeeds, the instantiated
715/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallcd7ba1c2009-10-21 00:58:09 +0000716DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
717 const MultiLevelTemplateArgumentList &Args,
718 SourceLocation Loc,
719 DeclarationName Entity) {
720 assert(!ActiveTemplateInstantiations.empty() &&
721 "Cannot perform an instantiation without some context on the "
722 "instantiation stack");
723
724 if (!T->getType()->isDependentType())
725 return T;
726
727 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
728 return Instantiator.TransformType(T);
729}
730
731/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000732QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000733 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000734 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000735 assert(!ActiveTemplateInstantiations.empty() &&
736 "Cannot perform an instantiation without some context on the "
737 "instantiation stack");
738
Douglas Gregor99ebf652009-02-27 19:31:52 +0000739 // If T is not a dependent type, there is nothing to do.
740 if (!T->isDependentType())
741 return T;
742
Douglas Gregor577f75a2009-08-04 16:50:30 +0000743 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
744 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000745}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000746
John McCallce3ff2b2009-08-25 22:02:44 +0000747/// \brief Perform substitution on the base class specifiers of the
748/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000749///
750/// Produces a diagnostic and returns true on error, returns false and
751/// attaches the instantiated base classes to the class template
752/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000753bool
John McCallce3ff2b2009-08-25 22:02:44 +0000754Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
755 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000756 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000757 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000758 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000759 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000760 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000761 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000762 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000763 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000764 continue;
765 }
766
Mike Stump1eb44332009-09-09 15:08:12 +0000767 QualType BaseType = SubstType(Base->getType(),
768 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000769 Base->getSourceRange().getBegin(),
770 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000771 if (BaseType.isNull()) {
772 Invalid = true;
773 continue;
774 }
775
776 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000777 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000778 Base->getSourceRange(),
779 Base->isVirtual(),
780 Base->getAccessSpecifierAsWritten(),
781 BaseType,
782 /*FIXME: Not totally accurate */
783 Base->getSourceRange().getBegin()))
784 InstantiatedBases.push_back(InstantiatedBase);
785 else
786 Invalid = true;
787 }
788
Douglas Gregor27b152f2009-03-10 18:52:44 +0000789 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000790 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000791 InstantiatedBases.size()))
792 Invalid = true;
793
794 return Invalid;
795}
796
Douglas Gregord475b8d2009-03-25 21:17:03 +0000797/// \brief Instantiate the definition of a class from a given pattern.
798///
799/// \param PointOfInstantiation The point of instantiation within the
800/// source code.
801///
802/// \param Instantiation is the declaration whose definition is being
803/// instantiated. This will be either a class template specialization
804/// or a member class of a class template specialization.
805///
806/// \param Pattern is the pattern from which the instantiation
807/// occurs. This will be either the declaration of a class template or
808/// the declaration of a member class of a class template.
809///
810/// \param TemplateArgs The template arguments to be substituted into
811/// the pattern.
812///
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000813/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +0000814///
815/// \param Complain whether to complain if the class cannot be instantiated due
816/// to the lack of a definition.
817///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000818/// \returns true if an error occurred, false otherwise.
819bool
820Sema::InstantiateClass(SourceLocation PointOfInstantiation,
821 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000822 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000823 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000824 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000825 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +0000826
Mike Stump1eb44332009-09-09 15:08:12 +0000827 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +0000828 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
829 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +0000830 if (!Complain) {
831 // Say nothing
832 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000833 Diag(PointOfInstantiation,
834 diag::err_implicit_instantiate_member_undefined)
835 << Context.getTypeDeclType(Instantiation);
836 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
837 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000838 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000839 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +0000840 << Context.getTypeDeclType(Instantiation);
841 Diag(Pattern->getLocation(), diag::note_template_decl_here);
842 }
843 return true;
844 }
845 Pattern = PatternDef;
846
Douglas Gregor454885e2009-10-15 15:54:05 +0000847 // \brief Record the point of instantiation.
848 if (MemberSpecializationInfo *MSInfo
849 = Instantiation->getMemberSpecializationInfo()) {
850 MSInfo->setTemplateSpecializationKind(TSK);
851 MSInfo->setPointOfInstantiation(PointOfInstantiation);
852 }
853
Douglas Gregord048bb72009-03-25 21:23:52 +0000854 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000855 if (Inst)
856 return true;
857
858 // Enter the scope of this instantiation. We don't use
859 // PushDeclContext because we don't have a scope.
860 DeclContext *PreviousContext = CurContext;
861 CurContext = Instantiation;
862
863 // Start the definition of this instantiation.
864 Instantiation->startDefinition();
865
John McCallce3ff2b2009-08-25 22:02:44 +0000866 // Do substitution on the base class specifiers.
867 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000868 Invalid = true;
869
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000870 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000871 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000872 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000873 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +0000874 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000875 if (NewMember) {
876 if (NewMember->isInvalidDecl())
877 Invalid = true;
878 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000879 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +0000880 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
881 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000882 } else {
883 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000884 // instantiations was a semantic disaster, and we'll want to set Invalid =
885 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000886 }
887 }
888
889 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000890 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000891 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000892 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000893 if (Instantiation->isInvalidDecl())
894 Invalid = true;
895
Douglas Gregord475b8d2009-03-25 21:17:03 +0000896 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +0000897 if (!Invalid)
898 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000899
900 // Exit the scope of this instantiation.
901 CurContext = PreviousContext;
902
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000903 if (!Invalid)
904 Consumer.HandleTagDeclDefinition(Instantiation);
905
Douglas Gregora58861f2009-05-13 20:28:22 +0000906 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000907 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000908 Inst.Clear();
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000909 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
910 TSK);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000911 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000912
Douglas Gregord475b8d2009-03-25 21:17:03 +0000913 return Invalid;
914}
915
Mike Stump1eb44332009-09-09 15:08:12 +0000916bool
Douglas Gregor2943aed2009-03-03 04:44:36 +0000917Sema::InstantiateClassTemplateSpecialization(
918 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000919 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000920 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000921 // Perform the actual instantiation on the canonical declaration.
922 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000923 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000924
Douglas Gregor52604ab2009-09-11 21:19:12 +0000925 // Check whether we have already instantiated or specialized this class
926 // template specialization.
927 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
928 if (ClassTemplateSpec->getSpecializationKind() ==
929 TSK_ExplicitInstantiationDeclaration &&
930 TSK == TSK_ExplicitInstantiationDefinition) {
931 // An explicit instantiation definition follows an explicit instantiation
932 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
933 // explicit instantiation.
934 ClassTemplateSpec->setSpecializationKind(TSK);
935 InstantiateClassTemplateSpecializationMembers(
936 /*FIXME?*/ClassTemplateSpec->getPointOfInstantiation(),
937 ClassTemplateSpec,
938 TSK);
939 return false;
940 }
941
942 // We can only instantiate something that hasn't already been
943 // instantiated or specialized. Fail without any diagnostics: our
944 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000945 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +0000946 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000947
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000948 if (ClassTemplateSpec->isInvalidDecl())
949 return true;
950
Douglas Gregor2943aed2009-03-03 04:44:36 +0000951 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000952 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000953
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000954 // C++ [temp.class.spec.match]p1:
955 // When a class template is used in a context that requires an
956 // instantiation of the class, it is necessary to determine
957 // whether the instantiation is to be generated using the primary
958 // template or one of the partial specializations. This is done by
959 // matching the template arguments of the class template
960 // specialization with the template argument lists of the partial
961 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000962 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
963 TemplateArgumentList *> MatchResult;
964 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +0000965 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000966 Partial = Template->getPartialSpecializations().begin(),
967 PartialEnd = Template->getPartialSpecializations().end();
968 Partial != PartialEnd;
969 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000970 TemplateDeductionInfo Info(Context);
971 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000972 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000973 ClassTemplateSpec->getTemplateArgs(),
974 Info)) {
975 // FIXME: Store the failed-deduction information for use in
976 // diagnostics, later.
977 (void)Result;
978 } else {
979 Matched.push_back(std::make_pair(&*Partial, Info.take()));
980 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000981 }
982
983 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000984 // -- If exactly one matching specialization is found, the
985 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000986 Pattern = Matched[0].first;
Douglas Gregor37d93e92009-08-02 23:24:31 +0000987 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000988 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000989 // -- If more than one matching specialization is found, the
990 // partial order rules (14.5.4.2) are used to determine
991 // whether one of the specializations is more specialized
992 // than the others. If none of the specializations is more
993 // specialized than all of the other matching
994 // specializations, then the use of the class template is
995 // ambiguous and the program is ill-formed.
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000996 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
997 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
998 PEnd = Matched.end();
999 P != PEnd; ++P) {
1000 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1001 == P->first)
1002 Best = P;
1003 }
1004
1005 // Determine if the best partial specialization is more specialized than
1006 // the others.
1007 bool Ambiguous = false;
1008 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1009 PEnd = Matched.end();
1010 P != PEnd; ++P) {
1011 if (P != Best &&
1012 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1013 != Best->first) {
1014 Ambiguous = true;
1015 break;
1016 }
1017 }
1018
1019 if (Ambiguous) {
1020 // Partial ordering did not produce a clear winner. Complain.
1021 ClassTemplateSpec->setInvalidDecl();
1022 Diag(ClassTemplateSpec->getPointOfInstantiation(),
1023 diag::err_partial_spec_ordering_ambiguous)
1024 << ClassTemplateSpec;
1025
1026 // Print the matching partial specializations.
1027 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1028 PEnd = Matched.end();
1029 P != PEnd; ++P)
1030 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1031 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1032 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001033
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001034 return true;
1035 }
1036
1037 // Instantiate using the best class template partial specialization.
1038 Pattern = Best->first;
1039 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001040 } else {
1041 // -- If no matches are found, the instantiation is generated
1042 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001043 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001044 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1045 // If we've found an explicit specialization of this class template,
1046 // stop here and use that as the pattern.
1047 if (OrigTemplate->isMemberSpecialization())
1048 break;
1049
Douglas Gregord6350ae2009-08-28 20:31:08 +00001050 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001051 }
1052
Douglas Gregord6350ae2009-08-28 20:31:08 +00001053 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001054 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001055
Douglas Gregord6350ae2009-08-28 20:31:08 +00001056 // Note that this is an instantiation.
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001057 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001058
John McCall9cc78072009-09-11 07:25:08 +00001059 bool Result = InstantiateClass(ClassTemplateSpec->getPointOfInstantiation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001060 ClassTemplateSpec, Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001061 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001062 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001063 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregor199d9912009-06-05 00:53:49 +00001065 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1066 // FIXME: Implement TemplateArgumentList::Destroy!
1067 // if (Matched[I].first != Pattern)
1068 // Matched[I].second->Destroy(Context);
1069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregor199d9912009-06-05 00:53:49 +00001071 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001072}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001073
John McCallce3ff2b2009-08-25 22:02:44 +00001074/// \brief Instantiates the definitions of all of the member
1075/// of the given class, which is an instantiation of a class template
1076/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001077void
1078Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001079 CXXRecordDecl *Instantiation,
1080 const MultiLevelTemplateArgumentList &TemplateArgs,
1081 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001082 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1083 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001084 D != DEnd; ++D) {
1085 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001086 if (Function->getInstantiatedFromMemberFunction()) {
1087 // If this member was explicitly specialized, do nothing.
1088 if (Function->getTemplateSpecializationKind() ==
1089 TSK_ExplicitSpecialization)
1090 continue;
1091
Douglas Gregor86035ae2009-10-15 23:05:15 +00001092 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregorf6b11852009-10-08 15:14:33 +00001093 }
1094
1095 if (!Function->getBody() && TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001096 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +00001097 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001098 if (Var->isStaticDataMember()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001099 // If this member was explicitly specialized, do nothing.
1100 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1101 continue;
1102
Douglas Gregor86035ae2009-10-15 23:05:15 +00001103 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001104
Douglas Gregorf6b11852009-10-08 15:14:33 +00001105 if (TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001106 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
1107 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001108 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001109 if (Record->isInjectedClassName())
1110 continue;
1111
1112 assert(Record->getInstantiatedFromMemberClass() &&
1113 "Missing instantiated-from-template information");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001114
1115 // If this member was explicitly specialized, do nothing.
1116 if (Record->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1117 continue;
1118
Douglas Gregor2db32322009-10-07 23:56:10 +00001119 if (!Record->getDefinition(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001120 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +00001121 Record->getInstantiatedFromMemberClass(),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001122 TemplateArgs,
1123 TSK);
Douglas Gregore9374d52009-10-08 01:19:17 +00001124
1125 InstantiateClassMembers(PointOfInstantiation, Record, TemplateArgs,
1126 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001127 }
1128 }
1129}
1130
1131/// \brief Instantiate the definitions of all of the members of the
1132/// given class template specialization, which was named as part of an
1133/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001134void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001135Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001136 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001137 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1138 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001139 // C++0x [temp.explicit]p7:
1140 // An explicit instantiation that names a class template
1141 // specialization is an explicit instantion of the same kind
1142 // (declaration or definition) of each of its members (not
1143 // including members inherited from base classes) that has not
1144 // been previously explicitly specialized in the translation unit
1145 // containing the explicit instantiation, except as described
1146 // below.
1147 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001148 getTemplateInstantiationArgs(ClassTemplateSpec),
1149 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001150}
1151
Mike Stump1eb44332009-09-09 15:08:12 +00001152Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001153Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001154 if (!S)
1155 return Owned(S);
1156
1157 TemplateInstantiator Instantiator(*this, TemplateArgs,
1158 SourceLocation(),
1159 DeclarationName());
1160 return Instantiator.TransformStmt(S);
1161}
1162
Mike Stump1eb44332009-09-09 15:08:12 +00001163Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001164Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001165 if (!E)
1166 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregorb98b1992009-08-11 05:31:07 +00001168 TemplateInstantiator Instantiator(*this, TemplateArgs,
1169 SourceLocation(),
1170 DeclarationName());
1171 return Instantiator.TransformExpr(E);
1172}
1173
John McCallce3ff2b2009-08-25 22:02:44 +00001174/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001175NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001176Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001177 SourceRange Range,
1178 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001179 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1180 DeclarationName());
1181 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001182}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001183
1184TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001185Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001186 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001187 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1188 DeclarationName());
1189 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001190}
Douglas Gregor91333002009-06-11 00:06:24 +00001191
Mike Stump1eb44332009-09-09 15:08:12 +00001192TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001193 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001194 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1195 DeclarationName());
1196 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregor91333002009-06-11 00:06:24 +00001197}