blob: dfe37d8caf17ccd270281f15efa6caade1c0c191 [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
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000425 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E,
426 bool isAddressOfOperand);
427 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E,
428 bool isAddressOfOperand);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
430 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000431 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000432 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
433 TemplateTypeParmTypeLoc TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000434 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000435}
436
Douglas Gregor577f75a2009-08-04 16:50:30 +0000437Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000438 if (!D)
439 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Douglas Gregorc68afe22009-09-03 21:38:09 +0000441 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000442 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
443 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
444 "Wrong kind of template template argument");
Mike Stump1eb44332009-09-09 15:08:12 +0000445 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000446 TTP->getPosition()).getAsDecl());
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
449 // If the corresponding template argument is NULL or non-existent, it's
450 // because we are performing instantiation from explicitly-specified
Douglas Gregord6350ae2009-08-28 20:31:08 +0000451 // template arguments in a function template, but there were some
452 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000453 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000454 TTP->getPosition()))
455 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Douglas Gregord6350ae2009-08-28 20:31:08 +0000457 // FIXME: Implement depth reduction of template template parameters
Mike Stump1eb44332009-09-09 15:08:12 +0000458 assert(false &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000459 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregord1067e52009-08-06 06:41:21 +0000460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Douglas Gregore95b4092009-09-16 18:34:49 +0000462 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000463}
464
Douglas Gregor43959a92009-08-20 07:17:43 +0000465Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000466 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000467 if (!Inst)
468 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregor43959a92009-08-20 07:17:43 +0000470 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
471 return Inst;
472}
473
Douglas Gregor6cd21982009-10-20 05:58:46 +0000474NamedDecl *
475TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
476 SourceLocation Loc) {
477 // If the first part of the nested-name-specifier was a template type
478 // parameter, instantiate that type parameter down to a tag type.
479 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
480 const TemplateTypeParmType *TTP
481 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
482 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
483 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
484 if (T.isNull())
485 return cast_or_null<NamedDecl>(TransformDecl(D));
486
487 if (const TagType *Tag = T->getAs<TagType>())
488 return Tag->getDecl();
489
490 // The resulting type is not a tag; complain.
491 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
492 return 0;
493 }
494 }
495
496 return cast_or_null<NamedDecl>(TransformDecl(D));
497}
498
Douglas Gregor43959a92009-08-20 07:17:43 +0000499VarDecl *
500TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000501 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000502 DeclaratorInfo *Declarator,
503 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000504 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000505 SourceRange TypeRange) {
506 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
507 Name, Loc, TypeRange);
508 if (Var && !Var->isInvalidDecl())
509 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
510 return Var;
511}
512
John McCallc4e70192009-09-11 04:59:25 +0000513QualType
514TemplateInstantiator::RebuildElaboratedType(QualType T,
515 ElaboratedType::TagKind Tag) {
516 if (const TagType *TT = T->getAs<TagType>()) {
517 TagDecl* TD = TT->getDecl();
518
519 // FIXME: this location is very wrong; we really need typelocs.
520 SourceLocation TagLocation = TD->getTagKeywordLoc();
521
522 // FIXME: type might be anonymous.
523 IdentifierInfo *Id = TD->getIdentifier();
524
525 // TODO: should we even warn on struct/class mismatches for this? Seems
526 // like it's likely to produce a lot of spurious errors.
527 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
528 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
529 << Id
530 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
531 TD->getKindName());
532 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
533 }
534 }
535
536 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
537}
538
539Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000540TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E,
541 bool isAddressOfOperand) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000542 if (!E->isTypeDependent())
543 return SemaRef.Owned(E->Retain());
544
545 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
546 assert(currentDecl && "Must have current function declaration when "
547 "instantiating.");
548
549 PredefinedExpr::IdentType IT = E->getIdentType();
550
551 unsigned Length =
552 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
553
554 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000555 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000556 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
557 ArrayType::Normal, 0);
558 PredefinedExpr *PE =
559 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
560 return getSema().Owned(PE);
561}
562
563Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000564TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E,
565 bool isAddressOfOperand) {
Douglas Gregorb98b1992009-08-11 05:31:07 +0000566 // FIXME: Clean this up a bit
567 NamedDecl *D = E->getDecl();
568 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000569 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
570
571 // If the corresponding template argument is NULL or non-existent, it's
572 // because we are performing instantiation from explicitly-specified
573 // template arguments in a function template, but there were some
574 // arguments left unspecified.
575 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
576 NTTP->getPosition()))
577 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Douglas Gregor550d9b22009-10-31 17:21:17 +0000579 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
580 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Douglas Gregor550d9b22009-10-31 17:21:17 +0000582 // The template argument itself might be an expression, in which
583 // case we just return that expression.
584 if (Arg.getKind() == TemplateArgument::Expression)
585 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Douglas Gregor550d9b22009-10-31 17:21:17 +0000587 if (Arg.getKind() == TemplateArgument::Declaration) {
588 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Douglas Gregor550d9b22009-10-31 17:21:17 +0000590 VD = cast_or_null<ValueDecl>(
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000591 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000592 if (!VD)
593 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Douglas Gregor550d9b22009-10-31 17:21:17 +0000595 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
596 /*FIXME:*/false, /*FIXME:*/false);
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregor550d9b22009-10-31 17:21:17 +0000599 assert(Arg.getKind() == TemplateArgument::Integral);
600 QualType T = Arg.getIntegralType();
601 if (T->isCharType() || T->isWideCharType())
602 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
603 Arg.getAsIntegral()->getZExtValue(),
604 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000605 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000606 E->getSourceRange().getBegin()));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000607 if (T->isBooleanType())
608 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
609 Arg.getAsIntegral()->getBoolValue(),
610 T,
611 E->getSourceRange().getBegin()));
612
613 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
614 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
615 *Arg.getAsIntegral(),
616 T,
617 E->getSourceRange().getBegin()));
618 }
619
620 // We have a non-type template parameter that isn't fully substituted;
621 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregore95b4092009-09-16 18:34:49 +0000624 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000625 if (!InstD)
626 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Anders Carlsson0d8df782009-08-29 19:37:28 +0000628 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000629 // we need to get the underlying decl.
Anders Carlsson0d8df782009-08-29 19:37:28 +0000630 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
631 InstD = InstD->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Douglas Gregora2813ce2009-10-23 18:54:35 +0000633 CXXScopeSpec SS;
634 NestedNameSpecifier *Qualifier = 0;
635 if (E->getQualifier()) {
636 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
637 E->getQualifierRange());
638 if (!Qualifier)
639 return SemaRef.ExprError();
640
641 SS.setScopeRep(Qualifier);
642 SS.setRange(E->getQualifierRange());
643 }
644
Mike Stump1eb44332009-09-09 15:08:12 +0000645 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregorb98b1992009-08-11 05:31:07 +0000646 /*FIXME:*/false,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000647 &SS,
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000648 isAddressOfOperand);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000649}
650
Mike Stump1eb44332009-09-09 15:08:12 +0000651QualType
John McCalla2becad2009-10-21 00:40:46 +0000652TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
653 TemplateTypeParmTypeLoc TL) {
654 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000655 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000656 // Replace the template type parameter with its corresponding
657 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000658
659 // If the corresponding template argument is NULL or doesn't exist, it's
660 // because we are performing instantiation from explicitly-specified
661 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000662 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000663 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
664 TemplateTypeParmTypeLoc NewTL
665 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
666 NewTL.setNameLoc(TL.getNameLoc());
667 return TL.getType();
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
670 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000671 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000672 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000673
John McCall49a832b2009-10-18 09:09:24 +0000674 QualType Replacement
675 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
676
677 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000678 QualType Result
679 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
680 SubstTemplateTypeParmTypeLoc NewTL
681 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
682 NewTL.setNameLoc(TL.getNameLoc());
683 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000684 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000685
686 // The template type parameter comes from an inner template (e.g.,
687 // the template parameter list of a member template inside the
688 // template we are instantiating). Create a new template type
689 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000690 QualType Result
691 = getSema().Context.getTemplateTypeParmType(T->getDepth()
692 - TemplateArgs.getNumLevels(),
693 T->getIndex(),
694 T->isParameterPack(),
695 T->getName());
696 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
697 NewTL.setNameLoc(TL.getNameLoc());
698 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000699}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000700
John McCallce3ff2b2009-08-25 22:02:44 +0000701/// \brief Perform substitution on the type T with a given set of template
702/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000703///
704/// This routine substitutes the given template arguments into the
705/// type T and produces the instantiated type.
706///
707/// \param T the type into which the template arguments will be
708/// substituted. If this type is not dependent, it will be returned
709/// immediately.
710///
711/// \param TemplateArgs the template arguments that will be
712/// substituted for the top-level template parameters within T.
713///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000714/// \param Loc the location in the source code where this substitution
715/// is being performed. It will typically be the location of the
716/// declarator (if we're instantiating the type of some declaration)
717/// or the location of the type in the source code (if, e.g., we're
718/// instantiating the type of a cast expression).
719///
720/// \param Entity the name of the entity associated with a declaration
721/// being instantiated (if any). May be empty to indicate that there
722/// is no such entity (if, e.g., this is a type that occurs as part of
723/// a cast expression) or that the entity has no name (e.g., an
724/// unnamed function parameter).
725///
726/// \returns If the instantiation succeeds, the instantiated
727/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallcd7ba1c2009-10-21 00:58:09 +0000728DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
729 const MultiLevelTemplateArgumentList &Args,
730 SourceLocation Loc,
731 DeclarationName Entity) {
732 assert(!ActiveTemplateInstantiations.empty() &&
733 "Cannot perform an instantiation without some context on the "
734 "instantiation stack");
735
736 if (!T->getType()->isDependentType())
737 return T;
738
739 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
740 return Instantiator.TransformType(T);
741}
742
743/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000744QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000745 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000746 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000747 assert(!ActiveTemplateInstantiations.empty() &&
748 "Cannot perform an instantiation without some context on the "
749 "instantiation stack");
750
Douglas Gregor99ebf652009-02-27 19:31:52 +0000751 // If T is not a dependent type, there is nothing to do.
752 if (!T->isDependentType())
753 return T;
754
Douglas Gregor577f75a2009-08-04 16:50:30 +0000755 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
756 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000757}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000758
John McCallce3ff2b2009-08-25 22:02:44 +0000759/// \brief Perform substitution on the base class specifiers of the
760/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000761///
762/// Produces a diagnostic and returns true on error, returns false and
763/// attaches the instantiated base classes to the class template
764/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000765bool
John McCallce3ff2b2009-08-25 22:02:44 +0000766Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
767 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000768 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000769 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000770 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000771 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000772 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000773 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000774 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000775 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000776 continue;
777 }
778
Mike Stump1eb44332009-09-09 15:08:12 +0000779 QualType BaseType = SubstType(Base->getType(),
780 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000781 Base->getSourceRange().getBegin(),
782 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000783 if (BaseType.isNull()) {
784 Invalid = true;
785 continue;
786 }
787
788 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000789 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000790 Base->getSourceRange(),
791 Base->isVirtual(),
792 Base->getAccessSpecifierAsWritten(),
793 BaseType,
794 /*FIXME: Not totally accurate */
795 Base->getSourceRange().getBegin()))
796 InstantiatedBases.push_back(InstantiatedBase);
797 else
798 Invalid = true;
799 }
800
Douglas Gregor27b152f2009-03-10 18:52:44 +0000801 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000802 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000803 InstantiatedBases.size()))
804 Invalid = true;
805
806 return Invalid;
807}
808
Douglas Gregord475b8d2009-03-25 21:17:03 +0000809/// \brief Instantiate the definition of a class from a given pattern.
810///
811/// \param PointOfInstantiation The point of instantiation within the
812/// source code.
813///
814/// \param Instantiation is the declaration whose definition is being
815/// instantiated. This will be either a class template specialization
816/// or a member class of a class template specialization.
817///
818/// \param Pattern is the pattern from which the instantiation
819/// occurs. This will be either the declaration of a class template or
820/// the declaration of a member class of a class template.
821///
822/// \param TemplateArgs The template arguments to be substituted into
823/// the pattern.
824///
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000825/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +0000826///
827/// \param Complain whether to complain if the class cannot be instantiated due
828/// to the lack of a definition.
829///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000830/// \returns true if an error occurred, false otherwise.
831bool
832Sema::InstantiateClass(SourceLocation PointOfInstantiation,
833 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000834 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000835 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000836 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000837 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +0000838
Mike Stump1eb44332009-09-09 15:08:12 +0000839 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +0000840 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
841 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +0000842 if (!Complain) {
843 // Say nothing
844 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000845 Diag(PointOfInstantiation,
846 diag::err_implicit_instantiate_member_undefined)
847 << Context.getTypeDeclType(Instantiation);
848 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
849 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000850 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000851 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +0000852 << Context.getTypeDeclType(Instantiation);
853 Diag(Pattern->getLocation(), diag::note_template_decl_here);
854 }
855 return true;
856 }
857 Pattern = PatternDef;
858
Douglas Gregor454885e2009-10-15 15:54:05 +0000859 // \brief Record the point of instantiation.
860 if (MemberSpecializationInfo *MSInfo
861 = Instantiation->getMemberSpecializationInfo()) {
862 MSInfo->setTemplateSpecializationKind(TSK);
863 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000864 } else if (ClassTemplateSpecializationDecl *Spec
865 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
866 Spec->setTemplateSpecializationKind(TSK);
867 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +0000868 }
869
Douglas Gregord048bb72009-03-25 21:23:52 +0000870 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000871 if (Inst)
872 return true;
873
874 // Enter the scope of this instantiation. We don't use
875 // PushDeclContext because we don't have a scope.
876 DeclContext *PreviousContext = CurContext;
877 CurContext = Instantiation;
878
879 // Start the definition of this instantiation.
880 Instantiation->startDefinition();
881
John McCallce3ff2b2009-08-25 22:02:44 +0000882 // Do substitution on the base class specifiers.
883 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000884 Invalid = true;
885
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000886 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000887 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000888 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000889 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +0000890 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000891 if (NewMember) {
892 if (NewMember->isInvalidDecl())
893 Invalid = true;
894 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000895 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +0000896 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
897 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000898 } else {
899 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000900 // instantiations was a semantic disaster, and we'll want to set Invalid =
901 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902 }
903 }
904
905 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000906 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000907 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000908 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000909 if (Instantiation->isInvalidDecl())
910 Invalid = true;
911
Douglas Gregord475b8d2009-03-25 21:17:03 +0000912 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +0000913 if (!Invalid)
914 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000915
916 // Exit the scope of this instantiation.
917 CurContext = PreviousContext;
918
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000919 if (!Invalid)
920 Consumer.HandleTagDeclDefinition(Instantiation);
921
Douglas Gregord475b8d2009-03-25 21:17:03 +0000922 return Invalid;
923}
924
Mike Stump1eb44332009-09-09 15:08:12 +0000925bool
Douglas Gregor2943aed2009-03-03 04:44:36 +0000926Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000927 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000928 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000929 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000930 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000931 // Perform the actual instantiation on the canonical declaration.
932 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000933 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000934
Douglas Gregor52604ab2009-09-11 21:19:12 +0000935 // Check whether we have already instantiated or specialized this class
936 // template specialization.
937 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
938 if (ClassTemplateSpec->getSpecializationKind() ==
939 TSK_ExplicitInstantiationDeclaration &&
940 TSK == TSK_ExplicitInstantiationDefinition) {
941 // An explicit instantiation definition follows an explicit instantiation
942 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
943 // explicit instantiation.
944 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +0000945 return false;
946 }
947
948 // We can only instantiate something that hasn't already been
949 // instantiated or specialized. Fail without any diagnostics: our
950 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000951 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +0000952 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000953
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000954 if (ClassTemplateSpec->isInvalidDecl())
955 return true;
956
Douglas Gregor2943aed2009-03-03 04:44:36 +0000957 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000958 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000959
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000960 // C++ [temp.class.spec.match]p1:
961 // When a class template is used in a context that requires an
962 // instantiation of the class, it is necessary to determine
963 // whether the instantiation is to be generated using the primary
964 // template or one of the partial specializations. This is done by
965 // matching the template arguments of the class template
966 // specialization with the template argument lists of the partial
967 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000968 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
969 TemplateArgumentList *> MatchResult;
970 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +0000971 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000972 Partial = Template->getPartialSpecializations().begin(),
973 PartialEnd = Template->getPartialSpecializations().end();
974 Partial != PartialEnd;
975 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000976 TemplateDeductionInfo Info(Context);
977 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000978 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000979 ClassTemplateSpec->getTemplateArgs(),
980 Info)) {
981 // FIXME: Store the failed-deduction information for use in
982 // diagnostics, later.
983 (void)Result;
984 } else {
985 Matched.push_back(std::make_pair(&*Partial, Info.take()));
986 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000987 }
988
Douglas Gregored9c0f92009-10-29 00:04:11 +0000989 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000990 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +0000991 if (Matched.size() == 1) {
992 // -- If exactly one matching specialization is found, the
993 // instantiation is generated from that specialization.
994 // We don't need to do anything for this.
995 } else {
996 // -- If more than one matching specialization is found, the
997 // partial order rules (14.5.4.2) are used to determine
998 // whether one of the specializations is more specialized
999 // than the others. If none of the specializations is more
1000 // specialized than all of the other matching
1001 // specializations, then the use of the class template is
1002 // ambiguous and the program is ill-formed.
1003 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1004 PEnd = Matched.end();
1005 P != PEnd; ++P) {
1006 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1007 == P->first)
1008 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001009 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001010
Douglas Gregored9c0f92009-10-29 00:04:11 +00001011 // Determine if the best partial specialization is more specialized than
1012 // the others.
1013 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001014 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1015 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001016 P != PEnd; ++P) {
1017 if (P != Best &&
1018 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1019 != Best->first) {
1020 Ambiguous = true;
1021 break;
1022 }
1023 }
1024
1025 if (Ambiguous) {
1026 // Partial ordering did not produce a clear winner. Complain.
1027 ClassTemplateSpec->setInvalidDecl();
1028 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1029 << ClassTemplateSpec;
1030
1031 // Print the matching partial specializations.
1032 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1033 PEnd = Matched.end();
1034 P != PEnd; ++P)
1035 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1036 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1037 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001038
Douglas Gregored9c0f92009-10-29 00:04:11 +00001039 return true;
1040 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001041 }
1042
1043 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001044 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1045 while (OrigPartialSpec->getInstantiatedFromMember()) {
1046 // If we've found an explicit specialization of this class template,
1047 // stop here and use that as the pattern.
1048 if (OrigPartialSpec->isMemberSpecialization())
1049 break;
1050
1051 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1052 }
1053
1054 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001055 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001056 } else {
1057 // -- If no matches are found, the instantiation is generated
1058 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001059 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001060 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1061 // If we've found an explicit specialization of this class template,
1062 // stop here and use that as the pattern.
1063 if (OrigTemplate->isMemberSpecialization())
1064 break;
1065
Douglas Gregord6350ae2009-08-28 20:31:08 +00001066 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001067 }
1068
Douglas Gregord6350ae2009-08-28 20:31:08 +00001069 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001070 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001071
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001072 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1073 Pattern,
1074 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001075 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001076 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Douglas Gregor199d9912009-06-05 00:53:49 +00001078 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1079 // FIXME: Implement TemplateArgumentList::Destroy!
1080 // if (Matched[I].first != Pattern)
1081 // Matched[I].second->Destroy(Context);
1082 }
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Douglas Gregor199d9912009-06-05 00:53:49 +00001084 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001085}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001086
John McCallce3ff2b2009-08-25 22:02:44 +00001087/// \brief Instantiates the definitions of all of the member
1088/// of the given class, which is an instantiation of a class template
1089/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001090void
1091Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001092 CXXRecordDecl *Instantiation,
1093 const MultiLevelTemplateArgumentList &TemplateArgs,
1094 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001095 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1096 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001097 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001098 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001099 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001100 if (FunctionDecl *Pattern
1101 = Function->getInstantiatedFromMemberFunction()) {
1102 MemberSpecializationInfo *MSInfo
1103 = Function->getMemberSpecializationInfo();
1104 assert(MSInfo && "No member specialization information?");
1105 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1106 Function,
1107 MSInfo->getTemplateSpecializationKind(),
1108 MSInfo->getPointOfInstantiation(),
1109 SuppressNew) ||
1110 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001111 continue;
1112
Douglas Gregor0d035142009-10-27 18:42:08 +00001113 if (Function->getBody())
1114 continue;
1115
1116 if (TSK == TSK_ExplicitInstantiationDefinition) {
1117 // C++0x [temp.explicit]p8:
1118 // An explicit instantiation definition that names a class template
1119 // specialization explicitly instantiates the class template
1120 // specialization and is only an explicit instantiation definition
1121 // of members whose definition is visible at the point of
1122 // instantiation.
1123 if (!Pattern->getBody())
1124 continue;
1125
1126 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1127
1128 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1129 } else {
1130 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1131 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001132 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001133 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001134 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001135 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1136 assert(MSInfo && "No member specialization information?");
1137 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1138 Var,
1139 MSInfo->getTemplateSpecializationKind(),
1140 MSInfo->getPointOfInstantiation(),
1141 SuppressNew) ||
1142 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001143 continue;
1144
Douglas Gregor0d035142009-10-27 18:42:08 +00001145 if (TSK == TSK_ExplicitInstantiationDefinition) {
1146 // C++0x [temp.explicit]p8:
1147 // An explicit instantiation definition that names a class template
1148 // specialization explicitly instantiates the class template
1149 // specialization and is only an explicit instantiation definition
1150 // of members whose definition is visible at the point of
1151 // instantiation.
1152 if (!Var->getInstantiatedFromStaticDataMember()
1153 ->getOutOfLineDefinition())
1154 continue;
1155
1156 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001157 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001158 } else {
1159 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1160 }
1161 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001162 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001163 if (Record->isInjectedClassName())
1164 continue;
1165
Douglas Gregor0d035142009-10-27 18:42:08 +00001166 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1167 assert(MSInfo && "No member specialization information?");
1168 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1169 Record,
1170 MSInfo->getTemplateSpecializationKind(),
1171 MSInfo->getPointOfInstantiation(),
1172 SuppressNew) ||
1173 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001174 continue;
1175
Douglas Gregor0d035142009-10-27 18:42:08 +00001176 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1177 assert(Pattern && "Missing instantiated-from-template information");
1178
1179 if (!Record->getDefinition(Context)) {
1180 if (!Pattern->getDefinition(Context)) {
1181 // C++0x [temp.explicit]p8:
1182 // An explicit instantiation definition that names a class template
1183 // specialization explicitly instantiates the class template
1184 // specialization and is only an explicit instantiation definition
1185 // of members whose definition is visible at the point of
1186 // instantiation.
1187 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1188 MSInfo->setTemplateSpecializationKind(TSK);
1189 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1190 }
1191
1192 continue;
1193 }
1194
1195 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001196 TemplateArgs,
1197 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001198 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001199
Douglas Gregor0d035142009-10-27 18:42:08 +00001200 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1201 if (Pattern)
1202 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1203 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001204 }
1205 }
1206}
1207
1208/// \brief Instantiate the definitions of all of the members of the
1209/// given class template specialization, which was named as part of an
1210/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001211void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001212Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001213 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001214 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1215 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001216 // C++0x [temp.explicit]p7:
1217 // An explicit instantiation that names a class template
1218 // specialization is an explicit instantion of the same kind
1219 // (declaration or definition) of each of its members (not
1220 // including members inherited from base classes) that has not
1221 // been previously explicitly specialized in the translation unit
1222 // containing the explicit instantiation, except as described
1223 // below.
1224 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001225 getTemplateInstantiationArgs(ClassTemplateSpec),
1226 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001227}
1228
Mike Stump1eb44332009-09-09 15:08:12 +00001229Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001230Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001231 if (!S)
1232 return Owned(S);
1233
1234 TemplateInstantiator Instantiator(*this, TemplateArgs,
1235 SourceLocation(),
1236 DeclarationName());
1237 return Instantiator.TransformStmt(S);
1238}
1239
Mike Stump1eb44332009-09-09 15:08:12 +00001240Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001241Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001242 if (!E)
1243 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Douglas Gregorb98b1992009-08-11 05:31:07 +00001245 TemplateInstantiator Instantiator(*this, TemplateArgs,
1246 SourceLocation(),
1247 DeclarationName());
1248 return Instantiator.TransformExpr(E);
1249}
1250
John McCallce3ff2b2009-08-25 22:02:44 +00001251/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001252NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001253Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001254 SourceRange Range,
1255 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001256 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1257 DeclarationName());
1258 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001259}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001260
1261TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001262Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001263 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001264 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1265 DeclarationName());
1266 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001267}
Douglas Gregor91333002009-06-11 00:06:24 +00001268
John McCall833ca992009-10-29 08:12:44 +00001269bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1270 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001271 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1272 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001273
1274 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001275}