blob: e79511f9230f930a85f628aba1dec138901f8681 [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 Gregora654dd82009-08-28 17:37:35 +000031MultiLevelTemplateArgumentList
Douglas Gregorb4850462009-05-14 23:26:13 +000032Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregora654dd82009-08-28 17:37:35 +000033 // Accumulate the set of template argument lists in this structure.
34 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000035
Douglas Gregora654dd82009-08-28 17:37:35 +000036 DeclContext *Ctx = dyn_cast<DeclContext>(D);
37 if (!Ctx)
38 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000039
John McCall970d5302009-08-29 03:16:09 +000040 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000041 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000042 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-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 Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregora654dd82009-08-28 17:37:35 +000048 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-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 Stump11289f42009-09-09 15:08:12 +000055 }
Douglas Gregora654dd82009-08-28 17:37:35 +000056 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000057 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorcf915552009-10-13 16:30:37 +000058 if (Function->getTemplateSpecializationKind()
59 == TSK_ExplicitSpecialization)
60 break;
61
Douglas Gregora654dd82009-08-28 17:37:35 +000062 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000063 = Function->getTemplateSpecializationArgs()) {
64 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000065 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000066
Douglas Gregorcf915552009-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 McCall970d5302009-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 Gregora654dd82009-08-28 17:37:35 +000082 }
John McCall970d5302009-08-29 03:16:09 +000083
84 Ctx = Ctx->getParent();
Douglas Gregorb4850462009-05-14 23:26:13 +000085 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Douglas Gregora654dd82009-08-28 17:37:35 +000087 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +000088}
89
Douglas Gregorfcd5db32009-03-10 00:06:19 +000090Sema::InstantiatingTemplate::
91InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +000092 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +000093 SourceRange InstantiationRange)
94 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +000095
96 Invalid = CheckInstantiationDepth(PointOfInstantiation,
97 InstantiationRange);
98 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +000099 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000100 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000101 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000102 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000103 Inst.TemplateArgs = 0;
104 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000105 Inst.InstantiationRange = InstantiationRange;
106 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
107 Invalid = false;
108 }
109}
110
Mike Stump11289f42009-09-09 15:08:12 +0000111Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-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 Stump11289f42009-09-09 15:08:12 +0000123 Inst.Kind
Douglas Gregor79cf6032009-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 Gregorfcd5db32009-03-10 00:06:19 +0000129 Inst.InstantiationRange = InstantiationRange;
130 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
131 Invalid = false;
132 }
133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000136 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-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 Stump11289f42009-09-09 15:08:12 +0000143
Douglas Gregorff6cbdf2009-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 Stump11289f42009-09-09 15:08:12 +0000159Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000160 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-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 Stump11289f42009-09-09 15:08:12 +0000171 Inst.Kind
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000172 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637d9982009-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 Stump11289f42009-09-09 15:08:12 +0000183Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Anders Carlsson657bad42009-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 Stump11289f42009-09-09 15:08:12 +0000190
Anders Carlsson657bad42009-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 Gregor85673582009-05-18 17:01:57 +0000207void Sema::InstantiatingTemplate::Clear() {
208 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000209 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000210 Invalid = true;
211 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000212}
213
Douglas Gregor79cf6032009-03-10 20:44:00 +0000214bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
215 SourceLocation PointOfInstantiation,
216 SourceRange InstantiationRange) {
Mike Stump11289f42009-09-09 15:08:12 +0000217 if (SemaRef.ActiveTemplateInstantiations.size()
Douglas Gregor79cf6032009-03-10 20:44:00 +0000218 <= SemaRef.getLangOptions().InstantiationDepth)
219 return false;
220
Mike Stump11289f42009-09-09 15:08:12 +0000221 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-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 Gregor4ea568f2009-03-10 18:03:33 +0000230/// \brief Prints the current instantiation stack through a series of
231/// notes.
232void Sema::PrintInstantiationStack() {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000233 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor4ea568f2009-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 Gregor79cf6032009-03-10 20:44:00 +0000239 switch (Active->Kind) {
240 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-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 Stump11289f42009-09-09 15:08:12 +0000246 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000247 DiagID)
248 << Context.getTypeDeclType(Record)
249 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000250 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-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 Stump11289f42009-09-09 15:08:12 +0000256 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000257 DiagID)
258 << Function
259 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-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 Gregor85673582009-05-18 17:01:57 +0000265 }
Douglas Gregor79cf6032009-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 Gregordc572a32009-03-30 22:58:21 +0000272 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000273 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000274 Active->NumTemplateArgs,
275 Context.PrintingPolicy);
Douglas Gregor79cf6032009-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 Gregor637d9982009-06-10 23:47:09 +0000282
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000283 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000284 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000285 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000286 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000287 diag::note_explicit_template_arg_substitution_here)
288 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000289 break;
290 }
Mike Stump11289f42009-09-09 15:08:12 +0000291
Douglas Gregorff6cbdf2009-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 Gregor637d9982009-06-10 23:47:09 +0000308
Anders Carlsson657bad42009-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 Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson657bad42009-09-05 05:14:19 +0000313 std::string TemplateArgsStr
314 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000315 Active->TemplateArgs,
Anders Carlsson657bad42009-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 Carlssondc6d2c32009-09-05 05:38:54 +0000320 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000321 << Active->InstantiationRange;
322 break;
323 }
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregor79cf6032009-03-10 20:44:00 +0000325 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000326 }
327}
328
Douglas Gregor33834512009-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 Gregorff6cbdf2009-07-01 22:01:06 +0000338 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000339 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
340
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000341 // This is a template instantiation, so there is no SFINAE.
342 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000343
Douglas Gregor33834512009-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 Stump11289f42009-09-09 15:08:12 +0000348
Douglas Gregorff6cbdf2009-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 Gregor33834512009-06-14 07:33:30 +0000354 }
355 }
356
357 return false;
358}
359
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000360//===----------------------------------------------------------------------===/
361// Template Instantiation for Types
362//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000363namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000364 class VISIBILITY_HIDDEN TemplateInstantiator
365 : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000366 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000367 SourceLocation Loc;
368 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000369
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000370 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000371 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000372
373 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000374 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000375 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000376 DeclarationName Entity)
377 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000378 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000379
Mike Stump11289f42009-09-09 15:08:12 +0000380 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-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 Gregorf61eca92009-05-13 18:28:20 +0000387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Douglas Gregord6ff3322009-08-04 16:50:30 +0000389 /// \brief Returns the location of the entity being instantiated, if known.
390 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Douglas Gregord6ff3322009-08-04 16:50:30 +0000392 /// \brief Returns the name of the entity being instantiated, if any.
393 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Douglas Gregoref6ab412009-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 Gregord6ff3322009-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 Gregora16548e2009-08-11 05:31:07 +0000405
Mike Stump11289f42009-09-09 15:08:12 +0000406 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000407 /// instantiating it.
408 Decl *TransformDefinition(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Douglas Gregora5cb6da2009-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 Gregorebe10102009-08-20 07:17:43 +0000414 /// \brief Rebuild the exception declaration and register the declaration
415 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000416 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000417 DeclaratorInfo *Declarator,
418 IdentifierInfo *Name,
419 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000420
John McCall7f41d982009-09-11 04:59:25 +0000421 /// \brief Check for tag mismatches when instantiating an
422 /// elaborated type.
423 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
424
Anders Carlsson0b209a82009-09-11 01:22:35 +0000425 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000426 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump11289f42009-09-09 15:08:12 +0000427
428 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000429 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000430 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
431 TemplateTypeParmTypeLoc TL);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000432 };
Douglas Gregor04318252009-07-06 15:59:29 +0000433}
434
Douglas Gregord6ff3322009-08-04 16:50:30 +0000435Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000436 if (!D)
437 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000438
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000439 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000440 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
441 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
442 "Wrong kind of template template argument");
Mike Stump11289f42009-09-09 15:08:12 +0000443 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000444 TTP->getPosition()).getAsDecl());
445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
447 // If the corresponding template argument is NULL or non-existent, it's
448 // because we are performing instantiation from explicitly-specified
Douglas Gregor01afeef2009-08-28 20:31:08 +0000449 // template arguments in a function template, but there were some
450 // arguments left unspecified.
Mike Stump11289f42009-09-09 15:08:12 +0000451 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000452 TTP->getPosition()))
453 return D;
Mike Stump11289f42009-09-09 15:08:12 +0000454
Douglas Gregor01afeef2009-08-28 20:31:08 +0000455 // FIXME: Implement depth reduction of template template parameters
Mike Stump11289f42009-09-09 15:08:12 +0000456 assert(false &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000457 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor71dc5092009-08-06 06:41:21 +0000458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Douglas Gregor64621e62009-09-16 18:34:49 +0000460 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000461}
462
Douglas Gregorebe10102009-08-20 07:17:43 +0000463Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000464 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000465 if (!Inst)
466 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Douglas Gregorebe10102009-08-20 07:17:43 +0000468 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
469 return Inst;
470}
471
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000472NamedDecl *
473TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
474 SourceLocation Loc) {
475 // If the first part of the nested-name-specifier was a template type
476 // parameter, instantiate that type parameter down to a tag type.
477 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
478 const TemplateTypeParmType *TTP
479 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
480 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
481 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
482 if (T.isNull())
483 return cast_or_null<NamedDecl>(TransformDecl(D));
484
485 if (const TagType *Tag = T->getAs<TagType>())
486 return Tag->getDecl();
487
488 // The resulting type is not a tag; complain.
489 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
490 return 0;
491 }
492 }
493
494 return cast_or_null<NamedDecl>(TransformDecl(D));
495}
496
Douglas Gregorebe10102009-08-20 07:17:43 +0000497VarDecl *
498TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000499 QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000500 DeclaratorInfo *Declarator,
501 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000502 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000503 SourceRange TypeRange) {
504 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
505 Name, Loc, TypeRange);
506 if (Var && !Var->isInvalidDecl())
507 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
508 return Var;
509}
510
John McCall7f41d982009-09-11 04:59:25 +0000511QualType
512TemplateInstantiator::RebuildElaboratedType(QualType T,
513 ElaboratedType::TagKind Tag) {
514 if (const TagType *TT = T->getAs<TagType>()) {
515 TagDecl* TD = TT->getDecl();
516
517 // FIXME: this location is very wrong; we really need typelocs.
518 SourceLocation TagLocation = TD->getTagKeywordLoc();
519
520 // FIXME: type might be anonymous.
521 IdentifierInfo *Id = TD->getIdentifier();
522
523 // TODO: should we even warn on struct/class mismatches for this? Seems
524 // like it's likely to produce a lot of spurious errors.
525 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
526 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
527 << Id
528 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
529 TD->getKindName());
530 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
531 }
532 }
533
534 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
535}
536
537Sema::OwningExprResult
Anders Carlsson0b209a82009-09-11 01:22:35 +0000538TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
539 if (!E->isTypeDependent())
540 return SemaRef.Owned(E->Retain());
541
542 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
543 assert(currentDecl && "Must have current function declaration when "
544 "instantiating.");
545
546 PredefinedExpr::IdentType IT = E->getIdentType();
547
548 unsigned Length =
549 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
550
551 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000552 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000553 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
554 ArrayType::Normal, 0);
555 PredefinedExpr *PE =
556 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
557 return getSema().Owned(PE);
558}
559
560Sema::OwningExprResult
Douglas Gregora16548e2009-08-11 05:31:07 +0000561TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
562 // FIXME: Clean this up a bit
563 NamedDecl *D = E->getDecl();
564 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor954de172009-10-31 17:21:17 +0000565 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
566
567 // If the corresponding template argument is NULL or non-existent, it's
568 // because we are performing instantiation from explicitly-specified
569 // template arguments in a function template, but there were some
570 // arguments left unspecified.
571 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
572 NTTP->getPosition()))
573 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000574
Douglas Gregor954de172009-10-31 17:21:17 +0000575 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
576 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000577
Douglas Gregor954de172009-10-31 17:21:17 +0000578 // The template argument itself might be an expression, in which
579 // case we just return that expression.
580 if (Arg.getKind() == TemplateArgument::Expression)
581 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000582
Douglas Gregor954de172009-10-31 17:21:17 +0000583 if (Arg.getKind() == TemplateArgument::Declaration) {
584 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000585
Douglas Gregor954de172009-10-31 17:21:17 +0000586 VD = cast_or_null<ValueDecl>(
587 getSema().FindInstantiatedDecl(VD, TemplateArgs));
588 if (!VD)
589 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000590
Douglas Gregor954de172009-10-31 17:21:17 +0000591 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
592 /*FIXME:*/false, /*FIXME:*/false);
593 }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Douglas Gregor954de172009-10-31 17:21:17 +0000595 assert(Arg.getKind() == TemplateArgument::Integral);
596 QualType T = Arg.getIntegralType();
597 if (T->isCharType() || T->isWideCharType())
598 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
599 Arg.getAsIntegral()->getZExtValue(),
600 T->isWideCharType(),
Mike Stump11289f42009-09-09 15:08:12 +0000601 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000602 E->getSourceRange().getBegin()));
Douglas Gregor954de172009-10-31 17:21:17 +0000603 if (T->isBooleanType())
604 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
605 Arg.getAsIntegral()->getBoolValue(),
606 T,
607 E->getSourceRange().getBegin()));
608
609 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
610 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
611 *Arg.getAsIntegral(),
612 T,
613 E->getSourceRange().getBegin()));
614 }
615
616 // We have a non-type template parameter that isn't fully substituted;
617 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregor64621e62009-09-16 18:34:49 +0000620 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregora16548e2009-08-11 05:31:07 +0000621 if (!InstD)
622 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000623
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000624 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000625 // we need to get the underlying decl.
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000626 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
627 InstD = InstD->getUnderlyingDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000628
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000629 CXXScopeSpec SS;
630 NestedNameSpecifier *Qualifier = 0;
631 if (E->getQualifier()) {
632 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
633 E->getQualifierRange());
634 if (!Qualifier)
635 return SemaRef.ExprError();
636
637 SS.setScopeRep(Qualifier);
638 SS.setRange(E->getQualifierRange());
639 }
640
Mike Stump11289f42009-09-09 15:08:12 +0000641 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregora16548e2009-08-11 05:31:07 +0000642 /*FIXME:*/false,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000643 &SS,
Mike Stump11289f42009-09-09 15:08:12 +0000644 /*FIXME:*/false);
Douglas Gregora16548e2009-08-11 05:31:07 +0000645}
646
Mike Stump11289f42009-09-09 15:08:12 +0000647QualType
John McCall550e0c22009-10-21 00:40:46 +0000648TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
649 TemplateTypeParmTypeLoc TL) {
650 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000651 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000652 // Replace the template type parameter with its corresponding
653 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000654
655 // If the corresponding template argument is NULL or doesn't exist, it's
656 // because we are performing instantiation from explicitly-specified
657 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000658 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000659 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
660 TemplateTypeParmTypeLoc NewTL
661 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
662 NewTL.setNameLoc(TL.getNameLoc());
663 return TL.getType();
664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665
666 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000667 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000668 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000669
John McCallcebee162009-10-18 09:09:24 +0000670 QualType Replacement
671 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
672
673 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000674 QualType Result
675 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
676 SubstTemplateTypeParmTypeLoc NewTL
677 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
678 NewTL.setNameLoc(TL.getNameLoc());
679 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000680 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000681
682 // The template type parameter comes from an inner template (e.g.,
683 // the template parameter list of a member template inside the
684 // template we are instantiating). Create a new template type
685 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000686 QualType Result
687 = getSema().Context.getTemplateTypeParmType(T->getDepth()
688 - TemplateArgs.getNumLevels(),
689 T->getIndex(),
690 T->isParameterPack(),
691 T->getName());
692 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
693 NewTL.setNameLoc(TL.getNameLoc());
694 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000695}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000696
John McCall76d824f2009-08-25 22:02:44 +0000697/// \brief Perform substitution on the type T with a given set of template
698/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000699///
700/// This routine substitutes the given template arguments into the
701/// type T and produces the instantiated type.
702///
703/// \param T the type into which the template arguments will be
704/// substituted. If this type is not dependent, it will be returned
705/// immediately.
706///
707/// \param TemplateArgs the template arguments that will be
708/// substituted for the top-level template parameters within T.
709///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000710/// \param Loc the location in the source code where this substitution
711/// is being performed. It will typically be the location of the
712/// declarator (if we're instantiating the type of some declaration)
713/// or the location of the type in the source code (if, e.g., we're
714/// instantiating the type of a cast expression).
715///
716/// \param Entity the name of the entity associated with a declaration
717/// being instantiated (if any). May be empty to indicate that there
718/// is no such entity (if, e.g., this is a type that occurs as part of
719/// a cast expression) or that the entity has no name (e.g., an
720/// unnamed function parameter).
721///
722/// \returns If the instantiation succeeds, the instantiated
723/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall609459e2009-10-21 00:58:09 +0000724DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
725 const MultiLevelTemplateArgumentList &Args,
726 SourceLocation Loc,
727 DeclarationName Entity) {
728 assert(!ActiveTemplateInstantiations.empty() &&
729 "Cannot perform an instantiation without some context on the "
730 "instantiation stack");
731
732 if (!T->getType()->isDependentType())
733 return T;
734
735 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
736 return Instantiator.TransformType(T);
737}
738
739/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000740QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000741 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000742 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000743 assert(!ActiveTemplateInstantiations.empty() &&
744 "Cannot perform an instantiation without some context on the "
745 "instantiation stack");
746
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000747 // If T is not a dependent type, there is nothing to do.
748 if (!T->isDependentType())
749 return T;
750
Douglas Gregord6ff3322009-08-04 16:50:30 +0000751 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
752 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000753}
Douglas Gregor463421d2009-03-03 04:44:36 +0000754
John McCall76d824f2009-08-25 22:02:44 +0000755/// \brief Perform substitution on the base class specifiers of the
756/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +0000757///
758/// Produces a diagnostic and returns true on error, returns false and
759/// attaches the instantiated base classes to the class template
760/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +0000761bool
John McCall76d824f2009-08-25 22:02:44 +0000762Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
763 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000764 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000765 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +0000766 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +0000767 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000768 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000769 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000770 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +0000771 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +0000772 continue;
773 }
774
Mike Stump11289f42009-09-09 15:08:12 +0000775 QualType BaseType = SubstType(Base->getType(),
776 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000777 Base->getSourceRange().getBegin(),
778 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +0000779 if (BaseType.isNull()) {
780 Invalid = true;
781 continue;
782 }
783
784 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000785 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000786 Base->getSourceRange(),
787 Base->isVirtual(),
788 Base->getAccessSpecifierAsWritten(),
789 BaseType,
790 /*FIXME: Not totally accurate */
791 Base->getSourceRange().getBegin()))
792 InstantiatedBases.push_back(InstantiatedBase);
793 else
794 Invalid = true;
795 }
796
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000797 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +0000798 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +0000799 InstantiatedBases.size()))
800 Invalid = true;
801
802 return Invalid;
803}
804
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000805/// \brief Instantiate the definition of a class from a given pattern.
806///
807/// \param PointOfInstantiation The point of instantiation within the
808/// source code.
809///
810/// \param Instantiation is the declaration whose definition is being
811/// instantiated. This will be either a class template specialization
812/// or a member class of a class template specialization.
813///
814/// \param Pattern is the pattern from which the instantiation
815/// occurs. This will be either the declaration of a class template or
816/// the declaration of a member class of a class template.
817///
818/// \param TemplateArgs The template arguments to be substituted into
819/// the pattern.
820///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000821/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000822///
823/// \param Complain whether to complain if the class cannot be instantiated due
824/// to the lack of a definition.
825///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000826/// \returns true if an error occurred, false otherwise.
827bool
828Sema::InstantiateClass(SourceLocation PointOfInstantiation,
829 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000830 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000831 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000832 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000833 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +0000834
Mike Stump11289f42009-09-09 15:08:12 +0000835 CXXRecordDecl *PatternDef
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000836 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
837 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000838 if (!Complain) {
839 // Say nothing
840 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000841 Diag(PointOfInstantiation,
842 diag::err_implicit_instantiate_member_undefined)
843 << Context.getTypeDeclType(Instantiation);
844 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
845 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +0000846 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000847 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000848 << Context.getTypeDeclType(Instantiation);
849 Diag(Pattern->getLocation(), diag::note_template_decl_here);
850 }
851 return true;
852 }
853 Pattern = PatternDef;
854
Douglas Gregord6ba93d2009-10-15 15:54:05 +0000855 // \brief Record the point of instantiation.
856 if (MemberSpecializationInfo *MSInfo
857 = Instantiation->getMemberSpecializationInfo()) {
858 MSInfo->setTemplateSpecializationKind(TSK);
859 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +0000860 } else if (ClassTemplateSpecializationDecl *Spec
861 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
862 Spec->setTemplateSpecializationKind(TSK);
863 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +0000864 }
865
Douglas Gregorf3430ae2009-03-25 21:23:52 +0000866 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000867 if (Inst)
868 return true;
869
870 // Enter the scope of this instantiation. We don't use
871 // PushDeclContext because we don't have a scope.
872 DeclContext *PreviousContext = CurContext;
873 CurContext = Instantiation;
874
875 // Start the definition of this instantiation.
876 Instantiation->startDefinition();
877
John McCall76d824f2009-08-25 22:02:44 +0000878 // Do substitution on the base class specifiers.
879 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000880 Invalid = true;
881
Douglas Gregor6181ded2009-05-29 18:27:38 +0000882 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000883 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000884 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000885 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +0000886 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000887 if (NewMember) {
888 if (NewMember->isInvalidDecl())
889 Invalid = true;
890 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +0000891 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000892 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
893 Instantiation->addDecl(UD);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000894 } else {
895 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +0000896 // instantiations was a semantic disaster, and we'll want to set Invalid =
897 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000898 }
899 }
900
901 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +0000902 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +0000903 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000904 0);
Douglas Gregor3c74d412009-10-14 20:14:33 +0000905 if (Instantiation->isInvalidDecl())
906 Invalid = true;
907
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000908 // Add any implicitly-declared members that we might need.
Douglas Gregor3c74d412009-10-14 20:14:33 +0000909 if (!Invalid)
910 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000911
912 // Exit the scope of this instantiation.
913 CurContext = PreviousContext;
914
Douglas Gregor28ad4b52009-05-26 20:50:29 +0000915 if (!Invalid)
916 Consumer.HandleTagDeclDefinition(Instantiation);
917
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000918 return Invalid;
919}
920
Mike Stump11289f42009-09-09 15:08:12 +0000921bool
Douglas Gregor463421d2009-03-03 04:44:36 +0000922Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +0000923 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000924 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000925 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000926 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000927 // Perform the actual instantiation on the canonical declaration.
928 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000929 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +0000930
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000931 // Check whether we have already instantiated or specialized this class
932 // template specialization.
933 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
934 if (ClassTemplateSpec->getSpecializationKind() ==
935 TSK_ExplicitInstantiationDeclaration &&
936 TSK == TSK_ExplicitInstantiationDefinition) {
937 // An explicit instantiation definition follows an explicit instantiation
938 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
939 // explicit instantiation.
940 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000941 return false;
942 }
943
944 // We can only instantiate something that hasn't already been
945 // instantiated or specialized. Fail without any diagnostics: our
946 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +0000947 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000948 }
Douglas Gregor463421d2009-03-03 04:44:36 +0000949
Douglas Gregor00a511f2009-09-15 16:51:42 +0000950 if (ClassTemplateSpec->isInvalidDecl())
951 return true;
952
Douglas Gregor463421d2009-03-03 04:44:36 +0000953 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000954 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +0000955
Douglas Gregor170bc422009-06-12 22:31:52 +0000956 // C++ [temp.class.spec.match]p1:
957 // When a class template is used in a context that requires an
958 // instantiation of the class, it is necessary to determine
959 // whether the instantiation is to be generated using the primary
960 // template or one of the partial specializations. This is done by
961 // matching the template arguments of the class template
962 // specialization with the template argument lists of the partial
963 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000964 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
965 TemplateArgumentList *> MatchResult;
966 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump11289f42009-09-09 15:08:12 +0000967 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor2373c592009-05-31 09:31:02 +0000968 Partial = Template->getPartialSpecializations().begin(),
969 PartialEnd = Template->getPartialSpecializations().end();
970 Partial != PartialEnd;
971 ++Partial) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000972 TemplateDeductionInfo Info(Context);
973 if (TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +0000974 = DeduceTemplateArguments(&*Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000975 ClassTemplateSpec->getTemplateArgs(),
976 Info)) {
977 // FIXME: Store the failed-deduction information for use in
978 // diagnostics, later.
979 (void)Result;
980 } else {
981 Matched.push_back(std::make_pair(&*Partial, Info.take()));
982 }
Douglas Gregor2373c592009-05-31 09:31:02 +0000983 }
984
Douglas Gregor21610382009-10-29 00:04:11 +0000985 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +0000986 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +0000987 if (Matched.size() == 1) {
988 // -- If exactly one matching specialization is found, the
989 // instantiation is generated from that specialization.
990 // We don't need to do anything for this.
991 } else {
992 // -- If more than one matching specialization is found, the
993 // partial order rules (14.5.4.2) are used to determine
994 // whether one of the specializations is more specialized
995 // than the others. If none of the specializations is more
996 // specialized than all of the other matching
997 // specializations, then the use of the class template is
998 // ambiguous and the program is ill-formed.
999 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1000 PEnd = Matched.end();
1001 P != PEnd; ++P) {
1002 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1003 == P->first)
1004 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001005 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001006
Douglas Gregor21610382009-10-29 00:04:11 +00001007 // Determine if the best partial specialization is more specialized than
1008 // the others.
1009 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001010 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1011 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001012 P != PEnd; ++P) {
1013 if (P != Best &&
1014 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1015 != Best->first) {
1016 Ambiguous = true;
1017 break;
1018 }
1019 }
1020
1021 if (Ambiguous) {
1022 // Partial ordering did not produce a clear winner. Complain.
1023 ClassTemplateSpec->setInvalidDecl();
1024 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1025 << ClassTemplateSpec;
1026
1027 // Print the matching partial specializations.
1028 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1029 PEnd = Matched.end();
1030 P != PEnd; ++P)
1031 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1032 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1033 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001034
Douglas Gregor21610382009-10-29 00:04:11 +00001035 return true;
1036 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001037 }
1038
1039 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001040 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1041 while (OrigPartialSpec->getInstantiatedFromMember()) {
1042 // If we've found an explicit specialization of this class template,
1043 // stop here and use that as the pattern.
1044 if (OrigPartialSpec->isMemberSpecialization())
1045 break;
1046
1047 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1048 }
1049
1050 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001051 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001052 } else {
1053 // -- If no matches are found, the instantiation is generated
1054 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001055 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001056 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1057 // If we've found an explicit specialization of this class template,
1058 // stop here and use that as the pattern.
1059 if (OrigTemplate->isMemberSpecialization())
1060 break;
1061
Douglas Gregor01afeef2009-08-28 20:31:08 +00001062 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001063 }
1064
Douglas Gregor01afeef2009-08-28 20:31:08 +00001065 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001066 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001067
Douglas Gregoref6ab412009-10-27 06:26:26 +00001068 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1069 Pattern,
1070 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001071 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001072 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001073
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001074 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1075 // FIXME: Implement TemplateArgumentList::Destroy!
1076 // if (Matched[I].first != Pattern)
1077 // Matched[I].second->Destroy(Context);
1078 }
Mike Stump11289f42009-09-09 15:08:12 +00001079
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001080 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001081}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001082
John McCall76d824f2009-08-25 22:02:44 +00001083/// \brief Instantiates the definitions of all of the member
1084/// of the given class, which is an instantiation of a class template
1085/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001086void
1087Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001088 CXXRecordDecl *Instantiation,
1089 const MultiLevelTemplateArgumentList &TemplateArgs,
1090 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001091 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1092 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001093 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001094 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001095 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001096 if (FunctionDecl *Pattern
1097 = Function->getInstantiatedFromMemberFunction()) {
1098 MemberSpecializationInfo *MSInfo
1099 = Function->getMemberSpecializationInfo();
1100 assert(MSInfo && "No member specialization information?");
1101 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1102 Function,
1103 MSInfo->getTemplateSpecializationKind(),
1104 MSInfo->getPointOfInstantiation(),
1105 SuppressNew) ||
1106 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001107 continue;
1108
Douglas Gregor1d957a32009-10-27 18:42:08 +00001109 if (Function->getBody())
1110 continue;
1111
1112 if (TSK == TSK_ExplicitInstantiationDefinition) {
1113 // C++0x [temp.explicit]p8:
1114 // An explicit instantiation definition that names a class template
1115 // specialization explicitly instantiates the class template
1116 // specialization and is only an explicit instantiation definition
1117 // of members whose definition is visible at the point of
1118 // instantiation.
1119 if (!Pattern->getBody())
1120 continue;
1121
1122 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1123
1124 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1125 } else {
1126 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1127 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001128 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001129 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001130 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001131 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1132 assert(MSInfo && "No member specialization information?");
1133 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1134 Var,
1135 MSInfo->getTemplateSpecializationKind(),
1136 MSInfo->getPointOfInstantiation(),
1137 SuppressNew) ||
1138 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001139 continue;
1140
Douglas Gregor1d957a32009-10-27 18:42:08 +00001141 if (TSK == TSK_ExplicitInstantiationDefinition) {
1142 // C++0x [temp.explicit]p8:
1143 // An explicit instantiation definition that names a class template
1144 // specialization explicitly instantiates the class template
1145 // specialization and is only an explicit instantiation definition
1146 // of members whose definition is visible at the point of
1147 // instantiation.
1148 if (!Var->getInstantiatedFromStaticDataMember()
1149 ->getOutOfLineDefinition())
1150 continue;
1151
1152 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001153 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001154 } else {
1155 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1156 }
1157 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001158 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregord801b062009-10-07 23:56:10 +00001159 if (Record->isInjectedClassName())
1160 continue;
1161
Douglas Gregor1d957a32009-10-27 18:42:08 +00001162 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1163 assert(MSInfo && "No member specialization information?");
1164 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1165 Record,
1166 MSInfo->getTemplateSpecializationKind(),
1167 MSInfo->getPointOfInstantiation(),
1168 SuppressNew) ||
1169 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001170 continue;
1171
Douglas Gregor1d957a32009-10-27 18:42:08 +00001172 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1173 assert(Pattern && "Missing instantiated-from-template information");
1174
1175 if (!Record->getDefinition(Context)) {
1176 if (!Pattern->getDefinition(Context)) {
1177 // C++0x [temp.explicit]p8:
1178 // An explicit instantiation definition that names a class template
1179 // specialization explicitly instantiates the class template
1180 // specialization and is only an explicit instantiation definition
1181 // of members whose definition is visible at the point of
1182 // instantiation.
1183 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1184 MSInfo->setTemplateSpecializationKind(TSK);
1185 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1186 }
1187
1188 continue;
1189 }
1190
1191 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001192 TemplateArgs,
1193 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001194 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001195
Douglas Gregor1d957a32009-10-27 18:42:08 +00001196 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1197 if (Pattern)
1198 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1199 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001200 }
1201 }
1202}
1203
1204/// \brief Instantiate the definitions of all of the members of the
1205/// given class template specialization, which was named as part of an
1206/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001207void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001208Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001209 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001210 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1211 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001212 // C++0x [temp.explicit]p7:
1213 // An explicit instantiation that names a class template
1214 // specialization is an explicit instantion of the same kind
1215 // (declaration or definition) of each of its members (not
1216 // including members inherited from base classes) that has not
1217 // been previously explicitly specialized in the translation unit
1218 // containing the explicit instantiation, except as described
1219 // below.
1220 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001221 getTemplateInstantiationArgs(ClassTemplateSpec),
1222 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001223}
1224
Mike Stump11289f42009-09-09 15:08:12 +00001225Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001226Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001227 if (!S)
1228 return Owned(S);
1229
1230 TemplateInstantiator Instantiator(*this, TemplateArgs,
1231 SourceLocation(),
1232 DeclarationName());
1233 return Instantiator.TransformStmt(S);
1234}
1235
Mike Stump11289f42009-09-09 15:08:12 +00001236Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001237Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001238 if (!E)
1239 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001240
Douglas Gregora16548e2009-08-11 05:31:07 +00001241 TemplateInstantiator Instantiator(*this, TemplateArgs,
1242 SourceLocation(),
1243 DeclarationName());
1244 return Instantiator.TransformExpr(E);
1245}
1246
John McCall76d824f2009-08-25 22:02:44 +00001247/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001248NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001249Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001250 SourceRange Range,
1251 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001252 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1253 DeclarationName());
1254 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001255}
Douglas Gregoraa594892009-03-31 18:38:02 +00001256
1257TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001258Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001259 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001260 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1261 DeclarationName());
1262 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001263}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001264
John McCall0ad16662009-10-29 08:12:44 +00001265bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1266 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001267 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1268 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001269
1270 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001271}