blob: 6a5235229a841adb9e364315aaf9f234a471f5f6 [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 Gregorc68afe22009-09-03 21:38:09 +0000432 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D));
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);
499 QualType ResTy = getSema().Context.CharTy.getQualifiedType(QualType::Const);
500 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 Gregord6350ae2009-08-28 20:31:08 +0000536 VD = cast_or_null<ValueDecl>(getSema().FindInstantiatedDecl(VD));
537 if (!VD)
538 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000539
540 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000541 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Douglas Gregorb98b1992009-08-11 05:31:07 +0000544 assert(Arg.getKind() == TemplateArgument::Integral);
545 QualType T = Arg.getIntegralType();
546 if (T->isCharType() || T->isWideCharType())
547 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000548 Arg.getAsIntegral()->getZExtValue(),
549 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000550 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000551 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000552 if (T->isBooleanType())
553 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000554 Arg.getAsIntegral()->getBoolValue(),
Mike Stump1eb44332009-09-09 15:08:12 +0000555 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000556 E->getSourceRange().getBegin()));
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Douglas Gregorb98b1992009-08-11 05:31:07 +0000558 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
559 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregord6350ae2009-08-28 20:31:08 +0000560 *Arg.getAsIntegral(),
Mike Stump1eb44332009-09-09 15:08:12 +0000561 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000562 E->getSourceRange().getBegin()));
Douglas Gregorb98b1992009-08-11 05:31:07 +0000563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
John McCallce3ff2b2009-08-25 22:02:44 +0000565 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000566 if (!InstD)
567 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Anders Carlsson0d8df782009-08-29 19:37:28 +0000569 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000570 // we need to get the underlying decl.
Anders Carlsson0d8df782009-08-29 19:37:28 +0000571 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
572 InstD = InstD->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Douglas Gregorb98b1992009-08-11 05:31:07 +0000574 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +0000575 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregorb98b1992009-08-11 05:31:07 +0000576 /*FIXME:*/false,
Mike Stump1eb44332009-09-09 15:08:12 +0000577 /*FIXME:*/0,
578 /*FIXME:*/false);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000579}
580
Mike Stump1eb44332009-09-09 15:08:12 +0000581QualType
Douglas Gregor577f75a2009-08-04 16:50:30 +0000582TemplateInstantiator::TransformTemplateTypeParmType(
583 const TemplateTypeParmType *T) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000584 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000585 // Replace the template type parameter with its corresponding
586 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000587
588 // If the corresponding template argument is NULL or doesn't exist, it's
589 // because we are performing instantiation from explicitly-specified
590 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000591 // arguments left unspecified.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000592 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
593 return QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
595 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000596 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000597 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000598
599 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Mike Stump1eb44332009-09-09 15:08:12 +0000600 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000601
602 // The template type parameter comes from an inner template (e.g.,
603 // the template parameter list of a member template inside the
604 // template we are instantiating). Create a new template type
605 // parameter with the template "level" reduced by one.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000606 return getSema().Context.getTemplateTypeParmType(
607 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor577f75a2009-08-04 16:50:30 +0000608 T->getIndex(),
609 T->isParameterPack(),
610 T->getName());
Douglas Gregorcd281c32009-02-28 00:25:32 +0000611}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000612
John McCallce3ff2b2009-08-25 22:02:44 +0000613/// \brief Perform substitution on the type T with a given set of template
614/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000615///
616/// This routine substitutes the given template arguments into the
617/// type T and produces the instantiated type.
618///
619/// \param T the type into which the template arguments will be
620/// substituted. If this type is not dependent, it will be returned
621/// immediately.
622///
623/// \param TemplateArgs the template arguments that will be
624/// substituted for the top-level template parameters within T.
625///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000626/// \param Loc the location in the source code where this substitution
627/// is being performed. It will typically be the location of the
628/// declarator (if we're instantiating the type of some declaration)
629/// or the location of the type in the source code (if, e.g., we're
630/// instantiating the type of a cast expression).
631///
632/// \param Entity the name of the entity associated with a declaration
633/// being instantiated (if any). May be empty to indicate that there
634/// is no such entity (if, e.g., this is a type that occurs as part of
635/// a cast expression) or that the entity has no name (e.g., an
636/// unnamed function parameter).
637///
638/// \returns If the instantiation succeeds, the instantiated
639/// type. Otherwise, produces diagnostics and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000640QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000641 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000642 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000643 assert(!ActiveTemplateInstantiations.empty() &&
644 "Cannot perform an instantiation without some context on the "
645 "instantiation stack");
646
Douglas Gregor99ebf652009-02-27 19:31:52 +0000647 // If T is not a dependent type, there is nothing to do.
648 if (!T->isDependentType())
649 return T;
650
Douglas Gregor577f75a2009-08-04 16:50:30 +0000651 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
652 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000653}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000654
John McCallce3ff2b2009-08-25 22:02:44 +0000655/// \brief Perform substitution on the base class specifiers of the
656/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000657///
658/// Produces a diagnostic and returns true on error, returns false and
659/// attaches the instantiated base classes to the class template
660/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000661bool
John McCallce3ff2b2009-08-25 22:02:44 +0000662Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
663 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000664 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000665 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000666 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000667 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000668 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000669 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000670 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000671 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000672 continue;
673 }
674
Mike Stump1eb44332009-09-09 15:08:12 +0000675 QualType BaseType = SubstType(Base->getType(),
676 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000677 Base->getSourceRange().getBegin(),
678 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000679 if (BaseType.isNull()) {
680 Invalid = true;
681 continue;
682 }
683
684 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000685 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000686 Base->getSourceRange(),
687 Base->isVirtual(),
688 Base->getAccessSpecifierAsWritten(),
689 BaseType,
690 /*FIXME: Not totally accurate */
691 Base->getSourceRange().getBegin()))
692 InstantiatedBases.push_back(InstantiatedBase);
693 else
694 Invalid = true;
695 }
696
Douglas Gregor27b152f2009-03-10 18:52:44 +0000697 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000698 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000699 InstantiatedBases.size()))
700 Invalid = true;
701
702 return Invalid;
703}
704
Douglas Gregord475b8d2009-03-25 21:17:03 +0000705/// \brief Instantiate the definition of a class from a given pattern.
706///
707/// \param PointOfInstantiation The point of instantiation within the
708/// source code.
709///
710/// \param Instantiation is the declaration whose definition is being
711/// instantiated. This will be either a class template specialization
712/// or a member class of a class template specialization.
713///
714/// \param Pattern is the pattern from which the instantiation
715/// occurs. This will be either the declaration of a class template or
716/// the declaration of a member class of a class template.
717///
718/// \param TemplateArgs The template arguments to be substituted into
719/// the pattern.
720///
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000721/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +0000722///
723/// \param Complain whether to complain if the class cannot be instantiated due
724/// to the lack of a definition.
725///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000726/// \returns true if an error occurred, false otherwise.
727bool
728Sema::InstantiateClass(SourceLocation PointOfInstantiation,
729 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000730 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000731 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000732 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000733 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +0000734
Mike Stump1eb44332009-09-09 15:08:12 +0000735 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +0000736 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
737 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +0000738 if (!Complain) {
739 // Say nothing
740 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000741 Diag(PointOfInstantiation,
742 diag::err_implicit_instantiate_member_undefined)
743 << Context.getTypeDeclType(Instantiation);
744 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
745 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000746 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000747 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +0000748 << Context.getTypeDeclType(Instantiation);
749 Diag(Pattern->getLocation(), diag::note_template_decl_here);
750 }
751 return true;
752 }
753 Pattern = PatternDef;
754
Douglas Gregord048bb72009-03-25 21:23:52 +0000755 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000756 if (Inst)
757 return true;
758
759 // Enter the scope of this instantiation. We don't use
760 // PushDeclContext because we don't have a scope.
761 DeclContext *PreviousContext = CurContext;
762 CurContext = Instantiation;
763
764 // Start the definition of this instantiation.
765 Instantiation->startDefinition();
766
John McCallce3ff2b2009-08-25 22:02:44 +0000767 // Do substitution on the base class specifiers.
768 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000769 Invalid = true;
770
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000771 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000772 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000773 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000774 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +0000775 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000776 if (NewMember) {
777 if (NewMember->isInvalidDecl())
778 Invalid = true;
779 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000780 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +0000781 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
782 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000783 } else {
784 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000785 // instantiations was a semantic disaster, and we'll want to set Invalid =
786 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000787 }
788 }
789
790 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000791 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000792 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000793 0);
794
795 // Add any implicitly-declared members that we might need.
796 AddImplicitlyDeclaredMembersToClass(Instantiation);
797
798 // Exit the scope of this instantiation.
799 CurContext = PreviousContext;
800
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000801 if (!Invalid)
802 Consumer.HandleTagDeclDefinition(Instantiation);
803
Douglas Gregora58861f2009-05-13 20:28:22 +0000804 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000805 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000806 Inst.Clear();
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000807 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
808 TSK);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000809 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000810
Douglas Gregord475b8d2009-03-25 21:17:03 +0000811 return Invalid;
812}
813
Mike Stump1eb44332009-09-09 15:08:12 +0000814bool
Douglas Gregor2943aed2009-03-03 04:44:36 +0000815Sema::InstantiateClassTemplateSpecialization(
816 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000817 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000818 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000819 // Perform the actual instantiation on the canonical declaration.
820 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000821 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000822
823 // We can only instantiate something that hasn't already been
824 // instantiated or specialized. Fail without any diagnostics: our
825 // caller will provide an error message.
826 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
827 return true;
828
Douglas Gregor2943aed2009-03-03 04:44:36 +0000829 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000830 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000831
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000832 // C++ [temp.class.spec.match]p1:
833 // When a class template is used in a context that requires an
834 // instantiation of the class, it is necessary to determine
835 // whether the instantiation is to be generated using the primary
836 // template or one of the partial specializations. This is done by
837 // matching the template arguments of the class template
838 // specialization with the template argument lists of the partial
839 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000840 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
841 TemplateArgumentList *> MatchResult;
842 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +0000843 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000844 Partial = Template->getPartialSpecializations().begin(),
845 PartialEnd = Template->getPartialSpecializations().end();
846 Partial != PartialEnd;
847 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000848 TemplateDeductionInfo Info(Context);
849 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000850 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000851 ClassTemplateSpec->getTemplateArgs(),
852 Info)) {
853 // FIXME: Store the failed-deduction information for use in
854 // diagnostics, later.
855 (void)Result;
856 } else {
857 Matched.push_back(std::make_pair(&*Partial, Info.take()));
858 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000859 }
860
861 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000862 // -- If exactly one matching specialization is found, the
863 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000864 Pattern = Matched[0].first;
Douglas Gregor37d93e92009-08-02 23:24:31 +0000865 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000866 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000867 // -- If more than one matching specialization is found, the
868 // partial order rules (14.5.4.2) are used to determine
869 // whether one of the specializations is more specialized
870 // than the others. If none of the specializations is more
871 // specialized than all of the other matching
872 // specializations, then the use of the class template is
873 // ambiguous and the program is ill-formed.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000874 // FIXME: Implement partial ordering of class template partial
875 // specializations.
Mike Stump1eb44332009-09-09 15:08:12 +0000876 Diag(ClassTemplateSpec->getLocation(),
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000877 diag::unsup_template_partial_spec_ordering);
Douglas Gregord6350ae2009-08-28 20:31:08 +0000878
879 // FIXME: Temporary hack to fall back to the primary template
880 ClassTemplateDecl *OrigTemplate = Template;
881 while (OrigTemplate->getInstantiatedFromMemberTemplate())
882 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregord6350ae2009-08-28 20:31:08 +0000884 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000885 } else {
886 // -- If no matches are found, the instantiation is generated
887 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +0000888 ClassTemplateDecl *OrigTemplate = Template;
889 while (OrigTemplate->getInstantiatedFromMemberTemplate())
890 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Douglas Gregord6350ae2009-08-28 20:31:08 +0000892 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000893 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000894
Douglas Gregord6350ae2009-08-28 20:31:08 +0000895 // Note that this is an instantiation.
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000896 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor2943aed2009-03-03 04:44:36 +0000897
John McCall9cc78072009-09-11 07:25:08 +0000898 bool Result = InstantiateClass(ClassTemplateSpec->getPointOfInstantiation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000899 ClassTemplateSpec, Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000900 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000901 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +0000902 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregor199d9912009-06-05 00:53:49 +0000904 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
905 // FIXME: Implement TemplateArgumentList::Destroy!
906 // if (Matched[I].first != Pattern)
907 // Matched[I].second->Destroy(Context);
908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Douglas Gregor199d9912009-06-05 00:53:49 +0000910 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +0000911}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000912
John McCallce3ff2b2009-08-25 22:02:44 +0000913/// \brief Instantiates the definitions of all of the member
914/// of the given class, which is an instantiation of a class template
915/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +0000916void
917Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000918 CXXRecordDecl *Instantiation,
919 const MultiLevelTemplateArgumentList &TemplateArgs,
920 TemplateSpecializationKind TSK) {
921 // FIXME: extern templates
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000922 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
923 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +0000924 D != DEnd; ++D) {
925 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000926 if (!Function->getBody())
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000927 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +0000928 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000929 if (Var->isStaticDataMember())
930 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregora58861f2009-05-13 20:28:22 +0000931 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
932 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000933 assert(Record->getInstantiatedFromMemberClass() &&
Douglas Gregora58861f2009-05-13 20:28:22 +0000934 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000935 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +0000936 Record->getInstantiatedFromMemberClass(),
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000937 TemplateArgs,
938 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +0000939 }
940 }
941 }
942}
943
944/// \brief Instantiate the definitions of all of the members of the
945/// given class template specialization, which was named as part of an
946/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000947void
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000948Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +0000949 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000950 ClassTemplateSpecializationDecl *ClassTemplateSpec,
951 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +0000952 // C++0x [temp.explicit]p7:
953 // An explicit instantiation that names a class template
954 // specialization is an explicit instantion of the same kind
955 // (declaration or definition) of each of its members (not
956 // including members inherited from base classes) that has not
957 // been previously explicitly specialized in the translation unit
958 // containing the explicit instantiation, except as described
959 // below.
960 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000961 getTemplateInstantiationArgs(ClassTemplateSpec),
962 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +0000963}
964
Mike Stump1eb44332009-09-09 15:08:12 +0000965Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +0000966Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +0000967 if (!S)
968 return Owned(S);
969
970 TemplateInstantiator Instantiator(*this, TemplateArgs,
971 SourceLocation(),
972 DeclarationName());
973 return Instantiator.TransformStmt(S);
974}
975
Mike Stump1eb44332009-09-09 15:08:12 +0000976Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +0000977Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +0000978 if (!E)
979 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregorb98b1992009-08-11 05:31:07 +0000981 TemplateInstantiator Instantiator(*this, TemplateArgs,
982 SourceLocation(),
983 DeclarationName());
984 return Instantiator.TransformExpr(E);
985}
986
John McCallce3ff2b2009-08-25 22:02:44 +0000987/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +0000988NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +0000989Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000990 SourceRange Range,
991 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +0000992 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
993 DeclarationName());
994 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000995}
Douglas Gregorde650ae2009-03-31 18:38:02 +0000996
997TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +0000998Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000999 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001000 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1001 DeclarationName());
1002 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001003}
Douglas Gregor91333002009-06-11 00:06:24 +00001004
Mike Stump1eb44332009-09-09 15:08:12 +00001005TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001006 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001007 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1008 DeclarationName());
1009 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregor91333002009-06-11 00:06:24 +00001010}