blob: b1e0481932ffeca231da1112dddd17b39721dd91 [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregor17c0d7b2009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregor4ea568f2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor01afeef2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000031///
32/// \param D the declaration for which we are computing template instantiation
33/// arguments.
34///
35/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregora654dd82009-08-28 17:37:35 +000036MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000037Sema::getTemplateInstantiationArgs(NamedDecl *D,
38 const TemplateArgumentList *Innermost) {
Douglas Gregora654dd82009-08-28 17:37:35 +000039 // Accumulate the set of template argument lists in this structure.
40 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000041
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000042 if (Innermost)
43 Result.addOuterTemplateArguments(Innermost);
44
Douglas Gregora654dd82009-08-28 17:37:35 +000045 DeclContext *Ctx = dyn_cast<DeclContext>(D);
46 if (!Ctx)
47 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000048
John McCall970d5302009-08-29 03:16:09 +000049 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000050 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000051 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000052 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
53 // We're done when we hit an explicit specialization.
54 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
55 break;
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregora654dd82009-08-28 17:37:35 +000057 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000058
59 // If this class template specialization was instantiated from a
60 // specialized member that is a class template, we're done.
61 assert(Spec->getSpecializedTemplate() && "No class template?");
62 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
63 break;
Mike Stump11289f42009-09-09 15:08:12 +000064 }
Douglas Gregora654dd82009-08-28 17:37:35 +000065 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000066 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorcf915552009-10-13 16:30:37 +000067 if (Function->getTemplateSpecializationKind()
68 == TSK_ExplicitSpecialization)
69 break;
70
Douglas Gregora654dd82009-08-28 17:37:35 +000071 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000072 = Function->getTemplateSpecializationArgs()) {
73 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000074 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000075
Douglas Gregorcf915552009-10-13 16:30:37 +000076 // If this function was instantiated from a specialized member that is
77 // a function template, we're done.
78 assert(Function->getPrimaryTemplate() && "No function template?");
79 if (Function->getPrimaryTemplate()->isMemberSpecialization())
80 break;
81 }
82
John McCall970d5302009-08-29 03:16:09 +000083 // If this is a friend declaration and it declares an entity at
84 // namespace scope, take arguments from its lexical parent
85 // instead of its semantic parent.
86 if (Function->getFriendObjectKind() &&
87 Function->getDeclContext()->isFileContext()) {
88 Ctx = Function->getLexicalDeclContext();
89 continue;
90 }
Douglas Gregora654dd82009-08-28 17:37:35 +000091 }
John McCall970d5302009-08-29 03:16:09 +000092
93 Ctx = Ctx->getParent();
Douglas Gregorb4850462009-05-14 23:26:13 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Douglas Gregora654dd82009-08-28 17:37:35 +000096 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +000097}
98
Douglas Gregorfcd5db32009-03-10 00:06:19 +000099Sema::InstantiatingTemplate::
100InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000101 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000102 SourceRange InstantiationRange)
103 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000104
105 Invalid = CheckInstantiationDepth(PointOfInstantiation,
106 InstantiationRange);
107 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000108 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000109 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000110 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000111 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000112 Inst.TemplateArgs = 0;
113 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000114 Inst.InstantiationRange = InstantiationRange;
115 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
116 Invalid = false;
117 }
118}
119
Mike Stump11289f42009-09-09 15:08:12 +0000120Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000121 SourceLocation PointOfInstantiation,
122 TemplateDecl *Template,
123 const TemplateArgument *TemplateArgs,
124 unsigned NumTemplateArgs,
125 SourceRange InstantiationRange)
126 : SemaRef(SemaRef) {
127
128 Invalid = CheckInstantiationDepth(PointOfInstantiation,
129 InstantiationRange);
130 if (!Invalid) {
131 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000132 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000133 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
134 Inst.PointOfInstantiation = PointOfInstantiation;
135 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
136 Inst.TemplateArgs = TemplateArgs;
137 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000138 Inst.InstantiationRange = InstantiationRange;
139 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
140 Invalid = false;
141 }
142}
143
Mike Stump11289f42009-09-09 15:08:12 +0000144Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000145 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000146 FunctionTemplateDecl *FunctionTemplate,
147 const TemplateArgument *TemplateArgs,
148 unsigned NumTemplateArgs,
149 ActiveTemplateInstantiation::InstantiationKind Kind,
150 SourceRange InstantiationRange)
151: SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000152
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000153 Invalid = CheckInstantiationDepth(PointOfInstantiation,
154 InstantiationRange);
155 if (!Invalid) {
156 ActiveTemplateInstantiation Inst;
157 Inst.Kind = Kind;
158 Inst.PointOfInstantiation = PointOfInstantiation;
159 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
160 Inst.TemplateArgs = TemplateArgs;
161 Inst.NumTemplateArgs = NumTemplateArgs;
162 Inst.InstantiationRange = InstantiationRange;
163 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
164 Invalid = false;
165 }
166}
167
Mike Stump11289f42009-09-09 15:08:12 +0000168Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000169 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000170 ClassTemplatePartialSpecializationDecl *PartialSpec,
171 const TemplateArgument *TemplateArgs,
172 unsigned NumTemplateArgs,
173 SourceRange InstantiationRange)
174 : SemaRef(SemaRef) {
175
176 Invalid = CheckInstantiationDepth(PointOfInstantiation,
177 InstantiationRange);
178 if (!Invalid) {
179 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000180 Inst.Kind
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000181 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637d9982009-06-10 23:47:09 +0000182 Inst.PointOfInstantiation = PointOfInstantiation;
183 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
184 Inst.TemplateArgs = TemplateArgs;
185 Inst.NumTemplateArgs = NumTemplateArgs;
186 Inst.InstantiationRange = InstantiationRange;
187 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
188 Invalid = false;
189 }
190}
191
Mike Stump11289f42009-09-09 15:08:12 +0000192Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000193 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000194 ParmVarDecl *Param,
195 const TemplateArgument *TemplateArgs,
196 unsigned NumTemplateArgs,
197 SourceRange InstantiationRange)
198 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000199
Douglas Gregore62e6a02009-11-11 19:13:48 +0000200 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000201
202 if (!Invalid) {
203 ActiveTemplateInstantiation Inst;
204 Inst.Kind
205 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000206 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000207 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
208 Inst.TemplateArgs = TemplateArgs;
209 Inst.NumTemplateArgs = NumTemplateArgs;
210 Inst.InstantiationRange = InstantiationRange;
211 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000212 }
213}
214
215Sema::InstantiatingTemplate::
216InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
217 TemplateDecl *Template,
218 NonTypeTemplateParmDecl *Param,
219 const TemplateArgument *TemplateArgs,
220 unsigned NumTemplateArgs,
221 SourceRange InstantiationRange) : SemaRef(SemaRef) {
222 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
223
224 if (!Invalid) {
225 ActiveTemplateInstantiation Inst;
226 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
227 Inst.PointOfInstantiation = PointOfInstantiation;
228 Inst.Template = Template;
229 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
230 Inst.TemplateArgs = TemplateArgs;
231 Inst.NumTemplateArgs = NumTemplateArgs;
232 Inst.InstantiationRange = InstantiationRange;
233 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
234 }
235}
236
237Sema::InstantiatingTemplate::
238InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
239 TemplateDecl *Template,
240 TemplateTemplateParmDecl *Param,
241 const TemplateArgument *TemplateArgs,
242 unsigned NumTemplateArgs,
243 SourceRange InstantiationRange) : SemaRef(SemaRef) {
244 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
245
246 if (!Invalid) {
247 ActiveTemplateInstantiation Inst;
248 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
249 Inst.PointOfInstantiation = PointOfInstantiation;
250 Inst.Template = Template;
251 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
252 Inst.TemplateArgs = TemplateArgs;
253 Inst.NumTemplateArgs = NumTemplateArgs;
254 Inst.InstantiationRange = InstantiationRange;
255 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Anders Carlsson657bad42009-09-05 05:14:19 +0000256 }
257}
258
Douglas Gregor85673582009-05-18 17:01:57 +0000259void Sema::InstantiatingTemplate::Clear() {
260 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000261 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000262 Invalid = true;
263 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000264}
265
Douglas Gregor79cf6032009-03-10 20:44:00 +0000266bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
267 SourceLocation PointOfInstantiation,
268 SourceRange InstantiationRange) {
Mike Stump11289f42009-09-09 15:08:12 +0000269 if (SemaRef.ActiveTemplateInstantiations.size()
Douglas Gregor79cf6032009-03-10 20:44:00 +0000270 <= SemaRef.getLangOptions().InstantiationDepth)
271 return false;
272
Mike Stump11289f42009-09-09 15:08:12 +0000273 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000274 diag::err_template_recursion_depth_exceeded)
275 << SemaRef.getLangOptions().InstantiationDepth
276 << InstantiationRange;
277 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
278 << SemaRef.getLangOptions().InstantiationDepth;
279 return true;
280}
281
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000282/// \brief Prints the current instantiation stack through a series of
283/// notes.
284void Sema::PrintInstantiationStack() {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000285 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000286 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
287 Active = ActiveTemplateInstantiations.rbegin(),
288 ActiveEnd = ActiveTemplateInstantiations.rend();
289 Active != ActiveEnd;
290 ++Active) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000291 switch (Active->Kind) {
292 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000293 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
294 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
295 unsigned DiagID = diag::note_template_member_class_here;
296 if (isa<ClassTemplateSpecializationDecl>(Record))
297 DiagID = diag::note_template_class_instantiation_here;
Mike Stump11289f42009-09-09 15:08:12 +0000298 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000299 DiagID)
300 << Context.getTypeDeclType(Record)
301 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000302 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000303 unsigned DiagID;
304 if (Function->getPrimaryTemplate())
305 DiagID = diag::note_function_template_spec_here;
306 else
307 DiagID = diag::note_template_member_function_here;
Mike Stump11289f42009-09-09 15:08:12 +0000308 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000309 DiagID)
310 << Function
311 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000312 } else {
313 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
314 diag::note_template_static_data_member_def_here)
315 << cast<VarDecl>(D)
316 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000317 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000318 break;
319 }
320
321 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
322 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
323 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000324 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000325 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000326 Active->NumTemplateArgs,
327 Context.PrintingPolicy);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000328 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
329 diag::note_default_arg_instantiation_here)
330 << (Template->getNameAsString() + TemplateArgsStr)
331 << Active->InstantiationRange;
332 break;
333 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000334
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000335 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000336 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000337 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000338 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000339 diag::note_explicit_template_arg_substitution_here)
340 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000341 break;
342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000344 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
345 if (ClassTemplatePartialSpecializationDecl *PartialSpec
346 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
347 (Decl *)Active->Entity)) {
348 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
349 diag::note_partial_spec_deduct_instantiation_here)
350 << Context.getTypeDeclType(PartialSpec)
351 << Active->InstantiationRange;
352 } else {
353 FunctionTemplateDecl *FnTmpl
354 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
355 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
356 diag::note_function_template_deduction_instantiation_here)
357 << FnTmpl << Active->InstantiationRange;
358 }
359 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000360
Anders Carlsson657bad42009-09-05 05:14:19 +0000361 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
362 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
363 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000364
Anders Carlsson657bad42009-09-05 05:14:19 +0000365 std::string TemplateArgsStr
366 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000367 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000368 Active->NumTemplateArgs,
369 Context.PrintingPolicy);
370 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
371 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000372 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000373 << Active->InstantiationRange;
374 break;
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Douglas Gregore62e6a02009-11-11 19:13:48 +0000377 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
378 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
379 std::string Name;
380 if (!Parm->getName().empty())
381 Name = std::string(" '") + Parm->getName().str() + "'";
382
383 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
384 diag::note_prior_template_arg_substitution)
385 << isa<TemplateTemplateParmDecl>(Parm)
386 << Name
387 << getTemplateArgumentBindingsText(
388 Active->Template->getTemplateParameters(),
389 Active->TemplateArgs,
390 Active->NumTemplateArgs)
391 << Active->InstantiationRange;
392 break;
393 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000394 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000395 }
396}
397
Douglas Gregor33834512009-06-14 07:33:30 +0000398bool Sema::isSFINAEContext() const {
399 using llvm::SmallVector;
400 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
401 Active = ActiveTemplateInstantiations.rbegin(),
402 ActiveEnd = ActiveTemplateInstantiations.rend();
403 Active != ActiveEnd;
404 ++Active) {
405
406 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000407 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000408 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
409
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000410 // This is a template instantiation, so there is no SFINAE.
411 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000412
Douglas Gregor33834512009-06-14 07:33:30 +0000413 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000414 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
415 // A default template argument instantiation and substitution into
416 // template parameters with arguments for prior parameters may or may
417 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000418 break;
Mike Stump11289f42009-09-09 15:08:12 +0000419
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000420 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
421 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
422 // We're either substitution explicitly-specified template arguments
423 // or deduced template arguments, so SFINAE applies.
424 return true;
Douglas Gregor33834512009-06-14 07:33:30 +0000425 }
426 }
427
428 return false;
429}
430
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000431//===----------------------------------------------------------------------===/
432// Template Instantiation for Types
433//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000434namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000435 class VISIBILITY_HIDDEN TemplateInstantiator
436 : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000437 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000438 SourceLocation Loc;
439 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000440
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000441 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000442 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000443
444 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000445 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000446 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000447 DeclarationName Entity)
448 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000449 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000450
Mike Stump11289f42009-09-09 15:08:12 +0000451 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000452 /// transformed.
453 ///
454 /// For the purposes of template instantiation, a type has already been
455 /// transformed if it is NULL or if it is not dependent.
456 bool AlreadyTransformed(QualType T) {
457 return T.isNull() || !T->isDependentType();
Douglas Gregorf61eca92009-05-13 18:28:20 +0000458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Douglas Gregord6ff3322009-08-04 16:50:30 +0000460 /// \brief Returns the location of the entity being instantiated, if known.
461 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000462
Douglas Gregord6ff3322009-08-04 16:50:30 +0000463 /// \brief Returns the name of the entity being instantiated, if any.
464 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Douglas Gregoref6ab412009-10-27 06:26:26 +0000466 /// \brief Sets the "base" location and entity when that
467 /// information is known based on another transformation.
468 void setBase(SourceLocation Loc, DeclarationName Entity) {
469 this->Loc = Loc;
470 this->Entity = Entity;
471 }
472
Douglas Gregord6ff3322009-08-04 16:50:30 +0000473 /// \brief Transform the given declaration by instantiating a reference to
474 /// this declaration.
475 Decl *TransformDecl(Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000476
Mike Stump11289f42009-09-09 15:08:12 +0000477 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000478 /// instantiating it.
479 Decl *TransformDefinition(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000480
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000481 /// \bried Transform the first qualifier within a scope by instantiating the
482 /// declaration.
483 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
484
Douglas Gregorebe10102009-08-20 07:17:43 +0000485 /// \brief Rebuild the exception declaration and register the declaration
486 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000487 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000488 DeclaratorInfo *Declarator,
489 IdentifierInfo *Name,
490 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000491
John McCall7f41d982009-09-11 04:59:25 +0000492 /// \brief Check for tag mismatches when instantiating an
493 /// elaborated type.
494 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
495
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000496 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E,
497 bool isAddressOfOperand);
498 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E,
499 bool isAddressOfOperand);
Mike Stump11289f42009-09-09 15:08:12 +0000500
Sebastian Redl14236c82009-11-08 13:56:19 +0000501 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E,
502 bool isAddressOfOperand);
503
Mike Stump11289f42009-09-09 15:08:12 +0000504 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000505 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000506 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
507 TemplateTypeParmTypeLoc TL);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000508 };
Douglas Gregor04318252009-07-06 15:59:29 +0000509}
510
Douglas Gregord6ff3322009-08-04 16:50:30 +0000511Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000512 if (!D)
513 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000515 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000516 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000517 TemplateName Template
518 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
519 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000520 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000521 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000522 }
Mike Stump11289f42009-09-09 15:08:12 +0000523
524 // If the corresponding template argument is NULL or non-existent, it's
525 // because we are performing instantiation from explicitly-specified
Douglas Gregor01afeef2009-08-28 20:31:08 +0000526 // template arguments in a function template, but there were some
527 // arguments left unspecified.
Mike Stump11289f42009-09-09 15:08:12 +0000528 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000529 TTP->getPosition()))
530 return D;
Mike Stump11289f42009-09-09 15:08:12 +0000531
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000532 // Fall through to find the instantiated declaration for this template
533 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
Douglas Gregor64621e62009-09-16 18:34:49 +0000536 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000537}
538
Douglas Gregorebe10102009-08-20 07:17:43 +0000539Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000540 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000541 if (!Inst)
542 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Douglas Gregorebe10102009-08-20 07:17:43 +0000544 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
545 return Inst;
546}
547
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000548NamedDecl *
549TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
550 SourceLocation Loc) {
551 // If the first part of the nested-name-specifier was a template type
552 // parameter, instantiate that type parameter down to a tag type.
553 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
554 const TemplateTypeParmType *TTP
555 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
556 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
557 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
558 if (T.isNull())
559 return cast_or_null<NamedDecl>(TransformDecl(D));
560
561 if (const TagType *Tag = T->getAs<TagType>())
562 return Tag->getDecl();
563
564 // The resulting type is not a tag; complain.
565 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
566 return 0;
567 }
568 }
569
570 return cast_or_null<NamedDecl>(TransformDecl(D));
571}
572
Douglas Gregorebe10102009-08-20 07:17:43 +0000573VarDecl *
574TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000575 QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000576 DeclaratorInfo *Declarator,
577 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000578 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000579 SourceRange TypeRange) {
580 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
581 Name, Loc, TypeRange);
582 if (Var && !Var->isInvalidDecl())
583 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
584 return Var;
585}
586
John McCall7f41d982009-09-11 04:59:25 +0000587QualType
588TemplateInstantiator::RebuildElaboratedType(QualType T,
589 ElaboratedType::TagKind Tag) {
590 if (const TagType *TT = T->getAs<TagType>()) {
591 TagDecl* TD = TT->getDecl();
592
593 // FIXME: this location is very wrong; we really need typelocs.
594 SourceLocation TagLocation = TD->getTagKeywordLoc();
595
596 // FIXME: type might be anonymous.
597 IdentifierInfo *Id = TD->getIdentifier();
598
599 // TODO: should we even warn on struct/class mismatches for this? Seems
600 // like it's likely to produce a lot of spurious errors.
601 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
602 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
603 << Id
604 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
605 TD->getKindName());
606 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
607 }
608 }
609
610 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
611}
612
613Sema::OwningExprResult
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000614TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E,
615 bool isAddressOfOperand) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000616 if (!E->isTypeDependent())
617 return SemaRef.Owned(E->Retain());
618
619 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
620 assert(currentDecl && "Must have current function declaration when "
621 "instantiating.");
622
623 PredefinedExpr::IdentType IT = E->getIdentType();
624
625 unsigned Length =
626 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
627
628 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000629 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000630 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
631 ArrayType::Normal, 0);
632 PredefinedExpr *PE =
633 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
634 return getSema().Owned(PE);
635}
636
637Sema::OwningExprResult
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000638TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E,
639 bool isAddressOfOperand) {
Douglas Gregora16548e2009-08-11 05:31:07 +0000640 // FIXME: Clean this up a bit
641 NamedDecl *D = E->getDecl();
642 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor954de172009-10-31 17:21:17 +0000643 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
644
645 // If the corresponding template argument is NULL or non-existent, it's
646 // because we are performing instantiation from explicitly-specified
647 // template arguments in a function template, but there were some
648 // arguments left unspecified.
649 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
650 NTTP->getPosition()))
651 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000652
Douglas Gregor954de172009-10-31 17:21:17 +0000653 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
654 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregor954de172009-10-31 17:21:17 +0000656 // The template argument itself might be an expression, in which
657 // case we just return that expression.
658 if (Arg.getKind() == TemplateArgument::Expression)
659 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregor954de172009-10-31 17:21:17 +0000661 if (Arg.getKind() == TemplateArgument::Declaration) {
662 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000663
Douglas Gregor954de172009-10-31 17:21:17 +0000664 VD = cast_or_null<ValueDecl>(
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000665 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor954de172009-10-31 17:21:17 +0000666 if (!VD)
667 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregor954de172009-10-31 17:21:17 +0000669 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
670 /*FIXME:*/false, /*FIXME:*/false);
671 }
Mike Stump11289f42009-09-09 15:08:12 +0000672
Douglas Gregor954de172009-10-31 17:21:17 +0000673 assert(Arg.getKind() == TemplateArgument::Integral);
674 QualType T = Arg.getIntegralType();
675 if (T->isCharType() || T->isWideCharType())
676 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
677 Arg.getAsIntegral()->getZExtValue(),
678 T->isWideCharType(),
Mike Stump11289f42009-09-09 15:08:12 +0000679 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000680 E->getSourceRange().getBegin()));
Douglas Gregor954de172009-10-31 17:21:17 +0000681 if (T->isBooleanType())
682 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
683 Arg.getAsIntegral()->getBoolValue(),
684 T,
685 E->getSourceRange().getBegin()));
686
687 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
688 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
689 *Arg.getAsIntegral(),
690 T,
691 E->getSourceRange().getBegin()));
692 }
693
694 // We have a non-type template parameter that isn't fully substituted;
695 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000696 }
Mike Stump11289f42009-09-09 15:08:12 +0000697
Douglas Gregor64621e62009-09-16 18:34:49 +0000698 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregora16548e2009-08-11 05:31:07 +0000699 if (!InstD)
700 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000701
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000702 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000703 // we need to get the underlying decl.
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000704 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
705 InstD = InstD->getUnderlyingDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000706
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000707 CXXScopeSpec SS;
708 NestedNameSpecifier *Qualifier = 0;
709 if (E->getQualifier()) {
710 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
711 E->getQualifierRange());
712 if (!Qualifier)
713 return SemaRef.ExprError();
714
715 SS.setScopeRep(Qualifier);
716 SS.setRange(E->getQualifierRange());
717 }
718
Mike Stump11289f42009-09-09 15:08:12 +0000719 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregora16548e2009-08-11 05:31:07 +0000720 /*FIXME:*/false,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000721 &SS,
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000722 isAddressOfOperand);
Douglas Gregora16548e2009-08-11 05:31:07 +0000723}
724
Sebastian Redl14236c82009-11-08 13:56:19 +0000725Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
726 CXXDefaultArgExpr *E, bool isAddressOfOperand) {
727 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
728 getDescribedFunctionTemplate() &&
729 "Default arg expressions are never formed in dependent cases.");
730 return SemaRef.Owned(E->Retain());
731}
732
733
Mike Stump11289f42009-09-09 15:08:12 +0000734QualType
John McCall550e0c22009-10-21 00:40:46 +0000735TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
736 TemplateTypeParmTypeLoc TL) {
737 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000738 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000739 // Replace the template type parameter with its corresponding
740 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000741
742 // If the corresponding template argument is NULL or doesn't exist, it's
743 // because we are performing instantiation from explicitly-specified
744 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000745 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000746 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
747 TemplateTypeParmTypeLoc NewTL
748 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
749 NewTL.setNameLoc(TL.getNameLoc());
750 return TL.getType();
751 }
Mike Stump11289f42009-09-09 15:08:12 +0000752
753 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000754 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000755 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000756
John McCallcebee162009-10-18 09:09:24 +0000757 QualType Replacement
758 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
759
760 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000761 QualType Result
762 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
763 SubstTemplateTypeParmTypeLoc NewTL
764 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
765 NewTL.setNameLoc(TL.getNameLoc());
766 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000767 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000768
769 // The template type parameter comes from an inner template (e.g.,
770 // the template parameter list of a member template inside the
771 // template we are instantiating). Create a new template type
772 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000773 QualType Result
774 = getSema().Context.getTemplateTypeParmType(T->getDepth()
775 - TemplateArgs.getNumLevels(),
776 T->getIndex(),
777 T->isParameterPack(),
778 T->getName());
779 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
780 NewTL.setNameLoc(TL.getNameLoc());
781 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000782}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000783
John McCall76d824f2009-08-25 22:02:44 +0000784/// \brief Perform substitution on the type T with a given set of template
785/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000786///
787/// This routine substitutes the given template arguments into the
788/// type T and produces the instantiated type.
789///
790/// \param T the type into which the template arguments will be
791/// substituted. If this type is not dependent, it will be returned
792/// immediately.
793///
794/// \param TemplateArgs the template arguments that will be
795/// substituted for the top-level template parameters within T.
796///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000797/// \param Loc the location in the source code where this substitution
798/// is being performed. It will typically be the location of the
799/// declarator (if we're instantiating the type of some declaration)
800/// or the location of the type in the source code (if, e.g., we're
801/// instantiating the type of a cast expression).
802///
803/// \param Entity the name of the entity associated with a declaration
804/// being instantiated (if any). May be empty to indicate that there
805/// is no such entity (if, e.g., this is a type that occurs as part of
806/// a cast expression) or that the entity has no name (e.g., an
807/// unnamed function parameter).
808///
809/// \returns If the instantiation succeeds, the instantiated
810/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall609459e2009-10-21 00:58:09 +0000811DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
812 const MultiLevelTemplateArgumentList &Args,
813 SourceLocation Loc,
814 DeclarationName Entity) {
815 assert(!ActiveTemplateInstantiations.empty() &&
816 "Cannot perform an instantiation without some context on the "
817 "instantiation stack");
818
819 if (!T->getType()->isDependentType())
820 return T;
821
822 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
823 return Instantiator.TransformType(T);
824}
825
826/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000827QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000828 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000829 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000830 assert(!ActiveTemplateInstantiations.empty() &&
831 "Cannot perform an instantiation without some context on the "
832 "instantiation stack");
833
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000834 // If T is not a dependent type, there is nothing to do.
835 if (!T->isDependentType())
836 return T;
837
Douglas Gregord6ff3322009-08-04 16:50:30 +0000838 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
839 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000840}
Douglas Gregor463421d2009-03-03 04:44:36 +0000841
John McCall76d824f2009-08-25 22:02:44 +0000842/// \brief Perform substitution on the base class specifiers of the
843/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +0000844///
845/// Produces a diagnostic and returns true on error, returns false and
846/// attaches the instantiated base classes to the class template
847/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +0000848bool
John McCall76d824f2009-08-25 22:02:44 +0000849Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
850 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000851 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000852 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +0000853 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +0000854 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000855 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000856 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000857 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +0000858 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +0000859 continue;
860 }
861
Mike Stump11289f42009-09-09 15:08:12 +0000862 QualType BaseType = SubstType(Base->getType(),
863 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000864 Base->getSourceRange().getBegin(),
865 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +0000866 if (BaseType.isNull()) {
867 Invalid = true;
868 continue;
869 }
870
871 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000872 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000873 Base->getSourceRange(),
874 Base->isVirtual(),
875 Base->getAccessSpecifierAsWritten(),
876 BaseType,
877 /*FIXME: Not totally accurate */
878 Base->getSourceRange().getBegin()))
879 InstantiatedBases.push_back(InstantiatedBase);
880 else
881 Invalid = true;
882 }
883
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000884 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +0000885 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +0000886 InstantiatedBases.size()))
887 Invalid = true;
888
889 return Invalid;
890}
891
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000892/// \brief Instantiate the definition of a class from a given pattern.
893///
894/// \param PointOfInstantiation The point of instantiation within the
895/// source code.
896///
897/// \param Instantiation is the declaration whose definition is being
898/// instantiated. This will be either a class template specialization
899/// or a member class of a class template specialization.
900///
901/// \param Pattern is the pattern from which the instantiation
902/// occurs. This will be either the declaration of a class template or
903/// the declaration of a member class of a class template.
904///
905/// \param TemplateArgs The template arguments to be substituted into
906/// the pattern.
907///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000908/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000909///
910/// \param Complain whether to complain if the class cannot be instantiated due
911/// to the lack of a definition.
912///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000913/// \returns true if an error occurred, false otherwise.
914bool
915Sema::InstantiateClass(SourceLocation PointOfInstantiation,
916 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000917 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000918 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000919 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000920 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +0000921
Mike Stump11289f42009-09-09 15:08:12 +0000922 CXXRecordDecl *PatternDef
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000923 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
924 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000925 if (!Complain) {
926 // Say nothing
927 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000928 Diag(PointOfInstantiation,
929 diag::err_implicit_instantiate_member_undefined)
930 << Context.getTypeDeclType(Instantiation);
931 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
932 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +0000933 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000934 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000935 << Context.getTypeDeclType(Instantiation);
936 Diag(Pattern->getLocation(), diag::note_template_decl_here);
937 }
938 return true;
939 }
940 Pattern = PatternDef;
941
Douglas Gregord6ba93d2009-10-15 15:54:05 +0000942 // \brief Record the point of instantiation.
943 if (MemberSpecializationInfo *MSInfo
944 = Instantiation->getMemberSpecializationInfo()) {
945 MSInfo->setTemplateSpecializationKind(TSK);
946 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +0000947 } else if (ClassTemplateSpecializationDecl *Spec
948 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
949 Spec->setTemplateSpecializationKind(TSK);
950 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +0000951 }
952
Douglas Gregorf3430ae2009-03-25 21:23:52 +0000953 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000954 if (Inst)
955 return true;
956
957 // Enter the scope of this instantiation. We don't use
958 // PushDeclContext because we don't have a scope.
959 DeclContext *PreviousContext = CurContext;
960 CurContext = Instantiation;
961
962 // Start the definition of this instantiation.
963 Instantiation->startDefinition();
964
John McCall76d824f2009-08-25 22:02:44 +0000965 // Do substitution on the base class specifiers.
966 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000967 Invalid = true;
968
Douglas Gregor6181ded2009-05-29 18:27:38 +0000969 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000970 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000971 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000972 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +0000973 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000974 if (NewMember) {
Douglas Gregore62e6a02009-11-11 19:13:48 +0000975 if (NewMember->isInvalidDecl()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000976 Invalid = true;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000977 } else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +0000978 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000979 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
980 Instantiation->addDecl(UD);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000981 } else {
982 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +0000983 // instantiations was a semantic disaster, and we'll want to set Invalid =
984 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000985 }
986 }
987
988 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +0000989 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +0000990 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000991 0);
Douglas Gregor3c74d412009-10-14 20:14:33 +0000992 if (Instantiation->isInvalidDecl())
993 Invalid = true;
994
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000995 // Add any implicitly-declared members that we might need.
Douglas Gregor3c74d412009-10-14 20:14:33 +0000996 if (!Invalid)
997 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000998
999 // Exit the scope of this instantiation.
1000 CurContext = PreviousContext;
1001
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001002 if (!Invalid)
1003 Consumer.HandleTagDeclDefinition(Instantiation);
1004
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001005 return Invalid;
1006}
1007
Mike Stump11289f42009-09-09 15:08:12 +00001008bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001009Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001010 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001011 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001012 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001013 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001014 // Perform the actual instantiation on the canonical declaration.
1015 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001016 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001017
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001018 // Check whether we have already instantiated or specialized this class
1019 // template specialization.
1020 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1021 if (ClassTemplateSpec->getSpecializationKind() ==
1022 TSK_ExplicitInstantiationDeclaration &&
1023 TSK == TSK_ExplicitInstantiationDefinition) {
1024 // An explicit instantiation definition follows an explicit instantiation
1025 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1026 // explicit instantiation.
1027 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001028 return false;
1029 }
1030
1031 // We can only instantiate something that hasn't already been
1032 // instantiated or specialized. Fail without any diagnostics: our
1033 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001034 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001035 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001036
Douglas Gregor00a511f2009-09-15 16:51:42 +00001037 if (ClassTemplateSpec->isInvalidDecl())
1038 return true;
1039
Douglas Gregor463421d2009-03-03 04:44:36 +00001040 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001041 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001042
Douglas Gregor170bc422009-06-12 22:31:52 +00001043 // C++ [temp.class.spec.match]p1:
1044 // When a class template is used in a context that requires an
1045 // instantiation of the class, it is necessary to determine
1046 // whether the instantiation is to be generated using the primary
1047 // template or one of the partial specializations. This is done by
1048 // matching the template arguments of the class template
1049 // specialization with the template argument lists of the partial
1050 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001051 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1052 TemplateArgumentList *> MatchResult;
1053 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump11289f42009-09-09 15:08:12 +00001054 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor2373c592009-05-31 09:31:02 +00001055 Partial = Template->getPartialSpecializations().begin(),
1056 PartialEnd = Template->getPartialSpecializations().end();
1057 Partial != PartialEnd;
1058 ++Partial) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001059 TemplateDeductionInfo Info(Context);
1060 if (TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +00001061 = DeduceTemplateArguments(&*Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001062 ClassTemplateSpec->getTemplateArgs(),
1063 Info)) {
1064 // FIXME: Store the failed-deduction information for use in
1065 // diagnostics, later.
1066 (void)Result;
1067 } else {
1068 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1069 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001070 }
1071
Douglas Gregor21610382009-10-29 00:04:11 +00001072 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001073 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001074 if (Matched.size() == 1) {
1075 // -- If exactly one matching specialization is found, the
1076 // instantiation is generated from that specialization.
1077 // We don't need to do anything for this.
1078 } else {
1079 // -- If more than one matching specialization is found, the
1080 // partial order rules (14.5.4.2) are used to determine
1081 // whether one of the specializations is more specialized
1082 // than the others. If none of the specializations is more
1083 // specialized than all of the other matching
1084 // specializations, then the use of the class template is
1085 // ambiguous and the program is ill-formed.
1086 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1087 PEnd = Matched.end();
1088 P != PEnd; ++P) {
1089 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1090 == P->first)
1091 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001092 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001093
Douglas Gregor21610382009-10-29 00:04:11 +00001094 // Determine if the best partial specialization is more specialized than
1095 // the others.
1096 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001097 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1098 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001099 P != PEnd; ++P) {
1100 if (P != Best &&
1101 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1102 != Best->first) {
1103 Ambiguous = true;
1104 break;
1105 }
1106 }
1107
1108 if (Ambiguous) {
1109 // Partial ordering did not produce a clear winner. Complain.
1110 ClassTemplateSpec->setInvalidDecl();
1111 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1112 << ClassTemplateSpec;
1113
1114 // Print the matching partial specializations.
1115 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1116 PEnd = Matched.end();
1117 P != PEnd; ++P)
1118 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1119 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1120 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001121
Douglas Gregor21610382009-10-29 00:04:11 +00001122 return true;
1123 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001124 }
1125
1126 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001127 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1128 while (OrigPartialSpec->getInstantiatedFromMember()) {
1129 // If we've found an explicit specialization of this class template,
1130 // stop here and use that as the pattern.
1131 if (OrigPartialSpec->isMemberSpecialization())
1132 break;
1133
1134 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1135 }
1136
1137 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001138 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001139 } else {
1140 // -- If no matches are found, the instantiation is generated
1141 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001142 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001143 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1144 // If we've found an explicit specialization of this class template,
1145 // stop here and use that as the pattern.
1146 if (OrigTemplate->isMemberSpecialization())
1147 break;
1148
Douglas Gregor01afeef2009-08-28 20:31:08 +00001149 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001150 }
1151
Douglas Gregor01afeef2009-08-28 20:31:08 +00001152 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001153 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001154
Douglas Gregoref6ab412009-10-27 06:26:26 +00001155 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1156 Pattern,
1157 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001158 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001159 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001160
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001161 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1162 // FIXME: Implement TemplateArgumentList::Destroy!
1163 // if (Matched[I].first != Pattern)
1164 // Matched[I].second->Destroy(Context);
1165 }
Mike Stump11289f42009-09-09 15:08:12 +00001166
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001167 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001168}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001169
John McCall76d824f2009-08-25 22:02:44 +00001170/// \brief Instantiates the definitions of all of the member
1171/// of the given class, which is an instantiation of a class template
1172/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001173void
1174Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001175 CXXRecordDecl *Instantiation,
1176 const MultiLevelTemplateArgumentList &TemplateArgs,
1177 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001178 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1179 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001180 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001181 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001182 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001183 if (FunctionDecl *Pattern
1184 = Function->getInstantiatedFromMemberFunction()) {
1185 MemberSpecializationInfo *MSInfo
1186 = Function->getMemberSpecializationInfo();
1187 assert(MSInfo && "No member specialization information?");
1188 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1189 Function,
1190 MSInfo->getTemplateSpecializationKind(),
1191 MSInfo->getPointOfInstantiation(),
1192 SuppressNew) ||
1193 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001194 continue;
1195
Douglas Gregor1d957a32009-10-27 18:42:08 +00001196 if (Function->getBody())
1197 continue;
1198
1199 if (TSK == TSK_ExplicitInstantiationDefinition) {
1200 // C++0x [temp.explicit]p8:
1201 // An explicit instantiation definition that names a class template
1202 // specialization explicitly instantiates the class template
1203 // specialization and is only an explicit instantiation definition
1204 // of members whose definition is visible at the point of
1205 // instantiation.
1206 if (!Pattern->getBody())
1207 continue;
1208
1209 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1210
1211 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1212 } else {
1213 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1214 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001215 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001216 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001217 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001218 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1219 assert(MSInfo && "No member specialization information?");
1220 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1221 Var,
1222 MSInfo->getTemplateSpecializationKind(),
1223 MSInfo->getPointOfInstantiation(),
1224 SuppressNew) ||
1225 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001226 continue;
1227
Douglas Gregor1d957a32009-10-27 18:42:08 +00001228 if (TSK == TSK_ExplicitInstantiationDefinition) {
1229 // C++0x [temp.explicit]p8:
1230 // An explicit instantiation definition that names a class template
1231 // specialization explicitly instantiates the class template
1232 // specialization and is only an explicit instantiation definition
1233 // of members whose definition is visible at the point of
1234 // instantiation.
1235 if (!Var->getInstantiatedFromStaticDataMember()
1236 ->getOutOfLineDefinition())
1237 continue;
1238
1239 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001240 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001241 } else {
1242 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1243 }
1244 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001245 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregord801b062009-10-07 23:56:10 +00001246 if (Record->isInjectedClassName())
1247 continue;
1248
Douglas Gregor1d957a32009-10-27 18:42:08 +00001249 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1250 assert(MSInfo && "No member specialization information?");
1251 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1252 Record,
1253 MSInfo->getTemplateSpecializationKind(),
1254 MSInfo->getPointOfInstantiation(),
1255 SuppressNew) ||
1256 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001257 continue;
1258
Douglas Gregor1d957a32009-10-27 18:42:08 +00001259 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1260 assert(Pattern && "Missing instantiated-from-template information");
1261
1262 if (!Record->getDefinition(Context)) {
1263 if (!Pattern->getDefinition(Context)) {
1264 // C++0x [temp.explicit]p8:
1265 // An explicit instantiation definition that names a class template
1266 // specialization explicitly instantiates the class template
1267 // specialization and is only an explicit instantiation definition
1268 // of members whose definition is visible at the point of
1269 // instantiation.
1270 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1271 MSInfo->setTemplateSpecializationKind(TSK);
1272 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1273 }
1274
1275 continue;
1276 }
1277
1278 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001279 TemplateArgs,
1280 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001281 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001282
Douglas Gregor1d957a32009-10-27 18:42:08 +00001283 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1284 if (Pattern)
1285 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1286 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001287 }
1288 }
1289}
1290
1291/// \brief Instantiate the definitions of all of the members of the
1292/// given class template specialization, which was named as part of an
1293/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001294void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001295Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001296 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001297 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1298 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001299 // C++0x [temp.explicit]p7:
1300 // An explicit instantiation that names a class template
1301 // specialization is an explicit instantion of the same kind
1302 // (declaration or definition) of each of its members (not
1303 // including members inherited from base classes) that has not
1304 // been previously explicitly specialized in the translation unit
1305 // containing the explicit instantiation, except as described
1306 // below.
1307 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001308 getTemplateInstantiationArgs(ClassTemplateSpec),
1309 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001310}
1311
Mike Stump11289f42009-09-09 15:08:12 +00001312Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001313Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001314 if (!S)
1315 return Owned(S);
1316
1317 TemplateInstantiator Instantiator(*this, TemplateArgs,
1318 SourceLocation(),
1319 DeclarationName());
1320 return Instantiator.TransformStmt(S);
1321}
1322
Mike Stump11289f42009-09-09 15:08:12 +00001323Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001324Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001325 if (!E)
1326 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001327
Douglas Gregora16548e2009-08-11 05:31:07 +00001328 TemplateInstantiator Instantiator(*this, TemplateArgs,
1329 SourceLocation(),
1330 DeclarationName());
1331 return Instantiator.TransformExpr(E);
1332}
1333
John McCall76d824f2009-08-25 22:02:44 +00001334/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001335NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001336Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001337 SourceRange Range,
1338 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001339 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1340 DeclarationName());
1341 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001342}
Douglas Gregoraa594892009-03-31 18:38:02 +00001343
1344TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001345Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001346 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001347 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1348 DeclarationName());
1349 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001350}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001351
John McCall0ad16662009-10-29 08:12:44 +00001352bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1353 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001354 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1355 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001356
1357 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001358}