blob: 14930134dad615f056113b50d004946b48d9bd24 [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 Gregor972e6ce2009-10-27 06:26:26 +0000395 /// \brief Sets the "base" location and entity when that
396 /// information is known based on another transformation.
397 void setBase(SourceLocation Loc, DeclarationName Entity) {
398 this->Loc = Loc;
399 this->Entity = Entity;
400 }
401
Douglas Gregor577f75a2009-08-04 16:50:30 +0000402 /// \brief Transform the given declaration by instantiating a reference to
403 /// this declaration.
404 Decl *TransformDecl(Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000405
Mike Stump1eb44332009-09-09 15:08:12 +0000406 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000407 /// instantiating it.
408 Decl *TransformDefinition(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor6cd21982009-10-20 05:58:46 +0000410 /// \bried Transform the first qualifier within a scope by instantiating the
411 /// declaration.
412 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
413
Douglas Gregor43959a92009-08-20 07:17:43 +0000414 /// \brief Rebuild the exception declaration and register the declaration
415 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000416 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000417 DeclaratorInfo *Declarator,
418 IdentifierInfo *Name,
419 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
John McCallc4e70192009-09-11 04:59:25 +0000421 /// \brief Check for tag mismatches when instantiating an
422 /// elaborated type.
423 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
424
Anders Carlsson773f3972009-09-11 01:22:35 +0000425 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000426 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
428 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000429 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000430 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
431 TemplateTypeParmTypeLoc TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000432 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000433}
434
Douglas Gregor577f75a2009-08-04 16:50:30 +0000435Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000436 if (!D)
437 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Douglas Gregorc68afe22009-09-03 21:38:09 +0000439 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000440 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
441 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
442 "Wrong kind of template template argument");
Mike Stump1eb44332009-09-09 15:08:12 +0000443 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000444 TTP->getPosition()).getAsDecl());
445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
447 // If the corresponding template argument is NULL or non-existent, it's
448 // because we are performing instantiation from explicitly-specified
Douglas Gregord6350ae2009-08-28 20:31:08 +0000449 // template arguments in a function template, but there were some
450 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000451 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000452 TTP->getPosition()))
453 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Douglas Gregord6350ae2009-08-28 20:31:08 +0000455 // FIXME: Implement depth reduction of template template parameters
Mike Stump1eb44332009-09-09 15:08:12 +0000456 assert(false &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000457 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregord1067e52009-08-06 06:41:21 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregore95b4092009-09-16 18:34:49 +0000460 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000461}
462
Douglas Gregor43959a92009-08-20 07:17:43 +0000463Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000464 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000465 if (!Inst)
466 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor43959a92009-08-20 07:17:43 +0000468 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
469 return Inst;
470}
471
Douglas Gregor6cd21982009-10-20 05:58:46 +0000472NamedDecl *
473TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
474 SourceLocation Loc) {
475 // If the first part of the nested-name-specifier was a template type
476 // parameter, instantiate that type parameter down to a tag type.
477 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
478 const TemplateTypeParmType *TTP
479 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
480 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
481 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
482 if (T.isNull())
483 return cast_or_null<NamedDecl>(TransformDecl(D));
484
485 if (const TagType *Tag = T->getAs<TagType>())
486 return Tag->getDecl();
487
488 // The resulting type is not a tag; complain.
489 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
490 return 0;
491 }
492 }
493
494 return cast_or_null<NamedDecl>(TransformDecl(D));
495}
496
Douglas Gregor43959a92009-08-20 07:17:43 +0000497VarDecl *
498TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000499 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000500 DeclaratorInfo *Declarator,
501 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000502 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000503 SourceRange TypeRange) {
504 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
505 Name, Loc, TypeRange);
506 if (Var && !Var->isInvalidDecl())
507 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
508 return Var;
509}
510
John McCallc4e70192009-09-11 04:59:25 +0000511QualType
512TemplateInstantiator::RebuildElaboratedType(QualType T,
513 ElaboratedType::TagKind Tag) {
514 if (const TagType *TT = T->getAs<TagType>()) {
515 TagDecl* TD = TT->getDecl();
516
517 // FIXME: this location is very wrong; we really need typelocs.
518 SourceLocation TagLocation = TD->getTagKeywordLoc();
519
520 // FIXME: type might be anonymous.
521 IdentifierInfo *Id = TD->getIdentifier();
522
523 // TODO: should we even warn on struct/class mismatches for this? Seems
524 // like it's likely to produce a lot of spurious errors.
525 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
526 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
527 << Id
528 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
529 TD->getKindName());
530 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
531 }
532 }
533
534 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
535}
536
537Sema::OwningExprResult
Anders Carlsson773f3972009-09-11 01:22:35 +0000538TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
539 if (!E->isTypeDependent())
540 return SemaRef.Owned(E->Retain());
541
542 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
543 assert(currentDecl && "Must have current function declaration when "
544 "instantiating.");
545
546 PredefinedExpr::IdentType IT = E->getIdentType();
547
548 unsigned Length =
549 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
550
551 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000552 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000553 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
554 ArrayType::Normal, 0);
555 PredefinedExpr *PE =
556 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
557 return getSema().Owned(PE);
558}
559
560Sema::OwningExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +0000561TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
562 // FIXME: Clean this up a bit
563 NamedDecl *D = E->getDecl();
564 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000565 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
566 assert(false && "Cannot reduce non-type template parameter depth yet");
567 return getSema().ExprError();
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
570 // If the corresponding template argument is NULL or non-existent, it's
571 // because we are performing instantiation from explicitly-specified
Douglas Gregorb98b1992009-08-11 05:31:07 +0000572 // template arguments in a function template, but there were some
573 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000574 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000575 NTTP->getPosition()))
576 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000577
578 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000579 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregorb98b1992009-08-11 05:31:07 +0000581 // The template argument itself might be an expression, in which
582 // case we just return that expression.
583 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregord6350ae2009-08-28 20:31:08 +0000584 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Douglas Gregorb98b1992009-08-11 05:31:07 +0000586 if (Arg.getKind() == TemplateArgument::Declaration) {
587 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Douglas Gregore95b4092009-09-16 18:34:49 +0000589 VD = cast_or_null<ValueDecl>(
590 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregord6350ae2009-08-28 20:31:08 +0000591 if (!VD)
592 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000593
594 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000595 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregorb98b1992009-08-11 05:31:07 +0000598 assert(Arg.getKind() == TemplateArgument::Integral);
599 QualType T = Arg.getIntegralType();
600 if (T->isCharType() || T->isWideCharType())
601 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000602 Arg.getAsIntegral()->getZExtValue(),
603 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000604 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000605 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000606 if (T->isBooleanType())
607 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000608 Arg.getAsIntegral()->getBoolValue(),
Mike Stump1eb44332009-09-09 15:08:12 +0000609 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000610 E->getSourceRange().getBegin()));
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregorb98b1992009-08-11 05:31:07 +0000612 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
613 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000614 *Arg.getAsIntegral(),
Mike Stump1eb44332009-09-09 15:08:12 +0000615 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000616 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregore95b4092009-09-16 18:34:49 +0000619 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000620 if (!InstD)
621 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Anders Carlsson0d8df782009-08-29 19:37:28 +0000623 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000624 // we need to get the underlying decl.
Anders Carlsson0d8df782009-08-29 19:37:28 +0000625 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
626 InstD = InstD->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Douglas Gregora2813ce2009-10-23 18:54:35 +0000628 CXXScopeSpec SS;
629 NestedNameSpecifier *Qualifier = 0;
630 if (E->getQualifier()) {
631 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
632 E->getQualifierRange());
633 if (!Qualifier)
634 return SemaRef.ExprError();
635
636 SS.setScopeRep(Qualifier);
637 SS.setRange(E->getQualifierRange());
638 }
639
Mike Stump1eb44332009-09-09 15:08:12 +0000640 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregorb98b1992009-08-11 05:31:07 +0000641 /*FIXME:*/false,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000642 &SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000643 /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000644}
645
Mike Stump1eb44332009-09-09 15:08:12 +0000646QualType
John McCalla2becad2009-10-21 00:40:46 +0000647TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
648 TemplateTypeParmTypeLoc TL) {
649 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000650 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000651 // Replace the template type parameter with its corresponding
652 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000653
654 // If the corresponding template argument is NULL or doesn't exist, it's
655 // because we are performing instantiation from explicitly-specified
656 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000657 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000658 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
659 TemplateTypeParmTypeLoc NewTL
660 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
661 NewTL.setNameLoc(TL.getNameLoc());
662 return TL.getType();
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
665 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000666 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000667 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000668
John McCall49a832b2009-10-18 09:09:24 +0000669 QualType Replacement
670 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
671
672 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000673 QualType Result
674 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
675 SubstTemplateTypeParmTypeLoc NewTL
676 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
677 NewTL.setNameLoc(TL.getNameLoc());
678 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000679 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000680
681 // The template type parameter comes from an inner template (e.g.,
682 // the template parameter list of a member template inside the
683 // template we are instantiating). Create a new template type
684 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000685 QualType Result
686 = getSema().Context.getTemplateTypeParmType(T->getDepth()
687 - TemplateArgs.getNumLevels(),
688 T->getIndex(),
689 T->isParameterPack(),
690 T->getName());
691 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
692 NewTL.setNameLoc(TL.getNameLoc());
693 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000694}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000695
John McCallce3ff2b2009-08-25 22:02:44 +0000696/// \brief Perform substitution on the type T with a given set of template
697/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000698///
699/// This routine substitutes the given template arguments into the
700/// type T and produces the instantiated type.
701///
702/// \param T the type into which the template arguments will be
703/// substituted. If this type is not dependent, it will be returned
704/// immediately.
705///
706/// \param TemplateArgs the template arguments that will be
707/// substituted for the top-level template parameters within T.
708///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000709/// \param Loc the location in the source code where this substitution
710/// is being performed. It will typically be the location of the
711/// declarator (if we're instantiating the type of some declaration)
712/// or the location of the type in the source code (if, e.g., we're
713/// instantiating the type of a cast expression).
714///
715/// \param Entity the name of the entity associated with a declaration
716/// being instantiated (if any). May be empty to indicate that there
717/// is no such entity (if, e.g., this is a type that occurs as part of
718/// a cast expression) or that the entity has no name (e.g., an
719/// unnamed function parameter).
720///
721/// \returns If the instantiation succeeds, the instantiated
722/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallcd7ba1c2009-10-21 00:58:09 +0000723DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
724 const MultiLevelTemplateArgumentList &Args,
725 SourceLocation Loc,
726 DeclarationName Entity) {
727 assert(!ActiveTemplateInstantiations.empty() &&
728 "Cannot perform an instantiation without some context on the "
729 "instantiation stack");
730
731 if (!T->getType()->isDependentType())
732 return T;
733
734 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
735 return Instantiator.TransformType(T);
736}
737
738/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000739QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000740 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000741 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000742 assert(!ActiveTemplateInstantiations.empty() &&
743 "Cannot perform an instantiation without some context on the "
744 "instantiation stack");
745
Douglas Gregor99ebf652009-02-27 19:31:52 +0000746 // If T is not a dependent type, there is nothing to do.
747 if (!T->isDependentType())
748 return T;
749
Douglas Gregor577f75a2009-08-04 16:50:30 +0000750 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
751 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000752}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000753
John McCallce3ff2b2009-08-25 22:02:44 +0000754/// \brief Perform substitution on the base class specifiers of the
755/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000756///
757/// Produces a diagnostic and returns true on error, returns false and
758/// attaches the instantiated base classes to the class template
759/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000760bool
John McCallce3ff2b2009-08-25 22:02:44 +0000761Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
762 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000763 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000764 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000765 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000766 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000767 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000768 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000769 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000770 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000771 continue;
772 }
773
Mike Stump1eb44332009-09-09 15:08:12 +0000774 QualType BaseType = SubstType(Base->getType(),
775 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000776 Base->getSourceRange().getBegin(),
777 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000778 if (BaseType.isNull()) {
779 Invalid = true;
780 continue;
781 }
782
783 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000784 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000785 Base->getSourceRange(),
786 Base->isVirtual(),
787 Base->getAccessSpecifierAsWritten(),
788 BaseType,
789 /*FIXME: Not totally accurate */
790 Base->getSourceRange().getBegin()))
791 InstantiatedBases.push_back(InstantiatedBase);
792 else
793 Invalid = true;
794 }
795
Douglas Gregor27b152f2009-03-10 18:52:44 +0000796 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000797 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000798 InstantiatedBases.size()))
799 Invalid = true;
800
801 return Invalid;
802}
803
Douglas Gregord475b8d2009-03-25 21:17:03 +0000804/// \brief Instantiate the definition of a class from a given pattern.
805///
806/// \param PointOfInstantiation The point of instantiation within the
807/// source code.
808///
809/// \param Instantiation is the declaration whose definition is being
810/// instantiated. This will be either a class template specialization
811/// or a member class of a class template specialization.
812///
813/// \param Pattern is the pattern from which the instantiation
814/// occurs. This will be either the declaration of a class template or
815/// the declaration of a member class of a class template.
816///
817/// \param TemplateArgs The template arguments to be substituted into
818/// the pattern.
819///
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000820/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +0000821///
822/// \param Complain whether to complain if the class cannot be instantiated due
823/// to the lack of a definition.
824///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000825/// \returns true if an error occurred, false otherwise.
826bool
827Sema::InstantiateClass(SourceLocation PointOfInstantiation,
828 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000829 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000830 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000831 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000832 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +0000833
Mike Stump1eb44332009-09-09 15:08:12 +0000834 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +0000835 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
836 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +0000837 if (!Complain) {
838 // Say nothing
839 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000840 Diag(PointOfInstantiation,
841 diag::err_implicit_instantiate_member_undefined)
842 << Context.getTypeDeclType(Instantiation);
843 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
844 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000845 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000846 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +0000847 << Context.getTypeDeclType(Instantiation);
848 Diag(Pattern->getLocation(), diag::note_template_decl_here);
849 }
850 return true;
851 }
852 Pattern = PatternDef;
853
Douglas Gregor454885e2009-10-15 15:54:05 +0000854 // \brief Record the point of instantiation.
855 if (MemberSpecializationInfo *MSInfo
856 = Instantiation->getMemberSpecializationInfo()) {
857 MSInfo->setTemplateSpecializationKind(TSK);
858 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000859 } else if (ClassTemplateSpecializationDecl *Spec
860 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
861 Spec->setTemplateSpecializationKind(TSK);
862 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +0000863 }
864
Douglas Gregord048bb72009-03-25 21:23:52 +0000865 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000866 if (Inst)
867 return true;
868
869 // Enter the scope of this instantiation. We don't use
870 // PushDeclContext because we don't have a scope.
871 DeclContext *PreviousContext = CurContext;
872 CurContext = Instantiation;
873
874 // Start the definition of this instantiation.
875 Instantiation->startDefinition();
876
John McCallce3ff2b2009-08-25 22:02:44 +0000877 // Do substitution on the base class specifiers.
878 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000879 Invalid = true;
880
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000881 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000882 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000883 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000884 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +0000885 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000886 if (NewMember) {
887 if (NewMember->isInvalidDecl())
888 Invalid = true;
889 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000890 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +0000891 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
892 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000893 } else {
894 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000895 // instantiations was a semantic disaster, and we'll want to set Invalid =
896 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000897 }
898 }
899
900 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000901 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000902 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000903 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000904 if (Instantiation->isInvalidDecl())
905 Invalid = true;
906
Douglas Gregord475b8d2009-03-25 21:17:03 +0000907 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +0000908 if (!Invalid)
909 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000910
911 // Exit the scope of this instantiation.
912 CurContext = PreviousContext;
913
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000914 if (!Invalid)
915 Consumer.HandleTagDeclDefinition(Instantiation);
916
Douglas Gregord475b8d2009-03-25 21:17:03 +0000917 return Invalid;
918}
919
Mike Stump1eb44332009-09-09 15:08:12 +0000920bool
Douglas Gregor2943aed2009-03-03 04:44:36 +0000921Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000922 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000923 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000924 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000925 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000926 // Perform the actual instantiation on the canonical declaration.
927 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000928 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000929
Douglas Gregor52604ab2009-09-11 21:19:12 +0000930 // Check whether we have already instantiated or specialized this class
931 // template specialization.
932 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
933 if (ClassTemplateSpec->getSpecializationKind() ==
934 TSK_ExplicitInstantiationDeclaration &&
935 TSK == TSK_ExplicitInstantiationDefinition) {
936 // An explicit instantiation definition follows an explicit instantiation
937 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
938 // explicit instantiation.
939 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +0000940 return false;
941 }
942
943 // We can only instantiate something that hasn't already been
944 // instantiated or specialized. Fail without any diagnostics: our
945 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000946 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +0000947 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000948
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000949 if (ClassTemplateSpec->isInvalidDecl())
950 return true;
951
Douglas Gregor2943aed2009-03-03 04:44:36 +0000952 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000953 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000954
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000955 // C++ [temp.class.spec.match]p1:
956 // When a class template is used in a context that requires an
957 // instantiation of the class, it is necessary to determine
958 // whether the instantiation is to be generated using the primary
959 // template or one of the partial specializations. This is done by
960 // matching the template arguments of the class template
961 // specialization with the template argument lists of the partial
962 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000963 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
964 TemplateArgumentList *> MatchResult;
965 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +0000966 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000967 Partial = Template->getPartialSpecializations().begin(),
968 PartialEnd = Template->getPartialSpecializations().end();
969 Partial != PartialEnd;
970 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000971 TemplateDeductionInfo Info(Context);
972 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000973 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000974 ClassTemplateSpec->getTemplateArgs(),
975 Info)) {
976 // FIXME: Store the failed-deduction information for use in
977 // diagnostics, later.
978 (void)Result;
979 } else {
980 Matched.push_back(std::make_pair(&*Partial, Info.take()));
981 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000982 }
983
984 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000985 // -- If exactly one matching specialization is found, the
986 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000987 Pattern = Matched[0].first;
Douglas Gregor37d93e92009-08-02 23:24:31 +0000988 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000989 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000990 // -- If more than one matching specialization is found, the
991 // partial order rules (14.5.4.2) are used to determine
992 // whether one of the specializations is more specialized
993 // than the others. If none of the specializations is more
994 // specialized than all of the other matching
995 // specializations, then the use of the class template is
996 // ambiguous and the program is ill-formed.
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000997 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
998 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
999 PEnd = Matched.end();
1000 P != PEnd; ++P) {
1001 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1002 == P->first)
1003 Best = P;
1004 }
1005
1006 // Determine if the best partial specialization is more specialized than
1007 // the others.
1008 bool Ambiguous = false;
1009 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1010 PEnd = Matched.end();
1011 P != PEnd; ++P) {
1012 if (P != Best &&
1013 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1014 != Best->first) {
1015 Ambiguous = true;
1016 break;
1017 }
1018 }
1019
1020 if (Ambiguous) {
1021 // Partial ordering did not produce a clear winner. Complain.
1022 ClassTemplateSpec->setInvalidDecl();
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001023 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001024 << 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 Gregor972e6ce2009-10-27 06:26:26 +00001056 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1057 Pattern,
1058 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001059 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001060 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Douglas Gregor199d9912009-06-05 00:53:49 +00001062 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1063 // FIXME: Implement TemplateArgumentList::Destroy!
1064 // if (Matched[I].first != Pattern)
1065 // Matched[I].second->Destroy(Context);
1066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Douglas Gregor199d9912009-06-05 00:53:49 +00001068 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001069}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001070
John McCallce3ff2b2009-08-25 22:02:44 +00001071/// \brief Instantiates the definitions of all of the member
1072/// of the given class, which is an instantiation of a class template
1073/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001074void
1075Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001076 CXXRecordDecl *Instantiation,
1077 const MultiLevelTemplateArgumentList &TemplateArgs,
1078 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001079 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1080 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001081 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001082 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001083 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001084 if (FunctionDecl *Pattern
1085 = Function->getInstantiatedFromMemberFunction()) {
1086 MemberSpecializationInfo *MSInfo
1087 = Function->getMemberSpecializationInfo();
1088 assert(MSInfo && "No member specialization information?");
1089 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1090 Function,
1091 MSInfo->getTemplateSpecializationKind(),
1092 MSInfo->getPointOfInstantiation(),
1093 SuppressNew) ||
1094 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001095 continue;
1096
Douglas Gregor0d035142009-10-27 18:42:08 +00001097 if (Function->getBody())
1098 continue;
1099
1100 if (TSK == TSK_ExplicitInstantiationDefinition) {
1101 // C++0x [temp.explicit]p8:
1102 // An explicit instantiation definition that names a class template
1103 // specialization explicitly instantiates the class template
1104 // specialization and is only an explicit instantiation definition
1105 // of members whose definition is visible at the point of
1106 // instantiation.
1107 if (!Pattern->getBody())
1108 continue;
1109
1110 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1111
1112 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1113 } else {
1114 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1115 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001116 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001117 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001118 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001119 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1120 assert(MSInfo && "No member specialization information?");
1121 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1122 Var,
1123 MSInfo->getTemplateSpecializationKind(),
1124 MSInfo->getPointOfInstantiation(),
1125 SuppressNew) ||
1126 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001127 continue;
1128
Douglas Gregor0d035142009-10-27 18:42:08 +00001129 if (TSK == TSK_ExplicitInstantiationDefinition) {
1130 // C++0x [temp.explicit]p8:
1131 // An explicit instantiation definition that names a class template
1132 // specialization explicitly instantiates the class template
1133 // specialization and is only an explicit instantiation definition
1134 // of members whose definition is visible at the point of
1135 // instantiation.
1136 if (!Var->getInstantiatedFromStaticDataMember()
1137 ->getOutOfLineDefinition())
1138 continue;
1139
1140 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001141 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001142 } else {
1143 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1144 }
1145 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001146 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001147 if (Record->isInjectedClassName())
1148 continue;
1149
Douglas Gregor0d035142009-10-27 18:42:08 +00001150 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1151 assert(MSInfo && "No member specialization information?");
1152 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1153 Record,
1154 MSInfo->getTemplateSpecializationKind(),
1155 MSInfo->getPointOfInstantiation(),
1156 SuppressNew) ||
1157 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001158 continue;
1159
Douglas Gregor0d035142009-10-27 18:42:08 +00001160 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1161 assert(Pattern && "Missing instantiated-from-template information");
1162
1163 if (!Record->getDefinition(Context)) {
1164 if (!Pattern->getDefinition(Context)) {
1165 // C++0x [temp.explicit]p8:
1166 // An explicit instantiation definition that names a class template
1167 // specialization explicitly instantiates the class template
1168 // specialization and is only an explicit instantiation definition
1169 // of members whose definition is visible at the point of
1170 // instantiation.
1171 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1172 MSInfo->setTemplateSpecializationKind(TSK);
1173 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1174 }
1175
1176 continue;
1177 }
1178
1179 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001180 TemplateArgs,
1181 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001182 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001183
Douglas Gregor0d035142009-10-27 18:42:08 +00001184 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1185 if (Pattern)
1186 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1187 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001188 }
1189 }
1190}
1191
1192/// \brief Instantiate the definitions of all of the members of the
1193/// given class template specialization, which was named as part of an
1194/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001195void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001196Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001197 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001198 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1199 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001200 // C++0x [temp.explicit]p7:
1201 // An explicit instantiation that names a class template
1202 // specialization is an explicit instantion of the same kind
1203 // (declaration or definition) of each of its members (not
1204 // including members inherited from base classes) that has not
1205 // been previously explicitly specialized in the translation unit
1206 // containing the explicit instantiation, except as described
1207 // below.
1208 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001209 getTemplateInstantiationArgs(ClassTemplateSpec),
1210 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001211}
1212
Mike Stump1eb44332009-09-09 15:08:12 +00001213Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001214Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001215 if (!S)
1216 return Owned(S);
1217
1218 TemplateInstantiator Instantiator(*this, TemplateArgs,
1219 SourceLocation(),
1220 DeclarationName());
1221 return Instantiator.TransformStmt(S);
1222}
1223
Mike Stump1eb44332009-09-09 15:08:12 +00001224Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001225Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001226 if (!E)
1227 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Douglas Gregorb98b1992009-08-11 05:31:07 +00001229 TemplateInstantiator Instantiator(*this, TemplateArgs,
1230 SourceLocation(),
1231 DeclarationName());
1232 return Instantiator.TransformExpr(E);
1233}
1234
John McCallce3ff2b2009-08-25 22:02:44 +00001235/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001236NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001237Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001238 SourceRange Range,
1239 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001240 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1241 DeclarationName());
1242 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001243}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001244
1245TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001246Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001247 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001248 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1249 DeclarationName());
1250 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001251}
Douglas Gregor91333002009-06-11 00:06:24 +00001252
Mike Stump1eb44332009-09-09 15:08:12 +00001253TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001254 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001255 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1256 DeclarationName());
1257 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregor91333002009-06-11 00:06:24 +00001258}