blob: ec00d9805c095715286776f107b4a38be8d7fed0 [file] [log] [blame]
Douglas Gregor99ebf652009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregor577f75a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregoree1828a2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregord6350ae2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregord1102432009-08-28 17:37:35 +000031MultiLevelTemplateArgumentList
Douglas Gregor54dabfc2009-05-14 23:26:13 +000032Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregord1102432009-08-28 17:37:35 +000033 // Accumulate the set of template argument lists in this structure.
34 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Douglas Gregord1102432009-08-28 17:37:35 +000036 DeclContext *Ctx = dyn_cast<DeclContext>(D);
37 if (!Ctx)
38 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000039
John McCallf181d8a2009-08-29 03:16:09 +000040 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000041 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000042 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-08-28 17:37:35 +000043 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
44 // We're done when we hit an explicit specialization.
45 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
46 break;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregord1102432009-08-28 17:37:35 +000048 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Mike Stump1eb44332009-09-09 15:08:12 +000049 }
50
Douglas Gregord1102432009-08-28 17:37:35 +000051 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000052 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregord1102432009-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 McCallf181d8a2009-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 Gregord1102432009-08-28 17:37:35 +000066 }
John McCallf181d8a2009-08-29 03:16:09 +000067
68 Ctx = Ctx->getParent();
Douglas Gregor54dabfc2009-05-14 23:26:13 +000069 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Douglas Gregord1102432009-08-28 17:37:35 +000071 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +000072}
73
Douglas Gregor26dce442009-03-10 00:06:19 +000074Sema::InstantiatingTemplate::
75InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000076 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000077 SourceRange InstantiationRange)
78 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000079
80 Invalid = CheckInstantiationDepth(PointOfInstantiation,
81 InstantiationRange);
82 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000083 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000084 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000085 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000086 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000087 Inst.TemplateArgs = 0;
88 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +000089 Inst.InstantiationRange = InstantiationRange;
90 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
91 Invalid = false;
92 }
93}
94
Mike Stump1eb44332009-09-09 15:08:12 +000095Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-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 Stump1eb44332009-09-09 15:08:12 +0000107 Inst.Kind
Douglas Gregordf667e72009-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 Gregor26dce442009-03-10 00:06:19 +0000113 Inst.InstantiationRange = InstantiationRange;
114 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
115 Invalid = false;
116 }
117}
118
Mike Stump1eb44332009-09-09 15:08:12 +0000119Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000120 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-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 Stump1eb44332009-09-09 15:08:12 +0000127
Douglas Gregorcca9e962009-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 Stump1eb44332009-09-09 15:08:12 +0000143Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000144 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-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 Stump1eb44332009-09-09 15:08:12 +0000155 Inst.Kind
Douglas Gregorcca9e962009-07-01 22:01:06 +0000156 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637a4092009-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 Stump1eb44332009-09-09 15:08:12 +0000167Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Anders Carlsson25cae7f2009-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 Stump1eb44332009-09-09 15:08:12 +0000174
Anders Carlsson25cae7f2009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000191void Sema::InstantiatingTemplate::Clear() {
192 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000193 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000194 Invalid = true;
195 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000196}
197
Douglas Gregordf667e72009-03-10 20:44:00 +0000198bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
199 SourceLocation PointOfInstantiation,
200 SourceRange InstantiationRange) {
Mike Stump1eb44332009-09-09 15:08:12 +0000201 if (SemaRef.ActiveTemplateInstantiations.size()
Douglas Gregordf667e72009-03-10 20:44:00 +0000202 <= SemaRef.getLangOptions().InstantiationDepth)
203 return false;
204
Mike Stump1eb44332009-09-09 15:08:12 +0000205 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-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 Gregoree1828a2009-03-10 18:03:33 +0000214/// \brief Prints the current instantiation stack through a series of
215/// notes.
216void Sema::PrintInstantiationStack() {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000217 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregoree1828a2009-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 Gregordf667e72009-03-10 20:44:00 +0000223 switch (Active->Kind) {
224 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-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 Stump1eb44332009-09-09 15:08:12 +0000230 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000231 DiagID)
232 << Context.getTypeDeclType(Record)
233 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000234 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-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 Stump1eb44332009-09-09 15:08:12 +0000240 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000241 DiagID)
242 << Function
243 << Active->InstantiationRange;
Douglas Gregor7caa6822009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000249 }
Douglas Gregordf667e72009-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 Gregor7532dc62009-03-30 22:58:21 +0000256 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000257 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000258 Active->NumTemplateArgs,
259 Context.PrintingPolicy);
Douglas Gregordf667e72009-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 Gregor637a4092009-06-10 23:47:09 +0000266
Douglas Gregorcca9e962009-07-01 22:01:06 +0000267 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000268 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000269 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000270 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000271 diag::note_explicit_template_arg_substitution_here)
272 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000273 break;
274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Douglas Gregorcca9e962009-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 Gregor637a4092009-06-10 23:47:09 +0000292
Anders Carlsson25cae7f2009-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 Stump1eb44332009-09-09 15:08:12 +0000296
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000297 std::string TemplateArgsStr
298 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000299 Active->TemplateArgs,
Anders Carlsson25cae7f2009-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 Carlsson6bc107b2009-09-05 05:38:54 +0000304 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000305 << Active->InstantiationRange;
306 break;
307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Douglas Gregordf667e72009-03-10 20:44:00 +0000309 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000310 }
311}
312
Douglas Gregor5e9f35c2009-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 Gregorcca9e962009-07-01 22:01:06 +0000322 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000323 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
324
Douglas Gregorcca9e962009-07-01 22:01:06 +0000325 // This is a template instantiation, so there is no SFINAE.
326 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Douglas Gregor5e9f35c2009-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 Stump1eb44332009-09-09 15:08:12 +0000332
Douglas Gregorcca9e962009-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 Gregor5e9f35c2009-06-14 07:33:30 +0000338 }
339 }
340
341 return false;
342}
343
Douglas Gregor99ebf652009-02-27 19:31:52 +0000344//===----------------------------------------------------------------------===/
345// Template Instantiation for Types
346//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000347namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000348 class VISIBILITY_HIDDEN TemplateInstantiator
349 : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000350 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000351 SourceLocation Loc;
352 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000353
Douglas Gregorcd281c32009-02-28 00:25:32 +0000354 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000355 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
357 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000358 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000359 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000360 DeclarationName Entity)
361 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000362 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000363
Mike Stump1eb44332009-09-09 15:08:12 +0000364 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-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 Gregorff668032009-05-13 18:28:20 +0000371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Douglas Gregor577f75a2009-08-04 16:50:30 +0000373 /// \brief Returns the location of the entity being instantiated, if known.
374 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor577f75a2009-08-04 16:50:30 +0000376 /// \brief Returns the name of the entity being instantiated, if any.
377 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregor577f75a2009-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 Gregorb98b1992009-08-11 05:31:07 +0000382
Mike Stump1eb44332009-09-09 15:08:12 +0000383 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000384 /// instantiating it.
385 Decl *TransformDefinition(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregor43959a92009-08-20 07:17:43 +0000387 /// \brief Rebuild the exception declaration and register the declaration
388 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000389 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000390 DeclaratorInfo *Declarator,
391 IdentifierInfo *Name,
392 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000393
John McCallc4e70192009-09-11 04:59:25 +0000394 /// \brief Check for tag mismatches when instantiating an
395 /// elaborated type.
396 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
397
Anders Carlsson773f3972009-09-11 01:22:35 +0000398 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000399 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
401 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000402 /// substitution of the corresponding template type argument.
403 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
404 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000405}
406
Douglas Gregor577f75a2009-08-04 16:50:30 +0000407Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000408 if (!D)
409 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Douglas Gregorc68afe22009-09-03 21:38:09 +0000411 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000412 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
413 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
414 "Wrong kind of template template argument");
Mike Stump1eb44332009-09-09 15:08:12 +0000415 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000416 TTP->getPosition()).getAsDecl());
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
419 // If the corresponding template argument is NULL or non-existent, it's
420 // because we are performing instantiation from explicitly-specified
Douglas Gregord6350ae2009-08-28 20:31:08 +0000421 // template arguments in a function template, but there were some
422 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000423 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000424 TTP->getPosition()))
425 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregord6350ae2009-08-28 20:31:08 +0000427 // FIXME: Implement depth reduction of template template parameters
Mike Stump1eb44332009-09-09 15:08:12 +0000428 assert(false &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000429 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregord1067e52009-08-06 06:41:21 +0000430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregore95b4092009-09-16 18:34:49 +0000432 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000433}
434
Douglas Gregor43959a92009-08-20 07:17:43 +0000435Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000436 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000437 if (!Inst)
438 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Douglas Gregor43959a92009-08-20 07:17:43 +0000440 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
441 return Inst;
442}
443
444VarDecl *
445TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000446 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000447 DeclaratorInfo *Declarator,
448 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000449 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000450 SourceRange TypeRange) {
451 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
452 Name, Loc, TypeRange);
453 if (Var && !Var->isInvalidDecl())
454 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
455 return Var;
456}
457
John McCallc4e70192009-09-11 04:59:25 +0000458QualType
459TemplateInstantiator::RebuildElaboratedType(QualType T,
460 ElaboratedType::TagKind Tag) {
461 if (const TagType *TT = T->getAs<TagType>()) {
462 TagDecl* TD = TT->getDecl();
463
464 // FIXME: this location is very wrong; we really need typelocs.
465 SourceLocation TagLocation = TD->getTagKeywordLoc();
466
467 // FIXME: type might be anonymous.
468 IdentifierInfo *Id = TD->getIdentifier();
469
470 // TODO: should we even warn on struct/class mismatches for this? Seems
471 // like it's likely to produce a lot of spurious errors.
472 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
473 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
474 << Id
475 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
476 TD->getKindName());
477 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
478 }
479 }
480
481 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
482}
483
484Sema::OwningExprResult
Anders Carlsson773f3972009-09-11 01:22:35 +0000485TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
486 if (!E->isTypeDependent())
487 return SemaRef.Owned(E->Retain());
488
489 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
490 assert(currentDecl && "Must have current function declaration when "
491 "instantiating.");
492
493 PredefinedExpr::IdentType IT = E->getIdentType();
494
495 unsigned Length =
496 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
497
498 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000499 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000500 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
501 ArrayType::Normal, 0);
502 PredefinedExpr *PE =
503 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
504 return getSema().Owned(PE);
505}
506
507Sema::OwningExprResult
Douglas Gregorb98b1992009-08-11 05:31:07 +0000508TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
509 // FIXME: Clean this up a bit
510 NamedDecl *D = E->getDecl();
511 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000512 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
513 assert(false && "Cannot reduce non-type template parameter depth yet");
514 return getSema().ExprError();
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
517 // If the corresponding template argument is NULL or non-existent, it's
518 // because we are performing instantiation from explicitly-specified
Douglas Gregorb98b1992009-08-11 05:31:07 +0000519 // template arguments in a function template, but there were some
520 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000521 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000522 NTTP->getPosition()))
523 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000524
525 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000526 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Douglas Gregorb98b1992009-08-11 05:31:07 +0000528 // The template argument itself might be an expression, in which
529 // case we just return that expression.
530 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregord6350ae2009-08-28 20:31:08 +0000531 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Douglas Gregorb98b1992009-08-11 05:31:07 +0000533 if (Arg.getKind() == TemplateArgument::Declaration) {
534 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Douglas Gregore95b4092009-09-16 18:34:49 +0000536 VD = cast_or_null<ValueDecl>(
537 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregord6350ae2009-08-28 20:31:08 +0000538 if (!VD)
539 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
541 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000542 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Douglas Gregorb98b1992009-08-11 05:31:07 +0000545 assert(Arg.getKind() == TemplateArgument::Integral);
546 QualType T = Arg.getIntegralType();
547 if (T->isCharType() || T->isWideCharType())
548 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000549 Arg.getAsIntegral()->getZExtValue(),
550 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000551 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000552 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000553 if (T->isBooleanType())
554 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000555 Arg.getAsIntegral()->getBoolValue(),
Mike Stump1eb44332009-09-09 15:08:12 +0000556 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000557 E->getSourceRange().getBegin()));
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Douglas Gregorb98b1992009-08-11 05:31:07 +0000559 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
560 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000561 *Arg.getAsIntegral(),
Mike Stump1eb44332009-09-09 15:08:12 +0000562 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000563 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Douglas Gregore95b4092009-09-16 18:34:49 +0000566 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000567 if (!InstD)
568 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Anders Carlsson0d8df782009-08-29 19:37:28 +0000570 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000571 // we need to get the underlying decl.
Anders Carlsson0d8df782009-08-29 19:37:28 +0000572 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
573 InstD = InstD->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Douglas Gregorb98b1992009-08-11 05:31:07 +0000575 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +0000576 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregorb98b1992009-08-11 05:31:07 +0000577 /*FIXME:*/false,
Mike Stump1eb44332009-09-09 15:08:12 +0000578 /*FIXME:*/0,
579 /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000580}
581
Mike Stump1eb44332009-09-09 15:08:12 +0000582QualType
Douglas Gregor577f75a2009-08-04 16:50:30 +0000583TemplateInstantiator::TransformTemplateTypeParmType(
584 const TemplateTypeParmType *T) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000585 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000586 // Replace the template type parameter with its corresponding
587 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000588
589 // If the corresponding template argument is NULL or doesn't exist, it's
590 // because we are performing instantiation from explicitly-specified
591 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000592 // arguments left unspecified.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000593 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
594 return QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
596 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000597 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000598 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000599
600 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Mike Stump1eb44332009-09-09 15:08:12 +0000601 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000602
603 // The template type parameter comes from an inner template (e.g.,
604 // the template parameter list of a member template inside the
605 // template we are instantiating). Create a new template type
606 // parameter with the template "level" reduced by one.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000607 return getSema().Context.getTemplateTypeParmType(
608 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor577f75a2009-08-04 16:50:30 +0000609 T->getIndex(),
610 T->isParameterPack(),
611 T->getName());
Douglas Gregorcd281c32009-02-28 00:25:32 +0000612}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000613
John McCallce3ff2b2009-08-25 22:02:44 +0000614/// \brief Perform substitution on the type T with a given set of template
615/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000616///
617/// This routine substitutes the given template arguments into the
618/// type T and produces the instantiated type.
619///
620/// \param T the type into which the template arguments will be
621/// substituted. If this type is not dependent, it will be returned
622/// immediately.
623///
624/// \param TemplateArgs the template arguments that will be
625/// substituted for the top-level template parameters within T.
626///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000627/// \param Loc the location in the source code where this substitution
628/// is being performed. It will typically be the location of the
629/// declarator (if we're instantiating the type of some declaration)
630/// or the location of the type in the source code (if, e.g., we're
631/// instantiating the type of a cast expression).
632///
633/// \param Entity the name of the entity associated with a declaration
634/// being instantiated (if any). May be empty to indicate that there
635/// is no such entity (if, e.g., this is a type that occurs as part of
636/// a cast expression) or that the entity has no name (e.g., an
637/// unnamed function parameter).
638///
639/// \returns If the instantiation succeeds, the instantiated
640/// type. Otherwise, produces diagnostics and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000641QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000642 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000643 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000644 assert(!ActiveTemplateInstantiations.empty() &&
645 "Cannot perform an instantiation without some context on the "
646 "instantiation stack");
647
Douglas Gregor99ebf652009-02-27 19:31:52 +0000648 // If T is not a dependent type, there is nothing to do.
649 if (!T->isDependentType())
650 return T;
651
Douglas Gregor577f75a2009-08-04 16:50:30 +0000652 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
653 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000654}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000655
John McCallce3ff2b2009-08-25 22:02:44 +0000656/// \brief Perform substitution on the base class specifiers of the
657/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000658///
659/// Produces a diagnostic and returns true on error, returns false and
660/// attaches the instantiated base classes to the class template
661/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000662bool
John McCallce3ff2b2009-08-25 22:02:44 +0000663Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
664 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000665 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000666 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000667 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000668 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000669 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000670 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000671 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000672 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000673 continue;
674 }
675
Mike Stump1eb44332009-09-09 15:08:12 +0000676 QualType BaseType = SubstType(Base->getType(),
677 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000678 Base->getSourceRange().getBegin(),
679 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000680 if (BaseType.isNull()) {
681 Invalid = true;
682 continue;
683 }
684
685 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000686 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000687 Base->getSourceRange(),
688 Base->isVirtual(),
689 Base->getAccessSpecifierAsWritten(),
690 BaseType,
691 /*FIXME: Not totally accurate */
692 Base->getSourceRange().getBegin()))
693 InstantiatedBases.push_back(InstantiatedBase);
694 else
695 Invalid = true;
696 }
697
Douglas Gregor27b152f2009-03-10 18:52:44 +0000698 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000699 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000700 InstantiatedBases.size()))
701 Invalid = true;
702
703 return Invalid;
704}
705
Douglas Gregord475b8d2009-03-25 21:17:03 +0000706/// \brief Instantiate the definition of a class from a given pattern.
707///
708/// \param PointOfInstantiation The point of instantiation within the
709/// source code.
710///
711/// \param Instantiation is the declaration whose definition is being
712/// instantiated. This will be either a class template specialization
713/// or a member class of a class template specialization.
714///
715/// \param Pattern is the pattern from which the instantiation
716/// occurs. This will be either the declaration of a class template or
717/// the declaration of a member class of a class template.
718///
719/// \param TemplateArgs The template arguments to be substituted into
720/// the pattern.
721///
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000722/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +0000723///
724/// \param Complain whether to complain if the class cannot be instantiated due
725/// to the lack of a definition.
726///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000727/// \returns true if an error occurred, false otherwise.
728bool
729Sema::InstantiateClass(SourceLocation PointOfInstantiation,
730 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000731 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000732 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000733 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000734 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +0000735
Mike Stump1eb44332009-09-09 15:08:12 +0000736 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +0000737 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
738 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +0000739 if (!Complain) {
740 // Say nothing
741 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000742 Diag(PointOfInstantiation,
743 diag::err_implicit_instantiate_member_undefined)
744 << Context.getTypeDeclType(Instantiation);
745 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
746 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000747 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000748 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +0000749 << Context.getTypeDeclType(Instantiation);
750 Diag(Pattern->getLocation(), diag::note_template_decl_here);
751 }
752 return true;
753 }
754 Pattern = PatternDef;
755
Douglas Gregord048bb72009-03-25 21:23:52 +0000756 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000757 if (Inst)
758 return true;
759
760 // Enter the scope of this instantiation. We don't use
761 // PushDeclContext because we don't have a scope.
762 DeclContext *PreviousContext = CurContext;
763 CurContext = Instantiation;
764
765 // Start the definition of this instantiation.
766 Instantiation->startDefinition();
767
John McCallce3ff2b2009-08-25 22:02:44 +0000768 // Do substitution on the base class specifiers.
769 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000770 Invalid = true;
771
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000772 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000773 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000774 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000775 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +0000776 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000777 if (NewMember) {
778 if (NewMember->isInvalidDecl())
779 Invalid = true;
780 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000781 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +0000782 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
783 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000784 } else {
785 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000786 // instantiations was a semantic disaster, and we'll want to set Invalid =
787 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000788 }
789 }
790
791 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000792 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000793 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000794 0);
795
796 // Add any implicitly-declared members that we might need.
797 AddImplicitlyDeclaredMembersToClass(Instantiation);
798
799 // Exit the scope of this instantiation.
800 CurContext = PreviousContext;
801
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000802 if (!Invalid)
803 Consumer.HandleTagDeclDefinition(Instantiation);
804
Douglas Gregora58861f2009-05-13 20:28:22 +0000805 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000806 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000807 Inst.Clear();
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000808 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
809 TSK);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000810 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000811
Douglas Gregord475b8d2009-03-25 21:17:03 +0000812 return Invalid;
813}
814
Mike Stump1eb44332009-09-09 15:08:12 +0000815bool
Douglas Gregor2943aed2009-03-03 04:44:36 +0000816Sema::InstantiateClassTemplateSpecialization(
817 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000818 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000819 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000820 // Perform the actual instantiation on the canonical declaration.
821 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000822 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000823
Douglas Gregor52604ab2009-09-11 21:19:12 +0000824 // Check whether we have already instantiated or specialized this class
825 // template specialization.
826 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
827 if (ClassTemplateSpec->getSpecializationKind() ==
828 TSK_ExplicitInstantiationDeclaration &&
829 TSK == TSK_ExplicitInstantiationDefinition) {
830 // An explicit instantiation definition follows an explicit instantiation
831 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
832 // explicit instantiation.
833 ClassTemplateSpec->setSpecializationKind(TSK);
834 InstantiateClassTemplateSpecializationMembers(
835 /*FIXME?*/ClassTemplateSpec->getPointOfInstantiation(),
836 ClassTemplateSpec,
837 TSK);
838 return false;
839 }
840
841 // We can only instantiate something that hasn't already been
842 // instantiated or specialized. Fail without any diagnostics: our
843 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000844 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +0000845 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000846
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000847 if (ClassTemplateSpec->isInvalidDecl())
848 return true;
849
Douglas Gregor2943aed2009-03-03 04:44:36 +0000850 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000851 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000852
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000853 // C++ [temp.class.spec.match]p1:
854 // When a class template is used in a context that requires an
855 // instantiation of the class, it is necessary to determine
856 // whether the instantiation is to be generated using the primary
857 // template or one of the partial specializations. This is done by
858 // matching the template arguments of the class template
859 // specialization with the template argument lists of the partial
860 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000861 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
862 TemplateArgumentList *> MatchResult;
863 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +0000864 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000865 Partial = Template->getPartialSpecializations().begin(),
866 PartialEnd = Template->getPartialSpecializations().end();
867 Partial != PartialEnd;
868 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000869 TemplateDeductionInfo Info(Context);
870 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000871 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000872 ClassTemplateSpec->getTemplateArgs(),
873 Info)) {
874 // FIXME: Store the failed-deduction information for use in
875 // diagnostics, later.
876 (void)Result;
877 } else {
878 Matched.push_back(std::make_pair(&*Partial, Info.take()));
879 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000880 }
881
882 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000883 // -- If exactly one matching specialization is found, the
884 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000885 Pattern = Matched[0].first;
Douglas Gregor37d93e92009-08-02 23:24:31 +0000886 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000887 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000888 // -- If more than one matching specialization is found, the
889 // partial order rules (14.5.4.2) are used to determine
890 // whether one of the specializations is more specialized
891 // than the others. If none of the specializations is more
892 // specialized than all of the other matching
893 // specializations, then the use of the class template is
894 // ambiguous and the program is ill-formed.
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000895 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
896 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
897 PEnd = Matched.end();
898 P != PEnd; ++P) {
899 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
900 == P->first)
901 Best = P;
902 }
903
904 // Determine if the best partial specialization is more specialized than
905 // the others.
906 bool Ambiguous = false;
907 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
908 PEnd = Matched.end();
909 P != PEnd; ++P) {
910 if (P != Best &&
911 getMoreSpecializedPartialSpecialization(P->first, Best->first)
912 != Best->first) {
913 Ambiguous = true;
914 break;
915 }
916 }
917
918 if (Ambiguous) {
919 // Partial ordering did not produce a clear winner. Complain.
920 ClassTemplateSpec->setInvalidDecl();
921 Diag(ClassTemplateSpec->getPointOfInstantiation(),
922 diag::err_partial_spec_ordering_ambiguous)
923 << ClassTemplateSpec;
924
925 // Print the matching partial specializations.
926 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
927 PEnd = Matched.end();
928 P != PEnd; ++P)
929 Diag(P->first->getLocation(), diag::note_partial_spec_match)
930 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
931 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +0000932
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000933 return true;
934 }
935
936 // Instantiate using the best class template partial specialization.
937 Pattern = Best->first;
938 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000939 } else {
940 // -- If no matches are found, the instantiation is generated
941 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000942 ClassTemplateDecl *OrigTemplate = Template;
943 while (OrigTemplate->getInstantiatedFromMemberTemplate())
944 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Douglas Gregord6350ae2009-08-28 20:31:08 +0000946 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000947 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000948
Douglas Gregord6350ae2009-08-28 20:31:08 +0000949 // Note that this is an instantiation.
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000950 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor2943aed2009-03-03 04:44:36 +0000951
John McCall9cc78072009-09-11 07:25:08 +0000952 bool Result = InstantiateClass(ClassTemplateSpec->getPointOfInstantiation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000953 ClassTemplateSpec, Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000954 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000955 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000956 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor199d9912009-06-05 00:53:49 +0000958 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
959 // FIXME: Implement TemplateArgumentList::Destroy!
960 // if (Matched[I].first != Pattern)
961 // Matched[I].second->Destroy(Context);
962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregor199d9912009-06-05 00:53:49 +0000964 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +0000965}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000966
John McCallce3ff2b2009-08-25 22:02:44 +0000967/// \brief Instantiates the definitions of all of the member
968/// of the given class, which is an instantiation of a class template
969/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +0000970void
971Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000972 CXXRecordDecl *Instantiation,
973 const MultiLevelTemplateArgumentList &TemplateArgs,
974 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000975 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
976 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +0000977 D != DEnd; ++D) {
978 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000979 if (Function->getInstantiatedFromMemberFunction()) {
980 // If this member was explicitly specialized, do nothing.
981 if (Function->getTemplateSpecializationKind() ==
982 TSK_ExplicitSpecialization)
983 continue;
984
Douglas Gregor2db32322009-10-07 23:56:10 +0000985 Function->setTemplateSpecializationKind(TSK);
Douglas Gregorf6b11852009-10-08 15:14:33 +0000986 }
987
988 if (!Function->getBody() && TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000989 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +0000990 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000991 if (Var->isStaticDataMember()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000992 // If this member was explicitly specialized, do nothing.
993 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
994 continue;
995
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000996 Var->setTemplateSpecializationKind(TSK);
997
Douglas Gregorf6b11852009-10-08 15:14:33 +0000998 if (TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000999 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
1000 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001001 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001002 if (Record->isInjectedClassName())
1003 continue;
1004
1005 assert(Record->getInstantiatedFromMemberClass() &&
1006 "Missing instantiated-from-template information");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001007
1008 // If this member was explicitly specialized, do nothing.
1009 if (Record->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1010 continue;
1011
Douglas Gregor2db32322009-10-07 23:56:10 +00001012 if (!Record->getDefinition(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001013 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +00001014 Record->getInstantiatedFromMemberClass(),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001015 TemplateArgs,
1016 TSK);
Douglas Gregore9374d52009-10-08 01:19:17 +00001017
1018 InstantiateClassMembers(PointOfInstantiation, Record, TemplateArgs,
1019 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001020 }
1021 }
1022}
1023
1024/// \brief Instantiate the definitions of all of the members of the
1025/// given class template specialization, which was named as part of an
1026/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001027void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001028Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001029 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001030 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1031 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001032 // C++0x [temp.explicit]p7:
1033 // An explicit instantiation that names a class template
1034 // specialization is an explicit instantion of the same kind
1035 // (declaration or definition) of each of its members (not
1036 // including members inherited from base classes) that has not
1037 // been previously explicitly specialized in the translation unit
1038 // containing the explicit instantiation, except as described
1039 // below.
1040 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001041 getTemplateInstantiationArgs(ClassTemplateSpec),
1042 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001043}
1044
Mike Stump1eb44332009-09-09 15:08:12 +00001045Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001046Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001047 if (!S)
1048 return Owned(S);
1049
1050 TemplateInstantiator Instantiator(*this, TemplateArgs,
1051 SourceLocation(),
1052 DeclarationName());
1053 return Instantiator.TransformStmt(S);
1054}
1055
Mike Stump1eb44332009-09-09 15:08:12 +00001056Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001057Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001058 if (!E)
1059 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Douglas Gregorb98b1992009-08-11 05:31:07 +00001061 TemplateInstantiator Instantiator(*this, TemplateArgs,
1062 SourceLocation(),
1063 DeclarationName());
1064 return Instantiator.TransformExpr(E);
1065}
1066
John McCallce3ff2b2009-08-25 22:02:44 +00001067/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001068NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001069Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001070 SourceRange Range,
1071 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001072 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1073 DeclarationName());
1074 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001075}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001076
1077TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001078Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001079 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001080 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1081 DeclarationName());
1082 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001083}
Douglas Gregor91333002009-06-11 00:06:24 +00001084
Mike Stump1eb44332009-09-09 15:08:12 +00001085TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001086 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001087 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1088 DeclarationName());
1089 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregor91333002009-06-11 00:06:24 +00001090}