blob: 912b965507e999cdaed020bb229f79ff8213791d [file] [log] [blame]
Douglas Gregor74296542009-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 Gregor841324a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
Douglas Gregordc18e892009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor74296542009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregorfee85d62009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor8dbd0382009-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 Gregor5b8beb32009-08-28 17:37:35 +000031MultiLevelTemplateArgumentList
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000032Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregor5b8beb32009-08-28 17:37:35 +000033 // Accumulate the set of template argument lists in this structure.
34 MultiLevelTemplateArgumentList Result;
Mike Stump25cf7602009-09-09 15:08:12 +000035
Douglas Gregor5b8beb32009-08-28 17:37:35 +000036 DeclContext *Ctx = dyn_cast<DeclContext>(D);
37 if (!Ctx)
38 Ctx = D->getDeclContext();
Mike Stump25cf7602009-09-09 15:08:12 +000039
John McCall178664c2009-08-29 03:16:09 +000040 while (!Ctx->isFileContext()) {
Douglas Gregor5b8beb32009-08-28 17:37:35 +000041 // Add template arguments from a class template instantiation.
Mike Stump25cf7602009-09-09 15:08:12 +000042 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor5b8beb32009-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 Stump25cf7602009-09-09 15:08:12 +000047
Douglas Gregor5b8beb32009-08-28 17:37:35 +000048 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Mike Stump25cf7602009-09-09 15:08:12 +000049 }
50
Douglas Gregor5b8beb32009-08-28 17:37:35 +000051 // Add template arguments from a function template specialization.
John McCall178664c2009-08-29 03:16:09 +000052 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor5b8beb32009-08-28 17:37:35 +000053 // FIXME: Check whether this is an explicit specialization.
54 if (const TemplateArgumentList *TemplateArgs
55 = Function->getTemplateSpecializationArgs())
56 Result.addOuterTemplateArguments(TemplateArgs);
John McCall178664c2009-08-29 03:16:09 +000057
58 // If this is a friend declaration and it declares an entity at
59 // namespace scope, take arguments from its lexical parent
60 // instead of its semantic parent.
61 if (Function->getFriendObjectKind() &&
62 Function->getDeclContext()->isFileContext()) {
63 Ctx = Function->getLexicalDeclContext();
64 continue;
65 }
Douglas Gregor5b8beb32009-08-28 17:37:35 +000066 }
John McCall178664c2009-08-29 03:16:09 +000067
68 Ctx = Ctx->getParent();
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000069 }
Mike Stump25cf7602009-09-09 15:08:12 +000070
Douglas Gregor5b8beb32009-08-28 17:37:35 +000071 return Result;
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000072}
73
Douglas Gregor375733c2009-03-10 00:06:19 +000074Sema::InstantiatingTemplate::
75InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000076 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000077 SourceRange InstantiationRange)
78 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000079
80 Invalid = CheckInstantiationDepth(PointOfInstantiation,
81 InstantiationRange);
82 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000083 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000084 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000085 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000086 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000087 Inst.TemplateArgs = 0;
88 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000089 Inst.InstantiationRange = InstantiationRange;
90 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
91 Invalid = false;
92 }
93}
94
Mike Stump25cf7602009-09-09 15:08:12 +000095Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor56d25a72009-03-10 20:44:00 +000096 SourceLocation PointOfInstantiation,
97 TemplateDecl *Template,
98 const TemplateArgument *TemplateArgs,
99 unsigned NumTemplateArgs,
100 SourceRange InstantiationRange)
101 : SemaRef(SemaRef) {
102
103 Invalid = CheckInstantiationDepth(PointOfInstantiation,
104 InstantiationRange);
105 if (!Invalid) {
106 ActiveTemplateInstantiation Inst;
Mike Stump25cf7602009-09-09 15:08:12 +0000107 Inst.Kind
Douglas Gregor56d25a72009-03-10 20:44:00 +0000108 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
109 Inst.PointOfInstantiation = PointOfInstantiation;
110 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
111 Inst.TemplateArgs = TemplateArgs;
112 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +0000113 Inst.InstantiationRange = InstantiationRange;
114 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
115 Invalid = false;
116 }
117}
118
Mike Stump25cf7602009-09-09 15:08:12 +0000119Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000120 SourceLocation PointOfInstantiation,
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000121 FunctionTemplateDecl *FunctionTemplate,
122 const TemplateArgument *TemplateArgs,
123 unsigned NumTemplateArgs,
124 ActiveTemplateInstantiation::InstantiationKind Kind,
125 SourceRange InstantiationRange)
126: SemaRef(SemaRef) {
Mike Stump25cf7602009-09-09 15:08:12 +0000127
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000128 Invalid = CheckInstantiationDepth(PointOfInstantiation,
129 InstantiationRange);
130 if (!Invalid) {
131 ActiveTemplateInstantiation Inst;
132 Inst.Kind = Kind;
133 Inst.PointOfInstantiation = PointOfInstantiation;
134 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
135 Inst.TemplateArgs = TemplateArgs;
136 Inst.NumTemplateArgs = NumTemplateArgs;
137 Inst.InstantiationRange = InstantiationRange;
138 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
139 Invalid = false;
140 }
141}
142
Mike Stump25cf7602009-09-09 15:08:12 +0000143Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000144 SourceLocation PointOfInstantiation,
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000145 ClassTemplatePartialSpecializationDecl *PartialSpec,
146 const TemplateArgument *TemplateArgs,
147 unsigned NumTemplateArgs,
148 SourceRange InstantiationRange)
149 : SemaRef(SemaRef) {
150
151 Invalid = CheckInstantiationDepth(PointOfInstantiation,
152 InstantiationRange);
153 if (!Invalid) {
154 ActiveTemplateInstantiation Inst;
Mike Stump25cf7602009-09-09 15:08:12 +0000155 Inst.Kind
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000156 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000157 Inst.PointOfInstantiation = PointOfInstantiation;
158 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
159 Inst.TemplateArgs = TemplateArgs;
160 Inst.NumTemplateArgs = NumTemplateArgs;
161 Inst.InstantiationRange = InstantiationRange;
162 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
163 Invalid = false;
164 }
165}
166
Mike Stump25cf7602009-09-09 15:08:12 +0000167Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Anders Carlsson21d18f22009-09-05 05:14:19 +0000168 SourceLocation PointOfInstantation,
169 ParmVarDecl *Param,
170 const TemplateArgument *TemplateArgs,
171 unsigned NumTemplateArgs,
172 SourceRange InstantiationRange)
173 : SemaRef(SemaRef) {
Mike Stump25cf7602009-09-09 15:08:12 +0000174
Anders Carlsson21d18f22009-09-05 05:14:19 +0000175 Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange);
176
177 if (!Invalid) {
178 ActiveTemplateInstantiation Inst;
179 Inst.Kind
180 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
181 Inst.PointOfInstantiation = PointOfInstantation;
182 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
183 Inst.TemplateArgs = TemplateArgs;
184 Inst.NumTemplateArgs = NumTemplateArgs;
185 Inst.InstantiationRange = InstantiationRange;
186 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
187 Invalid = false;
188 }
189}
190
Douglas Gregorb12249d2009-05-18 17:01:57 +0000191void Sema::InstantiatingTemplate::Clear() {
192 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000193 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000194 Invalid = true;
195 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000196}
197
Douglas Gregor56d25a72009-03-10 20:44:00 +0000198bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
199 SourceLocation PointOfInstantiation,
200 SourceRange InstantiationRange) {
Mike Stump25cf7602009-09-09 15:08:12 +0000201 if (SemaRef.ActiveTemplateInstantiations.size()
Douglas Gregor56d25a72009-03-10 20:44:00 +0000202 <= SemaRef.getLangOptions().InstantiationDepth)
203 return false;
204
Mike Stump25cf7602009-09-09 15:08:12 +0000205 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor56d25a72009-03-10 20:44:00 +0000206 diag::err_template_recursion_depth_exceeded)
207 << SemaRef.getLangOptions().InstantiationDepth
208 << InstantiationRange;
209 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
210 << SemaRef.getLangOptions().InstantiationDepth;
211 return true;
212}
213
Douglas Gregorfee85d62009-03-10 18:03:33 +0000214/// \brief Prints the current instantiation stack through a series of
215/// notes.
216void Sema::PrintInstantiationStack() {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000217 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorfee85d62009-03-10 18:03:33 +0000218 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
219 Active = ActiveTemplateInstantiations.rbegin(),
220 ActiveEnd = ActiveTemplateInstantiations.rend();
221 Active != ActiveEnd;
222 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000223 switch (Active->Kind) {
224 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000225 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
226 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
227 unsigned DiagID = diag::note_template_member_class_here;
228 if (isa<ClassTemplateSpecializationDecl>(Record))
229 DiagID = diag::note_template_class_instantiation_here;
Mike Stump25cf7602009-09-09 15:08:12 +0000230 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorb12249d2009-05-18 17:01:57 +0000231 DiagID)
232 << Context.getTypeDeclType(Record)
233 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000234 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000235 unsigned DiagID;
236 if (Function->getPrimaryTemplate())
237 DiagID = diag::note_function_template_spec_here;
238 else
239 DiagID = diag::note_template_member_function_here;
Mike Stump25cf7602009-09-09 15:08:12 +0000240 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorb12249d2009-05-18 17:01:57 +0000241 DiagID)
242 << Function
243 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000244 } else {
245 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
246 diag::note_template_static_data_member_def_here)
247 << cast<VarDecl>(D)
248 << Active->InstantiationRange;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000249 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000250 break;
251 }
252
253 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
254 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
255 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000256 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump25cf7602009-09-09 15:08:12 +0000257 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000258 Active->NumTemplateArgs,
259 Context.PrintingPolicy);
Douglas Gregor56d25a72009-03-10 20:44:00 +0000260 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
261 diag::note_default_arg_instantiation_here)
262 << (Template->getNameAsString() + TemplateArgsStr)
263 << Active->InstantiationRange;
264 break;
265 }
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000266
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000267 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump25cf7602009-09-09 15:08:12 +0000268 FunctionTemplateDecl *FnTmpl
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000269 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000270 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000271 diag::note_explicit_template_arg_substitution_here)
272 << FnTmpl << Active->InstantiationRange;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000273 break;
274 }
Mike Stump25cf7602009-09-09 15:08:12 +0000275
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000276 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
277 if (ClassTemplatePartialSpecializationDecl *PartialSpec
278 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
279 (Decl *)Active->Entity)) {
280 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
281 diag::note_partial_spec_deduct_instantiation_here)
282 << Context.getTypeDeclType(PartialSpec)
283 << Active->InstantiationRange;
284 } else {
285 FunctionTemplateDecl *FnTmpl
286 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
287 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
288 diag::note_function_template_deduction_instantiation_here)
289 << FnTmpl << Active->InstantiationRange;
290 }
291 break;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000292
Anders Carlsson21d18f22009-09-05 05:14:19 +0000293 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
294 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
295 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump25cf7602009-09-09 15:08:12 +0000296
Anders Carlsson21d18f22009-09-05 05:14:19 +0000297 std::string TemplateArgsStr
298 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump25cf7602009-09-09 15:08:12 +0000299 Active->TemplateArgs,
Anders Carlsson21d18f22009-09-05 05:14:19 +0000300 Active->NumTemplateArgs,
301 Context.PrintingPolicy);
302 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
303 diag::note_default_function_arg_instantiation_here)
Anders Carlssonf608ead2009-09-05 05:38:54 +0000304 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson21d18f22009-09-05 05:14:19 +0000305 << Active->InstantiationRange;
306 break;
307 }
Mike Stump25cf7602009-09-09 15:08:12 +0000308
Douglas Gregor56d25a72009-03-10 20:44:00 +0000309 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000310 }
311}
312
Douglas Gregor95d6c952009-06-14 07:33:30 +0000313bool Sema::isSFINAEContext() const {
314 using llvm::SmallVector;
315 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
316 Active = ActiveTemplateInstantiations.rbegin(),
317 ActiveEnd = ActiveTemplateInstantiations.rend();
318 Active != ActiveEnd;
319 ++Active) {
320
321 switch(Active->Kind) {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000322 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson21d18f22009-09-05 05:14:19 +0000323 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
324
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000325 // This is a template instantiation, so there is no SFINAE.
326 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000327
Douglas Gregor95d6c952009-06-14 07:33:30 +0000328 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
329 // A default template argument instantiation may or may not be a
330 // SFINAE context; look further up the stack.
331 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000332
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000333 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
334 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
335 // We're either substitution explicitly-specified template arguments
336 // or deduced template arguments, so SFINAE applies.
337 return true;
Douglas Gregor95d6c952009-06-14 07:33:30 +0000338 }
339 }
340
341 return false;
342}
343
Douglas Gregor74296542009-02-27 19:31:52 +0000344//===----------------------------------------------------------------------===/
345// Template Instantiation for Types
346//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000347namespace {
Mike Stump25cf7602009-09-09 15:08:12 +0000348 class VISIBILITY_HIDDEN TemplateInstantiator
349 : public TreeTransform<TemplateInstantiator> {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000350 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000351 SourceLocation Loc;
352 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000353
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000354 public:
Douglas Gregor23a44be2009-08-20 07:17:43 +0000355 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump25cf7602009-09-09 15:08:12 +0000356
357 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000358 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor841324a2009-08-04 16:50:30 +0000359 SourceLocation Loc,
Mike Stump25cf7602009-09-09 15:08:12 +0000360 DeclarationName Entity)
361 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor23a44be2009-08-20 07:17:43 +0000362 Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000363
Mike Stump25cf7602009-09-09 15:08:12 +0000364 /// \brief Determine whether the given type \p T has already been
Douglas Gregor841324a2009-08-04 16:50:30 +0000365 /// transformed.
366 ///
367 /// For the purposes of template instantiation, a type has already been
368 /// transformed if it is NULL or if it is not dependent.
369 bool AlreadyTransformed(QualType T) {
370 return T.isNull() || !T->isDependentType();
Douglas Gregor90177912009-05-13 18:28:20 +0000371 }
Mike Stump25cf7602009-09-09 15:08:12 +0000372
Douglas Gregor841324a2009-08-04 16:50:30 +0000373 /// \brief Returns the location of the entity being instantiated, if known.
374 SourceLocation getBaseLocation() { return Loc; }
Mike Stump25cf7602009-09-09 15:08:12 +0000375
Douglas Gregor841324a2009-08-04 16:50:30 +0000376 /// \brief Returns the name of the entity being instantiated, if any.
377 DeclarationName getBaseEntity() { return Entity; }
Mike Stump25cf7602009-09-09 15:08:12 +0000378
Douglas Gregor841324a2009-08-04 16:50:30 +0000379 /// \brief Transform the given declaration by instantiating a reference to
380 /// this declaration.
381 Decl *TransformDecl(Decl *D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000382
Mike Stump25cf7602009-09-09 15:08:12 +0000383 /// \brief Transform the definition of the given declaration by
Douglas Gregor23a44be2009-08-20 07:17:43 +0000384 /// instantiating it.
385 Decl *TransformDefinition(Decl *D);
Mike Stump25cf7602009-09-09 15:08:12 +0000386
Douglas Gregor23a44be2009-08-20 07:17:43 +0000387 /// \brief Rebuild the exception declaration and register the declaration
388 /// as an instantiated local.
Mike Stump25cf7602009-09-09 15:08:12 +0000389 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregor23a44be2009-08-20 07:17:43 +0000390 DeclaratorInfo *Declarator,
391 IdentifierInfo *Name,
392 SourceLocation Loc, SourceRange TypeRange);
Mike Stump25cf7602009-09-09 15:08:12 +0000393
Douglas Gregor9d879762009-08-11 05:31:07 +0000394 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump25cf7602009-09-09 15:08:12 +0000395
396 /// \brief Transforms a template type parameter type by performing
Douglas Gregor841324a2009-08-04 16:50:30 +0000397 /// substitution of the corresponding template type argument.
398 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
399 };
Douglas Gregor1d381132009-07-06 15:59:29 +0000400}
401
Douglas Gregor841324a2009-08-04 16:50:30 +0000402Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000403 if (!D)
404 return 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000405
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000406 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000407 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
408 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
409 "Wrong kind of template template argument");
Mike Stump25cf7602009-09-09 15:08:12 +0000410 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000411 TTP->getPosition()).getAsDecl());
412 }
Mike Stump25cf7602009-09-09 15:08:12 +0000413
414 // If the corresponding template argument is NULL or non-existent, it's
415 // because we are performing instantiation from explicitly-specified
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000416 // template arguments in a function template, but there were some
417 // arguments left unspecified.
Mike Stump25cf7602009-09-09 15:08:12 +0000418 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000419 TTP->getPosition()))
420 return D;
Mike Stump25cf7602009-09-09 15:08:12 +0000421
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000422 // FIXME: Implement depth reduction of template template parameters
Mike Stump25cf7602009-09-09 15:08:12 +0000423 assert(false &&
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000424 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor214d0462009-08-06 06:41:21 +0000425 }
Mike Stump25cf7602009-09-09 15:08:12 +0000426
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000427 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D));
Douglas Gregor841324a2009-08-04 16:50:30 +0000428}
429
Douglas Gregor23a44be2009-08-20 07:17:43 +0000430Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall0ba26ee2009-08-25 22:02:44 +0000431 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor23a44be2009-08-20 07:17:43 +0000432 if (!Inst)
433 return 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000434
Douglas Gregor23a44be2009-08-20 07:17:43 +0000435 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
436 return Inst;
437}
438
439VarDecl *
440TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump25cf7602009-09-09 15:08:12 +0000441 QualType T,
Douglas Gregor23a44be2009-08-20 07:17:43 +0000442 DeclaratorInfo *Declarator,
443 IdentifierInfo *Name,
Mike Stump25cf7602009-09-09 15:08:12 +0000444 SourceLocation Loc,
Douglas Gregor23a44be2009-08-20 07:17:43 +0000445 SourceRange TypeRange) {
446 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
447 Name, Loc, TypeRange);
448 if (Var && !Var->isInvalidDecl())
449 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
450 return Var;
451}
452
Mike Stump25cf7602009-09-09 15:08:12 +0000453Sema::OwningExprResult
Douglas Gregor9d879762009-08-11 05:31:07 +0000454TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
455 // FIXME: Clean this up a bit
456 NamedDecl *D = E->getDecl();
457 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000458 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
459 assert(false && "Cannot reduce non-type template parameter depth yet");
460 return getSema().ExprError();
461 }
Mike Stump25cf7602009-09-09 15:08:12 +0000462
463 // If the corresponding template argument is NULL or non-existent, it's
464 // because we are performing instantiation from explicitly-specified
Douglas Gregor9d879762009-08-11 05:31:07 +0000465 // template arguments in a function template, but there were some
466 // arguments left unspecified.
Mike Stump25cf7602009-09-09 15:08:12 +0000467 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000468 NTTP->getPosition()))
469 return SemaRef.Owned(E->Retain());
Mike Stump25cf7602009-09-09 15:08:12 +0000470
471 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000472 NTTP->getPosition());
Mike Stump25cf7602009-09-09 15:08:12 +0000473
Douglas Gregor9d879762009-08-11 05:31:07 +0000474 // The template argument itself might be an expression, in which
475 // case we just return that expression.
476 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000477 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump25cf7602009-09-09 15:08:12 +0000478
Douglas Gregor9d879762009-08-11 05:31:07 +0000479 if (Arg.getKind() == TemplateArgument::Declaration) {
480 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump25cf7602009-09-09 15:08:12 +0000481
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000482 VD = cast_or_null<ValueDecl>(getSema().FindInstantiatedDecl(VD));
483 if (!VD)
484 return SemaRef.ExprError();
Mike Stump25cf7602009-09-09 15:08:12 +0000485
486 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000487 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregor9d879762009-08-11 05:31:07 +0000488 }
Mike Stump25cf7602009-09-09 15:08:12 +0000489
Douglas Gregor9d879762009-08-11 05:31:07 +0000490 assert(Arg.getKind() == TemplateArgument::Integral);
491 QualType T = Arg.getIntegralType();
492 if (T->isCharType() || T->isWideCharType())
493 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000494 Arg.getAsIntegral()->getZExtValue(),
495 T->isWideCharType(),
Mike Stump25cf7602009-09-09 15:08:12 +0000496 T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000497 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000498 if (T->isBooleanType())
499 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000500 Arg.getAsIntegral()->getBoolValue(),
Mike Stump25cf7602009-09-09 15:08:12 +0000501 T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000502 E->getSourceRange().getBegin()));
Mike Stump25cf7602009-09-09 15:08:12 +0000503
Douglas Gregor9d879762009-08-11 05:31:07 +0000504 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
505 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000506 *Arg.getAsIntegral(),
Mike Stump25cf7602009-09-09 15:08:12 +0000507 T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000508 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000509 }
Mike Stump25cf7602009-09-09 15:08:12 +0000510
John McCall0ba26ee2009-08-25 22:02:44 +0000511 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000512 if (!InstD)
513 return SemaRef.ExprError();
Mike Stump25cf7602009-09-09 15:08:12 +0000514
Anders Carlsson8197c482009-08-29 19:37:28 +0000515 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump25cf7602009-09-09 15:08:12 +0000516 // we need to get the underlying decl.
Anders Carlsson8197c482009-08-29 19:37:28 +0000517 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
518 InstD = InstD->getUnderlyingDecl();
Mike Stump25cf7602009-09-09 15:08:12 +0000519
Douglas Gregor9d879762009-08-11 05:31:07 +0000520 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
Mike Stump25cf7602009-09-09 15:08:12 +0000521 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregor9d879762009-08-11 05:31:07 +0000522 /*FIXME:*/false,
Mike Stump25cf7602009-09-09 15:08:12 +0000523 /*FIXME:*/0,
524 /*FIXME:*/false);
Douglas Gregor9d879762009-08-11 05:31:07 +0000525}
526
Mike Stump25cf7602009-09-09 15:08:12 +0000527QualType
Douglas Gregor841324a2009-08-04 16:50:30 +0000528TemplateInstantiator::TransformTemplateTypeParmType(
529 const TemplateTypeParmType *T) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000530 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor74296542009-02-27 19:31:52 +0000531 // Replace the template type parameter with its corresponding
532 // template argument.
Mike Stump25cf7602009-09-09 15:08:12 +0000533
534 // If the corresponding template argument is NULL or doesn't exist, it's
535 // because we are performing instantiation from explicitly-specified
536 // template arguments in a function template class, but there were some
Douglas Gregorecd63b82009-07-01 00:28:38 +0000537 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000538 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
539 return QualType(T, 0);
Mike Stump25cf7602009-09-09 15:08:12 +0000540
541 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000542 == TemplateArgument::Type &&
Douglas Gregor74296542009-02-27 19:31:52 +0000543 "Template argument kind mismatch");
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000544
545 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Mike Stump25cf7602009-09-09 15:08:12 +0000546 }
Douglas Gregor74296542009-02-27 19:31:52 +0000547
548 // The template type parameter comes from an inner template (e.g.,
549 // the template parameter list of a member template inside the
550 // template we are instantiating). Create a new template type
551 // parameter with the template "level" reduced by one.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000552 return getSema().Context.getTemplateTypeParmType(
553 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor841324a2009-08-04 16:50:30 +0000554 T->getIndex(),
555 T->isParameterPack(),
556 T->getName());
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000557}
Douglas Gregor74296542009-02-27 19:31:52 +0000558
John McCall0ba26ee2009-08-25 22:02:44 +0000559/// \brief Perform substitution on the type T with a given set of template
560/// arguments.
Douglas Gregor74296542009-02-27 19:31:52 +0000561///
562/// This routine substitutes the given template arguments into the
563/// type T and produces the instantiated type.
564///
565/// \param T the type into which the template arguments will be
566/// substituted. If this type is not dependent, it will be returned
567/// immediately.
568///
569/// \param TemplateArgs the template arguments that will be
570/// substituted for the top-level template parameters within T.
571///
Douglas Gregor74296542009-02-27 19:31:52 +0000572/// \param Loc the location in the source code where this substitution
573/// is being performed. It will typically be the location of the
574/// declarator (if we're instantiating the type of some declaration)
575/// or the location of the type in the source code (if, e.g., we're
576/// instantiating the type of a cast expression).
577///
578/// \param Entity the name of the entity associated with a declaration
579/// being instantiated (if any). May be empty to indicate that there
580/// is no such entity (if, e.g., this is a type that occurs as part of
581/// a cast expression) or that the entity has no name (e.g., an
582/// unnamed function parameter).
583///
584/// \returns If the instantiation succeeds, the instantiated
585/// type. Otherwise, produces diagnostics and returns a NULL type.
Mike Stump25cf7602009-09-09 15:08:12 +0000586QualType Sema::SubstType(QualType T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000587 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall0ba26ee2009-08-25 22:02:44 +0000588 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000589 assert(!ActiveTemplateInstantiations.empty() &&
590 "Cannot perform an instantiation without some context on the "
591 "instantiation stack");
592
Douglas Gregor74296542009-02-27 19:31:52 +0000593 // If T is not a dependent type, there is nothing to do.
594 if (!T->isDependentType())
595 return T;
596
Douglas Gregor841324a2009-08-04 16:50:30 +0000597 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
598 return Instantiator.TransformType(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000599}
Douglas Gregored3a3982009-03-03 04:44:36 +0000600
John McCall0ba26ee2009-08-25 22:02:44 +0000601/// \brief Perform substitution on the base class specifiers of the
602/// given class template specialization.
Douglas Gregored3a3982009-03-03 04:44:36 +0000603///
604/// Produces a diagnostic and returns true on error, returns false and
605/// attaches the instantiated base classes to the class template
606/// specialization if successful.
Mike Stump25cf7602009-09-09 15:08:12 +0000607bool
John McCall0ba26ee2009-08-25 22:02:44 +0000608Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
609 CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000610 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000611 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000612 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump25cf7602009-09-09 15:08:12 +0000613 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregorcc887972009-03-25 21:17:03 +0000614 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000615 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000616 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +0000617 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregored3a3982009-03-03 04:44:36 +0000618 continue;
619 }
620
Mike Stump25cf7602009-09-09 15:08:12 +0000621 QualType BaseType = SubstType(Base->getType(),
622 TemplateArgs,
John McCall0ba26ee2009-08-25 22:02:44 +0000623 Base->getSourceRange().getBegin(),
624 DeclarationName());
Douglas Gregored3a3982009-03-03 04:44:36 +0000625 if (BaseType.isNull()) {
626 Invalid = true;
627 continue;
628 }
629
630 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000631 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000632 Base->getSourceRange(),
633 Base->isVirtual(),
634 Base->getAccessSpecifierAsWritten(),
635 BaseType,
636 /*FIXME: Not totally accurate */
637 Base->getSourceRange().getBegin()))
638 InstantiatedBases.push_back(InstantiatedBase);
639 else
640 Invalid = true;
641 }
642
Douglas Gregord9572a12009-03-10 18:52:44 +0000643 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000644 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000645 InstantiatedBases.size()))
646 Invalid = true;
647
648 return Invalid;
649}
650
Douglas Gregorcc887972009-03-25 21:17:03 +0000651/// \brief Instantiate the definition of a class from a given pattern.
652///
653/// \param PointOfInstantiation The point of instantiation within the
654/// source code.
655///
656/// \param Instantiation is the declaration whose definition is being
657/// instantiated. This will be either a class template specialization
658/// or a member class of a class template specialization.
659///
660/// \param Pattern is the pattern from which the instantiation
661/// occurs. This will be either the declaration of a class template or
662/// the declaration of a member class of a class template.
663///
664/// \param TemplateArgs The template arguments to be substituted into
665/// the pattern.
666///
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000667/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregorb35c7992009-08-24 15:23:48 +0000668///
669/// \param Complain whether to complain if the class cannot be instantiated due
670/// to the lack of a definition.
671///
Douglas Gregorcc887972009-03-25 21:17:03 +0000672/// \returns true if an error occurred, false otherwise.
673bool
674Sema::InstantiateClass(SourceLocation PointOfInstantiation,
675 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000676 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000677 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000678 bool Complain) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000679 bool Invalid = false;
John McCalld64b41e2009-08-20 01:44:21 +0000680
Mike Stump25cf7602009-09-09 15:08:12 +0000681 CXXRecordDecl *PatternDef
Douglas Gregorcc887972009-03-25 21:17:03 +0000682 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
683 if (!PatternDef) {
Douglas Gregorb35c7992009-08-24 15:23:48 +0000684 if (!Complain) {
685 // Say nothing
686 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000687 Diag(PointOfInstantiation,
688 diag::err_implicit_instantiate_member_undefined)
689 << Context.getTypeDeclType(Instantiation);
690 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
691 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000692 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000693 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregorcc887972009-03-25 21:17:03 +0000694 << Context.getTypeDeclType(Instantiation);
695 Diag(Pattern->getLocation(), diag::note_template_decl_here);
696 }
697 return true;
698 }
699 Pattern = PatternDef;
700
Douglas Gregor42c48522009-03-25 21:23:52 +0000701 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000702 if (Inst)
703 return true;
704
705 // Enter the scope of this instantiation. We don't use
706 // PushDeclContext because we don't have a scope.
707 DeclContext *PreviousContext = CurContext;
708 CurContext = Instantiation;
709
710 // Start the definition of this instantiation.
711 Instantiation->startDefinition();
712
John McCall0ba26ee2009-08-25 22:02:44 +0000713 // Do substitution on the base class specifiers.
714 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000715 Invalid = true;
716
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000717 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000718 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump25cf7602009-09-09 15:08:12 +0000719 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000720 Member != MemberEnd; ++Member) {
John McCall0ba26ee2009-08-25 22:02:44 +0000721 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000722 if (NewMember) {
723 if (NewMember->isInvalidDecl())
724 Invalid = true;
725 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000726 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson8197c482009-08-29 19:37:28 +0000727 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
728 Instantiation->addDecl(UD);
Douglas Gregorcc887972009-03-25 21:17:03 +0000729 } else {
730 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000731 // instantiations was a semantic disaster, and we'll want to set Invalid =
732 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000733 }
734 }
735
736 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000737 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000738 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000739 0);
740
741 // Add any implicitly-declared members that we might need.
742 AddImplicitlyDeclaredMembersToClass(Instantiation);
743
744 // Exit the scope of this instantiation.
745 CurContext = PreviousContext;
746
Douglas Gregordc18e892009-05-26 20:50:29 +0000747 if (!Invalid)
748 Consumer.HandleTagDeclDefinition(Instantiation);
749
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000750 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000751 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000752 Inst.Clear();
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000753 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
754 TSK);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000755 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000756
Douglas Gregorcc887972009-03-25 21:17:03 +0000757 return Invalid;
758}
759
Mike Stump25cf7602009-09-09 15:08:12 +0000760bool
Douglas Gregored3a3982009-03-03 04:44:36 +0000761Sema::InstantiateClassTemplateSpecialization(
762 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000763 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000764 bool Complain) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000765 // Perform the actual instantiation on the canonical declaration.
766 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +0000767 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregored3a3982009-03-03 04:44:36 +0000768
769 // We can only instantiate something that hasn't already been
770 // instantiated or specialized. Fail without any diagnostics: our
771 // caller will provide an error message.
772 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
773 return true;
774
Douglas Gregored3a3982009-03-03 04:44:36 +0000775 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000776 CXXRecordDecl *Pattern = 0;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000777
Douglas Gregor21530c52009-06-12 22:31:52 +0000778 // C++ [temp.class.spec.match]p1:
779 // When a class template is used in a context that requires an
780 // instantiation of the class, it is necessary to determine
781 // whether the instantiation is to be generated using the primary
782 // template or one of the partial specializations. This is done by
783 // matching the template arguments of the class template
784 // specialization with the template argument lists of the partial
785 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000786 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
787 TemplateArgumentList *> MatchResult;
788 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump25cf7602009-09-09 15:08:12 +0000789 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor58944ac2009-05-31 09:31:02 +0000790 Partial = Template->getPartialSpecializations().begin(),
791 PartialEnd = Template->getPartialSpecializations().end();
792 Partial != PartialEnd;
793 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000794 TemplateDeductionInfo Info(Context);
795 if (TemplateDeductionResult Result
Mike Stump25cf7602009-09-09 15:08:12 +0000796 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000797 ClassTemplateSpec->getTemplateArgs(),
798 Info)) {
799 // FIXME: Store the failed-deduction information for use in
800 // diagnostics, later.
801 (void)Result;
802 } else {
803 Matched.push_back(std::make_pair(&*Partial, Info.take()));
804 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000805 }
806
807 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000808 // -- If exactly one matching specialization is found, the
809 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000810 Pattern = Matched[0].first;
Douglas Gregor7b0b83f2009-08-02 23:24:31 +0000811 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor58944ac2009-05-31 09:31:02 +0000812 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000813 // -- If more than one matching specialization is found, the
814 // partial order rules (14.5.4.2) are used to determine
815 // whether one of the specializations is more specialized
816 // than the others. If none of the specializations is more
817 // specialized than all of the other matching
818 // specializations, then the use of the class template is
819 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000820 // FIXME: Implement partial ordering of class template partial
821 // specializations.
Mike Stump25cf7602009-09-09 15:08:12 +0000822 Diag(ClassTemplateSpec->getLocation(),
Douglas Gregor58944ac2009-05-31 09:31:02 +0000823 diag::unsup_template_partial_spec_ordering);
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000824
825 // FIXME: Temporary hack to fall back to the primary template
826 ClassTemplateDecl *OrigTemplate = Template;
827 while (OrigTemplate->getInstantiatedFromMemberTemplate())
828 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Mike Stump25cf7602009-09-09 15:08:12 +0000829
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000830 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor21530c52009-06-12 22:31:52 +0000831 } else {
832 // -- If no matches are found, the instantiation is generated
833 // from the primary template.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000834 ClassTemplateDecl *OrigTemplate = Template;
835 while (OrigTemplate->getInstantiatedFromMemberTemplate())
836 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Mike Stump25cf7602009-09-09 15:08:12 +0000837
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000838 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000839 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000840
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000841 // Note that this is an instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000842 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregored3a3982009-03-03 04:44:36 +0000843
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000844 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
Mike Stump25cf7602009-09-09 15:08:12 +0000845 ClassTemplateSpec, Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000846 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000847 TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000848 Complain);
Mike Stump25cf7602009-09-09 15:08:12 +0000849
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000850 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
851 // FIXME: Implement TemplateArgumentList::Destroy!
852 // if (Matched[I].first != Pattern)
853 // Matched[I].second->Destroy(Context);
854 }
Mike Stump25cf7602009-09-09 15:08:12 +0000855
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000856 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +0000857}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000858
John McCall0ba26ee2009-08-25 22:02:44 +0000859/// \brief Instantiates the definitions of all of the member
860/// of the given class, which is an instantiation of a class template
861/// or a member class of a template.
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000862void
863Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000864 CXXRecordDecl *Instantiation,
865 const MultiLevelTemplateArgumentList &TemplateArgs,
866 TemplateSpecializationKind TSK) {
867 // FIXME: extern templates
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000868 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
869 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000870 D != DEnd; ++D) {
871 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000872 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +0000873 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000874 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor181fe792009-07-24 20:34:43 +0000875 if (Var->isStaticDataMember())
876 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000877 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
878 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
Mike Stump25cf7602009-09-09 15:08:12 +0000879 assert(Record->getInstantiatedFromMemberClass() &&
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000880 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000881 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000882 Record->getInstantiatedFromMemberClass(),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000883 TemplateArgs,
884 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000885 }
886 }
887 }
888}
889
890/// \brief Instantiate the definitions of all of the members of the
891/// given class template specialization, which was named as part of an
892/// explicit instantiation.
Mike Stump25cf7602009-09-09 15:08:12 +0000893void
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000894Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000895 SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000896 ClassTemplateSpecializationDecl *ClassTemplateSpec,
897 TemplateSpecializationKind TSK) {
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000898 // C++0x [temp.explicit]p7:
899 // An explicit instantiation that names a class template
900 // specialization is an explicit instantion of the same kind
901 // (declaration or definition) of each of its members (not
902 // including members inherited from base classes) that has not
903 // been previously explicitly specialized in the translation unit
904 // containing the explicit instantiation, except as described
905 // below.
906 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000907 getTemplateInstantiationArgs(ClassTemplateSpec),
908 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000909}
910
Mike Stump25cf7602009-09-09 15:08:12 +0000911Sema::OwningStmtResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000912Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor23a44be2009-08-20 07:17:43 +0000913 if (!S)
914 return Owned(S);
915
916 TemplateInstantiator Instantiator(*this, TemplateArgs,
917 SourceLocation(),
918 DeclarationName());
919 return Instantiator.TransformStmt(S);
920}
921
Mike Stump25cf7602009-09-09 15:08:12 +0000922Sema::OwningExprResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000923Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor9d879762009-08-11 05:31:07 +0000924 if (!E)
925 return Owned(E);
Mike Stump25cf7602009-09-09 15:08:12 +0000926
Douglas Gregor9d879762009-08-11 05:31:07 +0000927 TemplateInstantiator Instantiator(*this, TemplateArgs,
928 SourceLocation(),
929 DeclarationName());
930 return Instantiator.TransformExpr(E);
931}
932
John McCall0ba26ee2009-08-25 22:02:44 +0000933/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000934NestedNameSpecifier *
John McCall0ba26ee2009-08-25 22:02:44 +0000935Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000936 SourceRange Range,
937 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor12431cb2009-08-06 05:28:30 +0000938 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
939 DeclarationName());
940 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000941}
Douglas Gregor15a92852009-03-31 18:38:02 +0000942
943TemplateName
John McCall0ba26ee2009-08-25 22:02:44 +0000944Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000945 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000946 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
947 DeclarationName());
948 return Instantiator.TransformTemplateName(Name);
Douglas Gregor15a92852009-03-31 18:38:02 +0000949}
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000950
Mike Stump25cf7602009-09-09 15:08:12 +0000951TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000952 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2999faa2009-08-04 22:27:00 +0000953 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
954 DeclarationName());
955 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000956}