2009-02-27 22:31:52 +03:00
|
|
|
//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
//
|
|
|
|
// This file implements C++ template instantiation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2009-08-04 20:50:30 +04:00
|
|
|
#include "TreeTransform.h"
|
2009-05-27 00:50:29 +04:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-02-27 22:31:52 +03:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
2009-02-28 03:25:32 +03:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-02-27 22:31:52 +03:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-03-10 21:03:33 +03:00
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
// Template Instantiation Support
|
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
|
2009-08-29 00:31:08 +04:00
|
|
|
/// \brief Retrieve the template argument list(s) that should be used to
|
|
|
|
/// instantiate the definition of the given declaration.
|
2009-11-09 22:17:50 +03:00
|
|
|
///
|
|
|
|
/// \param D the declaration for which we are computing template instantiation
|
|
|
|
/// arguments.
|
|
|
|
///
|
|
|
|
/// \param Innermost if non-NULL, the innermost template argument list.
|
2009-08-28 21:37:35 +04:00
|
|
|
MultiLevelTemplateArgumentList
|
2009-11-09 22:17:50 +03:00
|
|
|
Sema::getTemplateInstantiationArgs(NamedDecl *D,
|
|
|
|
const TemplateArgumentList *Innermost) {
|
2009-08-28 21:37:35 +04:00
|
|
|
// Accumulate the set of template argument lists in this structure.
|
|
|
|
MultiLevelTemplateArgumentList Result;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-09 22:17:50 +03:00
|
|
|
if (Innermost)
|
|
|
|
Result.addOuterTemplateArguments(Innermost);
|
|
|
|
|
2009-08-28 21:37:35 +04:00
|
|
|
DeclContext *Ctx = dyn_cast<DeclContext>(D);
|
|
|
|
if (!Ctx)
|
|
|
|
Ctx = D->getDeclContext();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 07:16:09 +04:00
|
|
|
while (!Ctx->isFileContext()) {
|
2009-08-28 21:37:35 +04:00
|
|
|
// Add template arguments from a class template instantiation.
|
2009-09-09 19:08:12 +04:00
|
|
|
if (ClassTemplateSpecializationDecl *Spec
|
2009-08-28 21:37:35 +04:00
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
|
|
|
|
// We're done when we hit an explicit specialization.
|
|
|
|
if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
|
|
|
|
break;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-28 21:37:35 +04:00
|
|
|
Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
|
2009-10-13 20:30:37 +04:00
|
|
|
|
|
|
|
// If this class template specialization was instantiated from a
|
|
|
|
// specialized member that is a class template, we're done.
|
|
|
|
assert(Spec->getSpecializedTemplate() && "No class template?");
|
|
|
|
if (Spec->getSpecializedTemplate()->isMemberSpecialization())
|
|
|
|
break;
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-08-28 21:37:35 +04:00
|
|
|
// Add template arguments from a function template specialization.
|
2009-08-29 07:16:09 +04:00
|
|
|
else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
|
2009-10-13 20:30:37 +04:00
|
|
|
if (Function->getTemplateSpecializationKind()
|
|
|
|
== TSK_ExplicitSpecialization)
|
|
|
|
break;
|
|
|
|
|
2009-08-28 21:37:35 +04:00
|
|
|
if (const TemplateArgumentList *TemplateArgs
|
2009-10-13 20:30:37 +04:00
|
|
|
= Function->getTemplateSpecializationArgs()) {
|
|
|
|
// Add the template arguments for this specialization.
|
2009-08-28 21:37:35 +04:00
|
|
|
Result.addOuterTemplateArguments(TemplateArgs);
|
2009-08-29 07:16:09 +04:00
|
|
|
|
2009-10-13 20:30:37 +04:00
|
|
|
// If this function was instantiated from a specialized member that is
|
|
|
|
// a function template, we're done.
|
|
|
|
assert(Function->getPrimaryTemplate() && "No function template?");
|
|
|
|
if (Function->getPrimaryTemplate()->isMemberSpecialization())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-29 07:16:09 +04:00
|
|
|
// If this is a friend declaration and it declares an entity at
|
|
|
|
// namespace scope, take arguments from its lexical parent
|
|
|
|
// instead of its semantic parent.
|
|
|
|
if (Function->getFriendObjectKind() &&
|
|
|
|
Function->getDeclContext()->isFileContext()) {
|
|
|
|
Ctx = Function->getLexicalDeclContext();
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-28 21:37:35 +04:00
|
|
|
}
|
2009-08-29 07:16:09 +04:00
|
|
|
|
|
|
|
Ctx = Ctx->getParent();
|
2009-05-15 03:26:13 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-28 21:37:35 +04:00
|
|
|
return Result;
|
2009-05-15 03:26:13 +04:00
|
|
|
}
|
|
|
|
|
2009-03-10 03:06:19 +03:00
|
|
|
Sema::InstantiatingTemplate::
|
|
|
|
InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
|
2009-05-18 21:01:57 +04:00
|
|
|
Decl *Entity,
|
2009-03-10 03:06:19 +03:00
|
|
|
SourceRange InstantiationRange)
|
|
|
|
: SemaRef(SemaRef) {
|
2009-03-10 23:44:00 +03:00
|
|
|
|
|
|
|
Invalid = CheckInstantiationDepth(PointOfInstantiation,
|
|
|
|
InstantiationRange);
|
|
|
|
if (!Invalid) {
|
|
|
|
ActiveTemplateInstantiation Inst;
|
|
|
|
Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
|
|
|
|
Inst.PointOfInstantiation = PointOfInstantiation;
|
|
|
|
Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
|
2009-03-12 21:36:18 +03:00
|
|
|
Inst.TemplateArgs = 0;
|
|
|
|
Inst.NumTemplateArgs = 0;
|
2009-03-10 23:44:00 +03:00
|
|
|
Inst.InstantiationRange = InstantiationRange;
|
|
|
|
SemaRef.ActiveTemplateInstantiations.push_back(Inst);
|
|
|
|
Invalid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
|
2009-03-10 23:44:00 +03:00
|
|
|
SourceLocation PointOfInstantiation,
|
|
|
|
TemplateDecl *Template,
|
|
|
|
const TemplateArgument *TemplateArgs,
|
|
|
|
unsigned NumTemplateArgs,
|
|
|
|
SourceRange InstantiationRange)
|
|
|
|
: SemaRef(SemaRef) {
|
|
|
|
|
|
|
|
Invalid = CheckInstantiationDepth(PointOfInstantiation,
|
|
|
|
InstantiationRange);
|
|
|
|
if (!Invalid) {
|
2009-03-10 03:06:19 +03:00
|
|
|
ActiveTemplateInstantiation Inst;
|
2009-09-09 19:08:12 +04:00
|
|
|
Inst.Kind
|
2009-03-10 23:44:00 +03:00
|
|
|
= ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
|
2009-03-10 03:06:19 +03:00
|
|
|
Inst.PointOfInstantiation = PointOfInstantiation;
|
2009-03-10 23:44:00 +03:00
|
|
|
Inst.Entity = reinterpret_cast<uintptr_t>(Template);
|
|
|
|
Inst.TemplateArgs = TemplateArgs;
|
|
|
|
Inst.NumTemplateArgs = NumTemplateArgs;
|
2009-03-10 03:06:19 +03:00
|
|
|
Inst.InstantiationRange = InstantiationRange;
|
|
|
|
SemaRef.ActiveTemplateInstantiations.push_back(Inst);
|
|
|
|
Invalid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
|
2009-07-02 02:01:06 +04:00
|
|
|
SourceLocation PointOfInstantiation,
|
|
|
|
FunctionTemplateDecl *FunctionTemplate,
|
|
|
|
const TemplateArgument *TemplateArgs,
|
|
|
|
unsigned NumTemplateArgs,
|
|
|
|
ActiveTemplateInstantiation::InstantiationKind Kind,
|
|
|
|
SourceRange InstantiationRange)
|
|
|
|
: SemaRef(SemaRef) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-02 02:01:06 +04:00
|
|
|
Invalid = CheckInstantiationDepth(PointOfInstantiation,
|
|
|
|
InstantiationRange);
|
|
|
|
if (!Invalid) {
|
|
|
|
ActiveTemplateInstantiation Inst;
|
|
|
|
Inst.Kind = Kind;
|
|
|
|
Inst.PointOfInstantiation = PointOfInstantiation;
|
|
|
|
Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
|
|
|
|
Inst.TemplateArgs = TemplateArgs;
|
|
|
|
Inst.NumTemplateArgs = NumTemplateArgs;
|
|
|
|
Inst.InstantiationRange = InstantiationRange;
|
|
|
|
SemaRef.ActiveTemplateInstantiations.push_back(Inst);
|
|
|
|
Invalid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
|
2009-06-11 03:47:09 +04:00
|
|
|
SourceLocation PointOfInstantiation,
|
|
|
|
ClassTemplatePartialSpecializationDecl *PartialSpec,
|
|
|
|
const TemplateArgument *TemplateArgs,
|
|
|
|
unsigned NumTemplateArgs,
|
|
|
|
SourceRange InstantiationRange)
|
|
|
|
: SemaRef(SemaRef) {
|
|
|
|
|
|
|
|
Invalid = CheckInstantiationDepth(PointOfInstantiation,
|
|
|
|
InstantiationRange);
|
|
|
|
if (!Invalid) {
|
|
|
|
ActiveTemplateInstantiation Inst;
|
2009-09-09 19:08:12 +04:00
|
|
|
Inst.Kind
|
2009-07-02 02:01:06 +04:00
|
|
|
= ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
|
2009-06-11 03:47:09 +04:00
|
|
|
Inst.PointOfInstantiation = PointOfInstantiation;
|
|
|
|
Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
|
|
|
|
Inst.TemplateArgs = TemplateArgs;
|
|
|
|
Inst.NumTemplateArgs = NumTemplateArgs;
|
|
|
|
Inst.InstantiationRange = InstantiationRange;
|
|
|
|
SemaRef.ActiveTemplateInstantiations.push_back(Inst);
|
|
|
|
Invalid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
|
2009-09-05 09:14:19 +04:00
|
|
|
SourceLocation PointOfInstantation,
|
|
|
|
ParmVarDecl *Param,
|
|
|
|
const TemplateArgument *TemplateArgs,
|
|
|
|
unsigned NumTemplateArgs,
|
|
|
|
SourceRange InstantiationRange)
|
|
|
|
: SemaRef(SemaRef) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-05 09:14:19 +04:00
|
|
|
Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange);
|
|
|
|
|
|
|
|
if (!Invalid) {
|
|
|
|
ActiveTemplateInstantiation Inst;
|
|
|
|
Inst.Kind
|
|
|
|
= ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
|
|
|
|
Inst.PointOfInstantiation = PointOfInstantation;
|
|
|
|
Inst.Entity = reinterpret_cast<uintptr_t>(Param);
|
|
|
|
Inst.TemplateArgs = TemplateArgs;
|
|
|
|
Inst.NumTemplateArgs = NumTemplateArgs;
|
|
|
|
Inst.InstantiationRange = InstantiationRange;
|
|
|
|
SemaRef.ActiveTemplateInstantiations.push_back(Inst);
|
|
|
|
Invalid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 21:01:57 +04:00
|
|
|
void Sema::InstantiatingTemplate::Clear() {
|
|
|
|
if (!Invalid) {
|
2009-03-10 03:06:19 +03:00
|
|
|
SemaRef.ActiveTemplateInstantiations.pop_back();
|
2009-05-18 21:01:57 +04:00
|
|
|
Invalid = true;
|
|
|
|
}
|
2009-03-10 03:06:19 +03:00
|
|
|
}
|
|
|
|
|
2009-03-10 23:44:00 +03:00
|
|
|
bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
|
|
|
|
SourceLocation PointOfInstantiation,
|
|
|
|
SourceRange InstantiationRange) {
|
2009-09-09 19:08:12 +04:00
|
|
|
if (SemaRef.ActiveTemplateInstantiations.size()
|
2009-03-10 23:44:00 +03:00
|
|
|
<= SemaRef.getLangOptions().InstantiationDepth)
|
|
|
|
return false;
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
SemaRef.Diag(PointOfInstantiation,
|
2009-03-10 23:44:00 +03:00
|
|
|
diag::err_template_recursion_depth_exceeded)
|
|
|
|
<< SemaRef.getLangOptions().InstantiationDepth
|
|
|
|
<< InstantiationRange;
|
|
|
|
SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
|
|
|
|
<< SemaRef.getLangOptions().InstantiationDepth;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-10 21:03:33 +03:00
|
|
|
/// \brief Prints the current instantiation stack through a series of
|
|
|
|
/// notes.
|
|
|
|
void Sema::PrintInstantiationStack() {
|
2009-07-02 02:01:06 +04:00
|
|
|
// FIXME: In all of these cases, we need to show the template arguments
|
2009-03-10 21:03:33 +03:00
|
|
|
for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
|
|
|
|
Active = ActiveTemplateInstantiations.rbegin(),
|
|
|
|
ActiveEnd = ActiveTemplateInstantiations.rend();
|
|
|
|
Active != ActiveEnd;
|
|
|
|
++Active) {
|
2009-03-10 23:44:00 +03:00
|
|
|
switch (Active->Kind) {
|
|
|
|
case ActiveTemplateInstantiation::TemplateInstantiation: {
|
2009-05-18 21:01:57 +04:00
|
|
|
Decl *D = reinterpret_cast<Decl *>(Active->Entity);
|
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
unsigned DiagID = diag::note_template_member_class_here;
|
|
|
|
if (isa<ClassTemplateSpecializationDecl>(Record))
|
|
|
|
DiagID = diag::note_template_class_instantiation_here;
|
2009-09-09 19:08:12 +04:00
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
2009-05-18 21:01:57 +04:00
|
|
|
DiagID)
|
|
|
|
<< Context.getTypeDeclType(Record)
|
|
|
|
<< Active->InstantiationRange;
|
2009-07-25 00:34:43 +04:00
|
|
|
} else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
|
2009-06-26 04:10:03 +04:00
|
|
|
unsigned DiagID;
|
|
|
|
if (Function->getPrimaryTemplate())
|
|
|
|
DiagID = diag::note_function_template_spec_here;
|
|
|
|
else
|
|
|
|
DiagID = diag::note_template_member_function_here;
|
2009-09-09 19:08:12 +04:00
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
2009-05-18 21:01:57 +04:00
|
|
|
DiagID)
|
|
|
|
<< Function
|
|
|
|
<< Active->InstantiationRange;
|
2009-07-25 00:34:43 +04:00
|
|
|
} else {
|
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
|
|
|
diag::note_template_static_data_member_def_here)
|
|
|
|
<< cast<VarDecl>(D)
|
|
|
|
<< Active->InstantiationRange;
|
2009-05-18 21:01:57 +04:00
|
|
|
}
|
2009-03-10 23:44:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
|
|
|
|
TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
|
|
|
|
std::string TemplateArgsStr
|
2009-03-31 02:58:21 +04:00
|
|
|
= TemplateSpecializationType::PrintTemplateArgumentList(
|
2009-09-09 19:08:12 +04:00
|
|
|
Active->TemplateArgs,
|
2009-05-30 00:38:28 +04:00
|
|
|
Active->NumTemplateArgs,
|
|
|
|
Context.PrintingPolicy);
|
2009-03-10 23:44:00 +03:00
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
|
|
|
diag::note_default_arg_instantiation_here)
|
|
|
|
<< (Template->getNameAsString() + TemplateArgsStr)
|
|
|
|
<< Active->InstantiationRange;
|
|
|
|
break;
|
|
|
|
}
|
2009-06-11 03:47:09 +04:00
|
|
|
|
2009-07-02 02:01:06 +04:00
|
|
|
case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
|
2009-09-09 19:08:12 +04:00
|
|
|
FunctionTemplateDecl *FnTmpl
|
2009-07-02 02:01:06 +04:00
|
|
|
= cast<FunctionTemplateDecl>((Decl *)Active->Entity);
|
2009-06-11 03:47:09 +04:00
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
2009-07-02 02:01:06 +04:00
|
|
|
diag::note_explicit_template_arg_substitution_here)
|
|
|
|
<< FnTmpl << Active->InstantiationRange;
|
2009-06-11 03:47:09 +04:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-02 02:01:06 +04:00
|
|
|
case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
|
|
|
|
if (ClassTemplatePartialSpecializationDecl *PartialSpec
|
|
|
|
= dyn_cast<ClassTemplatePartialSpecializationDecl>(
|
|
|
|
(Decl *)Active->Entity)) {
|
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
|
|
|
diag::note_partial_spec_deduct_instantiation_here)
|
|
|
|
<< Context.getTypeDeclType(PartialSpec)
|
|
|
|
<< Active->InstantiationRange;
|
|
|
|
} else {
|
|
|
|
FunctionTemplateDecl *FnTmpl
|
|
|
|
= cast<FunctionTemplateDecl>((Decl *)Active->Entity);
|
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
|
|
|
diag::note_function_template_deduction_instantiation_here)
|
|
|
|
<< FnTmpl << Active->InstantiationRange;
|
|
|
|
}
|
|
|
|
break;
|
2009-06-11 03:47:09 +04:00
|
|
|
|
2009-09-05 09:14:19 +04:00
|
|
|
case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
|
|
|
|
ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
|
|
|
|
FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-05 09:14:19 +04:00
|
|
|
std::string TemplateArgsStr
|
|
|
|
= TemplateSpecializationType::PrintTemplateArgumentList(
|
2009-09-09 19:08:12 +04:00
|
|
|
Active->TemplateArgs,
|
2009-09-05 09:14:19 +04:00
|
|
|
Active->NumTemplateArgs,
|
|
|
|
Context.PrintingPolicy);
|
|
|
|
Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
|
|
|
|
diag::note_default_function_arg_instantiation_here)
|
2009-09-05 09:38:54 +04:00
|
|
|
<< (FD->getNameAsString() + TemplateArgsStr)
|
2009-09-05 09:14:19 +04:00
|
|
|
<< Active->InstantiationRange;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-10 23:44:00 +03:00
|
|
|
}
|
2009-03-10 21:03:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-14 11:33:30 +04:00
|
|
|
bool Sema::isSFINAEContext() const {
|
|
|
|
using llvm::SmallVector;
|
|
|
|
for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
|
|
|
|
Active = ActiveTemplateInstantiations.rbegin(),
|
|
|
|
ActiveEnd = ActiveTemplateInstantiations.rend();
|
|
|
|
Active != ActiveEnd;
|
|
|
|
++Active) {
|
|
|
|
|
|
|
|
switch(Active->Kind) {
|
2009-07-02 02:01:06 +04:00
|
|
|
case ActiveTemplateInstantiation::TemplateInstantiation:
|
2009-09-05 09:14:19 +04:00
|
|
|
case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
|
|
|
|
|
2009-07-02 02:01:06 +04:00
|
|
|
// This is a template instantiation, so there is no SFINAE.
|
|
|
|
return false;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-14 11:33:30 +04:00
|
|
|
case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
|
|
|
|
// A default template argument instantiation may or may not be a
|
|
|
|
// SFINAE context; look further up the stack.
|
|
|
|
break;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-02 02:01:06 +04:00
|
|
|
case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
|
|
|
|
case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
|
|
|
|
// We're either substitution explicitly-specified template arguments
|
|
|
|
// or deduced template arguments, so SFINAE applies.
|
|
|
|
return true;
|
2009-06-14 11:33:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-27 22:31:52 +03:00
|
|
|
//===----------------------------------------------------------------------===/
|
|
|
|
// Template Instantiation for Types
|
|
|
|
//===----------------------------------------------------------------------===/
|
2009-02-28 03:25:32 +03:00
|
|
|
namespace {
|
2009-09-09 19:08:12 +04:00
|
|
|
class VISIBILITY_HIDDEN TemplateInstantiator
|
|
|
|
: public TreeTransform<TemplateInstantiator> {
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs;
|
2009-02-28 03:25:32 +03:00
|
|
|
SourceLocation Loc;
|
|
|
|
DeclarationName Entity;
|
|
|
|
|
|
|
|
public:
|
2009-08-20 11:17:43 +04:00
|
|
|
typedef TreeTransform<TemplateInstantiator> inherited;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
TemplateInstantiator(Sema &SemaRef,
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs,
|
2009-08-04 20:50:30 +04:00
|
|
|
SourceLocation Loc,
|
2009-09-09 19:08:12 +04:00
|
|
|
DeclarationName Entity)
|
|
|
|
: inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
|
2009-08-20 11:17:43 +04:00
|
|
|
Entity(Entity) { }
|
2009-08-04 20:50:30 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// \brief Determine whether the given type \p T has already been
|
2009-08-04 20:50:30 +04:00
|
|
|
/// transformed.
|
|
|
|
///
|
|
|
|
/// For the purposes of template instantiation, a type has already been
|
|
|
|
/// transformed if it is NULL or if it is not dependent.
|
|
|
|
bool AlreadyTransformed(QualType T) {
|
|
|
|
return T.isNull() || !T->isDependentType();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-04 20:50:30 +04:00
|
|
|
/// \brief Returns the location of the entity being instantiated, if known.
|
|
|
|
SourceLocation getBaseLocation() { return Loc; }
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-04 20:50:30 +04:00
|
|
|
/// \brief Returns the name of the entity being instantiated, if any.
|
|
|
|
DeclarationName getBaseEntity() { return Entity; }
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-27 09:26:26 +03:00
|
|
|
/// \brief Sets the "base" location and entity when that
|
|
|
|
/// information is known based on another transformation.
|
|
|
|
void setBase(SourceLocation Loc, DeclarationName Entity) {
|
|
|
|
this->Loc = Loc;
|
|
|
|
this->Entity = Entity;
|
|
|
|
}
|
|
|
|
|
2009-08-04 20:50:30 +04:00
|
|
|
/// \brief Transform the given declaration by instantiating a reference to
|
|
|
|
/// this declaration.
|
|
|
|
Decl *TransformDecl(Decl *D);
|
2009-08-11 09:31:07 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// \brief Transform the definition of the given declaration by
|
2009-08-20 11:17:43 +04:00
|
|
|
/// instantiating it.
|
|
|
|
Decl *TransformDefinition(Decl *D);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-20 09:58:46 +04:00
|
|
|
/// \bried Transform the first qualifier within a scope by instantiating the
|
|
|
|
/// declaration.
|
|
|
|
NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
|
|
|
|
|
2009-08-20 11:17:43 +04:00
|
|
|
/// \brief Rebuild the exception declaration and register the declaration
|
|
|
|
/// as an instantiated local.
|
2009-09-09 19:08:12 +04:00
|
|
|
VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
|
2009-08-20 11:17:43 +04:00
|
|
|
DeclaratorInfo *Declarator,
|
|
|
|
IdentifierInfo *Name,
|
|
|
|
SourceLocation Loc, SourceRange TypeRange);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-11 08:59:25 +04:00
|
|
|
/// \brief Check for tag mismatches when instantiating an
|
|
|
|
/// elaborated type.
|
|
|
|
QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
|
|
|
|
|
2009-11-04 10:01:15 +03:00
|
|
|
Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E,
|
|
|
|
bool isAddressOfOperand);
|
|
|
|
Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E,
|
|
|
|
bool isAddressOfOperand);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-08 16:56:19 +03:00
|
|
|
Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E,
|
|
|
|
bool isAddressOfOperand);
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// \brief Transforms a template type parameter type by performing
|
2009-08-04 20:50:30 +04:00
|
|
|
/// substitution of the corresponding template type argument.
|
2009-10-21 04:40:46 +04:00
|
|
|
QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
|
|
|
|
TemplateTypeParmTypeLoc TL);
|
2009-05-13 22:28:20 +04:00
|
|
|
};
|
2009-02-27 22:31:52 +03:00
|
|
|
}
|
|
|
|
|
2009-08-04 20:50:30 +04:00
|
|
|
Decl *TemplateInstantiator::TransformDecl(Decl *D) {
|
2009-09-04 01:38:09 +04:00
|
|
|
if (!D)
|
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-04 01:38:09 +04:00
|
|
|
if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
|
2009-08-29 00:31:08 +04:00
|
|
|
if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
|
|
|
|
assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
|
|
|
|
"Wrong kind of template template argument");
|
2009-09-09 19:08:12 +04:00
|
|
|
return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
|
2009-08-29 00:31:08 +04:00
|
|
|
TTP->getPosition()).getAsDecl());
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
// If the corresponding template argument is NULL or non-existent, it's
|
|
|
|
// because we are performing instantiation from explicitly-specified
|
2009-08-29 00:31:08 +04:00
|
|
|
// template arguments in a function template, but there were some
|
|
|
|
// arguments left unspecified.
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
|
2009-08-29 00:31:08 +04:00
|
|
|
TTP->getPosition()))
|
|
|
|
return D;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 00:31:08 +04:00
|
|
|
// FIXME: Implement depth reduction of template template parameters
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(false &&
|
2009-08-29 00:31:08 +04:00
|
|
|
"Reducing depth of template template parameters is not yet implemented");
|
2009-08-06 10:41:21 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-16 22:34:49 +04:00
|
|
|
return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
|
2009-02-27 22:31:52 +03:00
|
|
|
}
|
|
|
|
|
2009-08-20 11:17:43 +04:00
|
|
|
Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
|
2009-08-26 02:02:44 +04:00
|
|
|
Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
|
2009-08-20 11:17:43 +04:00
|
|
|
if (!Inst)
|
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-20 11:17:43 +04:00
|
|
|
getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
2009-10-20 09:58:46 +04:00
|
|
|
NamedDecl *
|
|
|
|
TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
// If the first part of the nested-name-specifier was a template type
|
|
|
|
// parameter, instantiate that type parameter down to a tag type.
|
|
|
|
if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
|
|
|
|
const TemplateTypeParmType *TTP
|
|
|
|
= cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
|
|
|
|
if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
|
|
|
|
QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
|
|
|
|
if (T.isNull())
|
|
|
|
return cast_or_null<NamedDecl>(TransformDecl(D));
|
|
|
|
|
|
|
|
if (const TagType *Tag = T->getAs<TagType>())
|
|
|
|
return Tag->getDecl();
|
|
|
|
|
|
|
|
// The resulting type is not a tag; complain.
|
|
|
|
getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cast_or_null<NamedDecl>(TransformDecl(D));
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:17:43 +04:00
|
|
|
VarDecl *
|
|
|
|
TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType T,
|
2009-08-20 11:17:43 +04:00
|
|
|
DeclaratorInfo *Declarator,
|
|
|
|
IdentifierInfo *Name,
|
2009-09-09 19:08:12 +04:00
|
|
|
SourceLocation Loc,
|
2009-08-20 11:17:43 +04:00
|
|
|
SourceRange TypeRange) {
|
|
|
|
VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
|
|
|
|
Name, Loc, TypeRange);
|
|
|
|
if (Var && !Var->isInvalidDecl())
|
|
|
|
getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
|
|
|
|
return Var;
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:59:25 +04:00
|
|
|
QualType
|
|
|
|
TemplateInstantiator::RebuildElaboratedType(QualType T,
|
|
|
|
ElaboratedType::TagKind Tag) {
|
|
|
|
if (const TagType *TT = T->getAs<TagType>()) {
|
|
|
|
TagDecl* TD = TT->getDecl();
|
|
|
|
|
|
|
|
// FIXME: this location is very wrong; we really need typelocs.
|
|
|
|
SourceLocation TagLocation = TD->getTagKeywordLoc();
|
|
|
|
|
|
|
|
// FIXME: type might be anonymous.
|
|
|
|
IdentifierInfo *Id = TD->getIdentifier();
|
|
|
|
|
|
|
|
// TODO: should we even warn on struct/class mismatches for this? Seems
|
|
|
|
// like it's likely to produce a lot of spurious errors.
|
|
|
|
if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
|
|
|
|
SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
|
|
|
|
<< Id
|
|
|
|
<< CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
|
|
|
|
TD->getKindName());
|
|
|
|
SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sema::OwningExprResult
|
2009-11-04 10:01:15 +03:00
|
|
|
TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E,
|
|
|
|
bool isAddressOfOperand) {
|
2009-09-11 05:22:35 +04:00
|
|
|
if (!E->isTypeDependent())
|
|
|
|
return SemaRef.Owned(E->Retain());
|
|
|
|
|
|
|
|
FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
|
|
|
|
assert(currentDecl && "Must have current function declaration when "
|
|
|
|
"instantiating.");
|
|
|
|
|
|
|
|
PredefinedExpr::IdentType IT = E->getIdentType();
|
|
|
|
|
|
|
|
unsigned Length =
|
|
|
|
PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
|
|
|
|
|
|
|
|
llvm::APInt LengthI(32, Length + 1);
|
2009-09-24 23:53:00 +04:00
|
|
|
QualType ResTy = getSema().Context.CharTy.withConst();
|
2009-09-11 05:22:35 +04:00
|
|
|
ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
|
|
|
|
ArrayType::Normal, 0);
|
|
|
|
PredefinedExpr *PE =
|
|
|
|
new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
|
|
|
|
return getSema().Owned(PE);
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::OwningExprResult
|
2009-11-04 10:01:15 +03:00
|
|
|
TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E,
|
|
|
|
bool isAddressOfOperand) {
|
2009-08-11 09:31:07 +04:00
|
|
|
// FIXME: Clean this up a bit
|
|
|
|
NamedDecl *D = E->getDecl();
|
|
|
|
if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
|
2009-10-31 20:21:17 +03:00
|
|
|
if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
|
|
|
|
|
|
|
|
// If the corresponding template argument is NULL or non-existent, it's
|
|
|
|
// because we are performing instantiation from explicitly-specified
|
|
|
|
// template arguments in a function template, but there were some
|
|
|
|
// arguments left unspecified.
|
|
|
|
if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
|
|
|
|
NTTP->getPosition()))
|
|
|
|
return SemaRef.Owned(E->Retain());
|
|
|
|
|
|
|
|
const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
|
|
|
|
NTTP->getPosition());
|
|
|
|
|
|
|
|
// The template argument itself might be an expression, in which
|
|
|
|
// case we just return that expression.
|
|
|
|
if (Arg.getKind() == TemplateArgument::Expression)
|
|
|
|
return SemaRef.Owned(Arg.getAsExpr()->Retain());
|
|
|
|
|
|
|
|
if (Arg.getKind() == TemplateArgument::Declaration) {
|
|
|
|
ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
|
|
|
|
|
|
|
|
VD = cast_or_null<ValueDecl>(
|
2009-11-04 10:01:15 +03:00
|
|
|
getSema().FindInstantiatedDecl(VD, TemplateArgs));
|
2009-10-31 20:21:17 +03:00
|
|
|
if (!VD)
|
|
|
|
return SemaRef.ExprError();
|
|
|
|
|
|
|
|
return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
|
|
|
|
/*FIXME:*/false, /*FIXME:*/false);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-31 20:21:17 +03:00
|
|
|
assert(Arg.getKind() == TemplateArgument::Integral);
|
|
|
|
QualType T = Arg.getIntegralType();
|
|
|
|
if (T->isCharType() || T->isWideCharType())
|
|
|
|
return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
|
|
|
|
Arg.getAsIntegral()->getZExtValue(),
|
|
|
|
T->isWideCharType(),
|
2009-09-09 19:08:12 +04:00
|
|
|
T,
|
2009-08-29 00:31:08 +04:00
|
|
|
E->getSourceRange().getBegin()));
|
2009-10-31 20:21:17 +03:00
|
|
|
if (T->isBooleanType())
|
|
|
|
return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
|
|
|
|
Arg.getAsIntegral()->getBoolValue(),
|
|
|
|
T,
|
|
|
|
E->getSourceRange().getBegin()));
|
|
|
|
|
|
|
|
assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
|
|
|
|
return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
|
|
|
|
*Arg.getAsIntegral(),
|
|
|
|
T,
|
|
|
|
E->getSourceRange().getBegin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a non-type template parameter that isn't fully substituted;
|
|
|
|
// FindInstantiatedDecl will find it in the local instantiation scope.
|
2009-08-11 09:31:07 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-16 22:34:49 +04:00
|
|
|
NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
|
2009-08-11 09:31:07 +04:00
|
|
|
if (!InstD)
|
|
|
|
return SemaRef.ExprError();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-29 23:37:28 +04:00
|
|
|
// If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
|
2009-09-09 19:08:12 +04:00
|
|
|
// we need to get the underlying decl.
|
2009-08-29 23:37:28 +04:00
|
|
|
// FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
|
|
|
|
InstD = InstD->getUnderlyingDecl();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-23 22:54:35 +04:00
|
|
|
CXXScopeSpec SS;
|
|
|
|
NestedNameSpecifier *Qualifier = 0;
|
|
|
|
if (E->getQualifier()) {
|
|
|
|
Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
|
|
|
|
E->getQualifierRange());
|
|
|
|
if (!Qualifier)
|
|
|
|
return SemaRef.ExprError();
|
|
|
|
|
|
|
|
SS.setScopeRep(Qualifier);
|
|
|
|
SS.setRange(E->getQualifierRange());
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
|
2009-08-11 09:31:07 +04:00
|
|
|
/*FIXME:*/false,
|
2009-10-23 22:54:35 +04:00
|
|
|
&SS,
|
2009-11-04 10:01:15 +03:00
|
|
|
isAddressOfOperand);
|
2009-08-11 09:31:07 +04:00
|
|
|
}
|
|
|
|
|
2009-11-08 16:56:19 +03:00
|
|
|
Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
|
|
|
|
CXXDefaultArgExpr *E, bool isAddressOfOperand) {
|
|
|
|
assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
|
|
|
|
getDescribedFunctionTemplate() &&
|
|
|
|
"Default arg expressions are never formed in dependent cases.");
|
|
|
|
return SemaRef.Owned(E->Retain());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType
|
2009-10-21 04:40:46 +04:00
|
|
|
TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
|
|
|
|
TemplateTypeParmTypeLoc TL) {
|
|
|
|
TemplateTypeParmType *T = TL.getTypePtr();
|
2009-08-29 00:31:08 +04:00
|
|
|
if (T->getDepth() < TemplateArgs.getNumLevels()) {
|
2009-02-27 22:31:52 +03:00
|
|
|
// Replace the template type parameter with its corresponding
|
|
|
|
// template argument.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
// If the corresponding template argument is NULL or doesn't exist, it's
|
|
|
|
// because we are performing instantiation from explicitly-specified
|
|
|
|
// template arguments in a function template class, but there were some
|
2009-07-01 04:28:38 +04:00
|
|
|
// arguments left unspecified.
|
2009-10-21 04:40:46 +04:00
|
|
|
if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
|
|
|
|
TemplateTypeParmTypeLoc NewTL
|
|
|
|
= TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
|
|
|
|
NewTL.setNameLoc(TL.getNameLoc());
|
|
|
|
return TL.getType();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
|
2009-08-29 00:31:08 +04:00
|
|
|
== TemplateArgument::Type &&
|
2009-02-27 22:31:52 +03:00
|
|
|
"Template argument kind mismatch");
|
2009-08-29 00:31:08 +04:00
|
|
|
|
2009-10-18 13:09:24 +04:00
|
|
|
QualType Replacement
|
|
|
|
= TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
|
|
|
|
|
|
|
|
// TODO: only do this uniquing once, at the start of instantiation.
|
2009-10-21 04:40:46 +04:00
|
|
|
QualType Result
|
|
|
|
= getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
|
|
|
|
SubstTemplateTypeParmTypeLoc NewTL
|
|
|
|
= TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
|
|
|
|
NewTL.setNameLoc(TL.getNameLoc());
|
|
|
|
return Result;
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2009-02-27 22:31:52 +03:00
|
|
|
|
|
|
|
// The template type parameter comes from an inner template (e.g.,
|
|
|
|
// the template parameter list of a member template inside the
|
|
|
|
// template we are instantiating). Create a new template type
|
|
|
|
// parameter with the template "level" reduced by one.
|
2009-10-21 04:40:46 +04:00
|
|
|
QualType Result
|
|
|
|
= getSema().Context.getTemplateTypeParmType(T->getDepth()
|
|
|
|
- TemplateArgs.getNumLevels(),
|
|
|
|
T->getIndex(),
|
|
|
|
T->isParameterPack(),
|
|
|
|
T->getName());
|
|
|
|
TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
|
|
|
|
NewTL.setNameLoc(TL.getNameLoc());
|
|
|
|
return Result;
|
2009-02-28 03:25:32 +03:00
|
|
|
}
|
2009-02-27 22:31:52 +03:00
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
/// \brief Perform substitution on the type T with a given set of template
|
|
|
|
/// arguments.
|
2009-02-27 22:31:52 +03:00
|
|
|
///
|
|
|
|
/// This routine substitutes the given template arguments into the
|
|
|
|
/// type T and produces the instantiated type.
|
|
|
|
///
|
|
|
|
/// \param T the type into which the template arguments will be
|
|
|
|
/// substituted. If this type is not dependent, it will be returned
|
|
|
|
/// immediately.
|
|
|
|
///
|
|
|
|
/// \param TemplateArgs the template arguments that will be
|
|
|
|
/// substituted for the top-level template parameters within T.
|
|
|
|
///
|
|
|
|
/// \param Loc the location in the source code where this substitution
|
|
|
|
/// is being performed. It will typically be the location of the
|
|
|
|
/// declarator (if we're instantiating the type of some declaration)
|
|
|
|
/// or the location of the type in the source code (if, e.g., we're
|
|
|
|
/// instantiating the type of a cast expression).
|
|
|
|
///
|
|
|
|
/// \param Entity the name of the entity associated with a declaration
|
|
|
|
/// being instantiated (if any). May be empty to indicate that there
|
|
|
|
/// is no such entity (if, e.g., this is a type that occurs as part of
|
|
|
|
/// a cast expression) or that the entity has no name (e.g., an
|
|
|
|
/// unnamed function parameter).
|
|
|
|
///
|
|
|
|
/// \returns If the instantiation succeeds, the instantiated
|
|
|
|
/// type. Otherwise, produces diagnostics and returns a NULL type.
|
2009-10-21 04:58:09 +04:00
|
|
|
DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
|
|
|
|
const MultiLevelTemplateArgumentList &Args,
|
|
|
|
SourceLocation Loc,
|
|
|
|
DeclarationName Entity) {
|
|
|
|
assert(!ActiveTemplateInstantiations.empty() &&
|
|
|
|
"Cannot perform an instantiation without some context on the "
|
|
|
|
"instantiation stack");
|
|
|
|
|
|
|
|
if (!T->getType()->isDependentType())
|
|
|
|
return T;
|
|
|
|
|
|
|
|
TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
|
|
|
|
return Instantiator.TransformType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deprecated form of the above.
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType Sema::SubstType(QualType T,
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs,
|
2009-08-26 02:02:44 +04:00
|
|
|
SourceLocation Loc, DeclarationName Entity) {
|
2009-03-10 23:44:00 +03:00
|
|
|
assert(!ActiveTemplateInstantiations.empty() &&
|
|
|
|
"Cannot perform an instantiation without some context on the "
|
|
|
|
"instantiation stack");
|
|
|
|
|
2009-02-27 22:31:52 +03:00
|
|
|
// If T is not a dependent type, there is nothing to do.
|
|
|
|
if (!T->isDependentType())
|
|
|
|
return T;
|
|
|
|
|
2009-08-04 20:50:30 +04:00
|
|
|
TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
|
|
|
|
return Instantiator.TransformType(T);
|
2009-02-27 22:31:52 +03:00
|
|
|
}
|
2009-03-03 07:44:36 +03:00
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
/// \brief Perform substitution on the base class specifiers of the
|
|
|
|
/// given class template specialization.
|
2009-03-03 07:44:36 +03:00
|
|
|
///
|
|
|
|
/// Produces a diagnostic and returns true on error, returns false and
|
|
|
|
/// attaches the instantiated base classes to the class template
|
|
|
|
/// specialization if successful.
|
2009-09-09 19:08:12 +04:00
|
|
|
bool
|
2009-08-26 02:02:44 +04:00
|
|
|
Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
|
|
|
|
CXXRecordDecl *Pattern,
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-03-03 07:44:36 +03:00
|
|
|
bool Invalid = false;
|
2009-05-29 22:27:38 +04:00
|
|
|
llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
|
2009-09-09 19:08:12 +04:00
|
|
|
for (ClassTemplateSpecializationDecl::base_class_iterator
|
2009-03-26 00:17:03 +03:00
|
|
|
Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
|
2009-03-10 21:52:44 +03:00
|
|
|
Base != BaseEnd; ++Base) {
|
2009-03-03 07:44:36 +03:00
|
|
|
if (!Base->getType()->isDependentType()) {
|
2009-07-22 21:41:53 +04:00
|
|
|
InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
|
2009-03-03 07:44:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
QualType BaseType = SubstType(Base->getType(),
|
|
|
|
TemplateArgs,
|
2009-08-26 02:02:44 +04:00
|
|
|
Base->getSourceRange().getBegin(),
|
|
|
|
DeclarationName());
|
2009-03-03 07:44:36 +03:00
|
|
|
if (BaseType.isNull()) {
|
|
|
|
Invalid = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CXXBaseSpecifier *InstantiatedBase
|
2009-03-26 00:17:03 +03:00
|
|
|
= CheckBaseSpecifier(Instantiation,
|
2009-03-03 07:44:36 +03:00
|
|
|
Base->getSourceRange(),
|
|
|
|
Base->isVirtual(),
|
|
|
|
Base->getAccessSpecifierAsWritten(),
|
|
|
|
BaseType,
|
|
|
|
/*FIXME: Not totally accurate */
|
|
|
|
Base->getSourceRange().getBegin()))
|
|
|
|
InstantiatedBases.push_back(InstantiatedBase);
|
|
|
|
else
|
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
|
2009-03-10 21:52:44 +03:00
|
|
|
if (!Invalid &&
|
2009-05-21 13:52:38 +04:00
|
|
|
AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
|
2009-03-03 07:44:36 +03:00
|
|
|
InstantiatedBases.size()))
|
|
|
|
Invalid = true;
|
|
|
|
|
|
|
|
return Invalid;
|
|
|
|
}
|
|
|
|
|
2009-03-26 00:17:03 +03:00
|
|
|
/// \brief Instantiate the definition of a class from a given pattern.
|
|
|
|
///
|
|
|
|
/// \param PointOfInstantiation The point of instantiation within the
|
|
|
|
/// source code.
|
|
|
|
///
|
|
|
|
/// \param Instantiation is the declaration whose definition is being
|
|
|
|
/// instantiated. This will be either a class template specialization
|
|
|
|
/// or a member class of a class template specialization.
|
|
|
|
///
|
|
|
|
/// \param Pattern is the pattern from which the instantiation
|
|
|
|
/// occurs. This will be either the declaration of a class template or
|
|
|
|
/// the declaration of a member class of a class template.
|
|
|
|
///
|
|
|
|
/// \param TemplateArgs The template arguments to be substituted into
|
|
|
|
/// the pattern.
|
|
|
|
///
|
2009-09-05 02:48:11 +04:00
|
|
|
/// \param TSK the kind of implicit or explicit instantiation to perform.
|
2009-08-24 19:23:48 +04:00
|
|
|
///
|
|
|
|
/// \param Complain whether to complain if the class cannot be instantiated due
|
|
|
|
/// to the lack of a definition.
|
|
|
|
///
|
2009-03-26 00:17:03 +03:00
|
|
|
/// \returns true if an error occurred, false otherwise.
|
|
|
|
bool
|
|
|
|
Sema::InstantiateClass(SourceLocation PointOfInstantiation,
|
|
|
|
CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs,
|
2009-09-05 02:48:11 +04:00
|
|
|
TemplateSpecializationKind TSK,
|
2009-08-24 19:23:48 +04:00
|
|
|
bool Complain) {
|
2009-03-26 00:17:03 +03:00
|
|
|
bool Invalid = false;
|
2009-08-20 05:44:21 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
CXXRecordDecl *PatternDef
|
2009-03-26 00:17:03 +03:00
|
|
|
= cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
|
|
|
|
if (!PatternDef) {
|
2009-08-24 19:23:48 +04:00
|
|
|
if (!Complain) {
|
|
|
|
// Say nothing
|
|
|
|
} else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
|
2009-03-26 00:17:03 +03:00
|
|
|
Diag(PointOfInstantiation,
|
|
|
|
diag::err_implicit_instantiate_member_undefined)
|
|
|
|
<< Context.getTypeDeclType(Instantiation);
|
|
|
|
Diag(Pattern->getLocation(), diag::note_member_of_template_here);
|
|
|
|
} else {
|
2009-05-13 04:25:59 +04:00
|
|
|
Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
|
2009-09-05 02:48:11 +04:00
|
|
|
<< (TSK != TSK_ImplicitInstantiation)
|
2009-03-26 00:17:03 +03:00
|
|
|
<< Context.getTypeDeclType(Instantiation);
|
|
|
|
Diag(Pattern->getLocation(), diag::note_template_decl_here);
|
|
|
|
}
|
2009-03-03 07:44:36 +03:00
|
|
|
return true;
|
|
|
|
}
|
2009-03-26 00:17:03 +03:00
|
|
|
Pattern = PatternDef;
|
2009-03-03 07:44:36 +03:00
|
|
|
|
2009-10-15 19:54:05 +04:00
|
|
|
// \brief Record the point of instantiation.
|
|
|
|
if (MemberSpecializationInfo *MSInfo
|
|
|
|
= Instantiation->getMemberSpecializationInfo()) {
|
|
|
|
MSInfo->setTemplateSpecializationKind(TSK);
|
|
|
|
MSInfo->setPointOfInstantiation(PointOfInstantiation);
|
2009-10-27 09:26:26 +03:00
|
|
|
} else if (ClassTemplateSpecializationDecl *Spec
|
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
|
|
|
|
Spec->setTemplateSpecializationKind(TSK);
|
|
|
|
Spec->setPointOfInstantiation(PointOfInstantiation);
|
2009-10-15 19:54:05 +04:00
|
|
|
}
|
|
|
|
|
2009-03-26 00:23:52 +03:00
|
|
|
InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
|
2009-03-10 03:06:19 +03:00
|
|
|
if (Inst)
|
|
|
|
return true;
|
|
|
|
|
2009-03-03 07:44:36 +03:00
|
|
|
// Enter the scope of this instantiation. We don't use
|
|
|
|
// PushDeclContext because we don't have a scope.
|
|
|
|
DeclContext *PreviousContext = CurContext;
|
2009-03-26 00:17:03 +03:00
|
|
|
CurContext = Instantiation;
|
2009-03-03 07:44:36 +03:00
|
|
|
|
|
|
|
// Start the definition of this instantiation.
|
2009-03-26 00:17:03 +03:00
|
|
|
Instantiation->startDefinition();
|
2009-03-03 07:44:36 +03:00
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
// Do substitution on the base class specifiers.
|
|
|
|
if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
|
2009-03-03 07:44:36 +03:00
|
|
|
Invalid = true;
|
|
|
|
|
2009-05-29 22:27:38 +04:00
|
|
|
llvm::SmallVector<DeclPtrTy, 4> Fields;
|
2009-06-30 06:36:12 +04:00
|
|
|
for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
|
2009-09-09 19:08:12 +04:00
|
|
|
MemberEnd = Pattern->decls_end();
|
2009-04-10 01:40:53 +04:00
|
|
|
Member != MemberEnd; ++Member) {
|
2009-08-26 02:02:44 +04:00
|
|
|
Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
|
2009-03-18 00:15:40 +03:00
|
|
|
if (NewMember) {
|
|
|
|
if (NewMember->isInvalidDecl())
|
2009-03-15 21:44:04 +03:00
|
|
|
Invalid = true;
|
2009-03-18 00:15:40 +03:00
|
|
|
else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
|
2009-03-28 22:18:32 +03:00
|
|
|
Fields.push_back(DeclPtrTy::make(Field));
|
2009-08-29 23:37:28 +04:00
|
|
|
else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
|
|
|
|
Instantiation->addDecl(UD);
|
2009-03-18 00:15:40 +03:00
|
|
|
} else {
|
|
|
|
// FIXME: Eventually, a NULL return will mean that one of the
|
2009-05-16 11:39:55 +04:00
|
|
|
// instantiations was a semantic disaster, and we'll want to set Invalid =
|
|
|
|
// true. For now, we expect to skip some members that we can't yet handle.
|
2009-03-11 19:48:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 21:59:21 +03:00
|
|
|
// Finish checking fields.
|
2009-03-28 22:18:32 +03:00
|
|
|
ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
|
2009-05-21 13:52:38 +04:00
|
|
|
Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
|
2009-03-11 21:59:21 +03:00
|
|
|
0);
|
2009-10-15 00:14:33 +04:00
|
|
|
if (Instantiation->isInvalidDecl())
|
|
|
|
Invalid = true;
|
|
|
|
|
2009-03-03 07:44:36 +03:00
|
|
|
// Add any implicitly-declared members that we might need.
|
2009-10-15 00:14:33 +04:00
|
|
|
if (!Invalid)
|
|
|
|
AddImplicitlyDeclaredMembersToClass(Instantiation);
|
2009-03-03 07:44:36 +03:00
|
|
|
|
|
|
|
// Exit the scope of this instantiation.
|
|
|
|
CurContext = PreviousContext;
|
|
|
|
|
2009-05-27 00:50:29 +04:00
|
|
|
if (!Invalid)
|
|
|
|
Consumer.HandleTagDeclDefinition(Instantiation);
|
|
|
|
|
2009-03-03 07:44:36 +03:00
|
|
|
return Invalid;
|
|
|
|
}
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67308 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-19 20:26:29 +03:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
bool
|
2009-03-26 00:17:03 +03:00
|
|
|
Sema::InstantiateClassTemplateSpecialization(
|
2009-10-27 09:26:26 +03:00
|
|
|
SourceLocation PointOfInstantiation,
|
2009-03-26 00:17:03 +03:00
|
|
|
ClassTemplateSpecializationDecl *ClassTemplateSpec,
|
2009-09-05 02:48:11 +04:00
|
|
|
TemplateSpecializationKind TSK,
|
2009-08-24 19:23:48 +04:00
|
|
|
bool Complain) {
|
2009-03-26 00:17:03 +03:00
|
|
|
// Perform the actual instantiation on the canonical declaration.
|
|
|
|
ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
|
2009-07-18 04:34:25 +04:00
|
|
|
ClassTemplateSpec->getCanonicalDecl());
|
2009-03-26 00:17:03 +03:00
|
|
|
|
2009-09-12 01:19:12 +04:00
|
|
|
// Check whether we have already instantiated or specialized this class
|
|
|
|
// template specialization.
|
|
|
|
if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
|
|
|
|
if (ClassTemplateSpec->getSpecializationKind() ==
|
|
|
|
TSK_ExplicitInstantiationDeclaration &&
|
|
|
|
TSK == TSK_ExplicitInstantiationDefinition) {
|
|
|
|
// An explicit instantiation definition follows an explicit instantiation
|
|
|
|
// declaration (C++0x [temp.explicit]p10); go ahead and perform the
|
|
|
|
// explicit instantiation.
|
|
|
|
ClassTemplateSpec->setSpecializationKind(TSK);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can only instantiate something that hasn't already been
|
|
|
|
// instantiated or specialized. Fail without any diagnostics: our
|
|
|
|
// caller will provide an error message.
|
2009-03-26 00:17:03 +03:00
|
|
|
return true;
|
2009-09-12 01:19:12 +04:00
|
|
|
}
|
2009-03-26 00:17:03 +03:00
|
|
|
|
2009-09-15 20:51:42 +04:00
|
|
|
if (ClassTemplateSpec->isInvalidDecl())
|
|
|
|
return true;
|
|
|
|
|
2009-03-26 00:17:03 +03:00
|
|
|
ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
|
2009-08-29 00:31:08 +04:00
|
|
|
CXXRecordDecl *Pattern = 0;
|
2009-05-31 13:31:02 +04:00
|
|
|
|
2009-06-13 02:31:52 +04:00
|
|
|
// C++ [temp.class.spec.match]p1:
|
|
|
|
// When a class template is used in a context that requires an
|
|
|
|
// instantiation of the class, it is necessary to determine
|
|
|
|
// whether the instantiation is to be generated using the primary
|
|
|
|
// template or one of the partial specializations. This is done by
|
|
|
|
// matching the template arguments of the class template
|
|
|
|
// specialization with the template argument lists of the partial
|
|
|
|
// specializations.
|
2009-06-05 04:53:49 +04:00
|
|
|
typedef std::pair<ClassTemplatePartialSpecializationDecl *,
|
|
|
|
TemplateArgumentList *> MatchResult;
|
|
|
|
llvm::SmallVector<MatchResult, 4> Matched;
|
2009-09-09 19:08:12 +04:00
|
|
|
for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
|
2009-05-31 13:31:02 +04:00
|
|
|
Partial = Template->getPartialSpecializations().begin(),
|
|
|
|
PartialEnd = Template->getPartialSpecializations().end();
|
|
|
|
Partial != PartialEnd;
|
|
|
|
++Partial) {
|
2009-06-12 22:26:56 +04:00
|
|
|
TemplateDeductionInfo Info(Context);
|
|
|
|
if (TemplateDeductionResult Result
|
2009-09-09 19:08:12 +04:00
|
|
|
= DeduceTemplateArguments(&*Partial,
|
2009-06-12 22:26:56 +04:00
|
|
|
ClassTemplateSpec->getTemplateArgs(),
|
|
|
|
Info)) {
|
|
|
|
// FIXME: Store the failed-deduction information for use in
|
|
|
|
// diagnostics, later.
|
|
|
|
(void)Result;
|
|
|
|
} else {
|
|
|
|
Matched.push_back(std::make_pair(&*Partial, Info.take()));
|
|
|
|
}
|
2009-05-31 13:31:02 +04:00
|
|
|
}
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
if (Matched.size() >= 1) {
|
2009-09-15 20:23:51 +04:00
|
|
|
llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
|
2009-10-29 03:04:11 +03:00
|
|
|
if (Matched.size() == 1) {
|
|
|
|
// -- If exactly one matching specialization is found, the
|
|
|
|
// instantiation is generated from that specialization.
|
|
|
|
// We don't need to do anything for this.
|
|
|
|
} else {
|
|
|
|
// -- If more than one matching specialization is found, the
|
|
|
|
// partial order rules (14.5.4.2) are used to determine
|
|
|
|
// whether one of the specializations is more specialized
|
|
|
|
// than the others. If none of the specializations is more
|
|
|
|
// specialized than all of the other matching
|
|
|
|
// specializations, then the use of the class template is
|
|
|
|
// ambiguous and the program is ill-formed.
|
|
|
|
for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
|
|
|
|
PEnd = Matched.end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
|
|
|
|
== P->first)
|
|
|
|
Best = P;
|
2009-09-15 20:23:51 +04:00
|
|
|
}
|
|
|
|
|
2009-10-29 03:04:11 +03:00
|
|
|
// Determine if the best partial specialization is more specialized than
|
|
|
|
// the others.
|
|
|
|
bool Ambiguous = false;
|
2009-09-15 20:23:51 +04:00
|
|
|
for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
|
|
|
|
PEnd = Matched.end();
|
2009-10-29 03:04:11 +03:00
|
|
|
P != PEnd; ++P) {
|
|
|
|
if (P != Best &&
|
|
|
|
getMoreSpecializedPartialSpecialization(P->first, Best->first)
|
|
|
|
!= Best->first) {
|
|
|
|
Ambiguous = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ambiguous) {
|
|
|
|
// Partial ordering did not produce a clear winner. Complain.
|
|
|
|
ClassTemplateSpec->setInvalidDecl();
|
|
|
|
Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
|
|
|
|
<< ClassTemplateSpec;
|
|
|
|
|
|
|
|
// Print the matching partial specializations.
|
|
|
|
for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
|
|
|
|
PEnd = Matched.end();
|
|
|
|
P != PEnd; ++P)
|
|
|
|
Diag(P->first->getLocation(), diag::note_partial_spec_match)
|
|
|
|
<< getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
|
|
|
|
*P->second);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-15 20:23:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate using the best class template partial specialization.
|
2009-10-29 03:04:11 +03:00
|
|
|
ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
|
|
|
|
while (OrigPartialSpec->getInstantiatedFromMember()) {
|
|
|
|
// If we've found an explicit specialization of this class template,
|
|
|
|
// stop here and use that as the pattern.
|
|
|
|
if (OrigPartialSpec->isMemberSpecialization())
|
|
|
|
break;
|
|
|
|
|
|
|
|
OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
|
|
|
|
}
|
|
|
|
|
|
|
|
Pattern = OrigPartialSpec;
|
2009-09-15 20:23:51 +04:00
|
|
|
ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
|
2009-06-13 02:31:52 +04:00
|
|
|
} else {
|
|
|
|
// -- If no matches are found, the instantiation is generated
|
|
|
|
// from the primary template.
|
2009-08-29 00:31:08 +04:00
|
|
|
ClassTemplateDecl *OrigTemplate = Template;
|
2009-10-13 20:30:37 +04:00
|
|
|
while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
|
|
|
|
// If we've found an explicit specialization of this class template,
|
|
|
|
// stop here and use that as the pattern.
|
|
|
|
if (OrigTemplate->isMemberSpecialization())
|
|
|
|
break;
|
|
|
|
|
2009-08-29 00:31:08 +04:00
|
|
|
OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
|
2009-10-13 20:30:37 +04:00
|
|
|
}
|
|
|
|
|
2009-08-29 00:31:08 +04:00
|
|
|
Pattern = OrigTemplate->getTemplatedDecl();
|
2009-05-31 13:31:02 +04:00
|
|
|
}
|
2009-03-26 00:17:03 +03:00
|
|
|
|
2009-10-27 09:26:26 +03:00
|
|
|
bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
|
|
|
|
Pattern,
|
|
|
|
getTemplateInstantiationArgs(ClassTemplateSpec),
|
2009-09-05 02:48:11 +04:00
|
|
|
TSK,
|
2009-08-24 19:23:48 +04:00
|
|
|
Complain);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-05 04:53:49 +04:00
|
|
|
for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
|
|
|
|
// FIXME: Implement TemplateArgumentList::Destroy!
|
|
|
|
// if (Matched[I].first != Pattern)
|
|
|
|
// Matched[I].second->Destroy(Context);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-05 04:53:49 +04:00
|
|
|
return Result;
|
2009-03-26 00:17:03 +03:00
|
|
|
}
|
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
/// \brief Instantiates the definitions of all of the member
|
|
|
|
/// of the given class, which is an instantiation of a class template
|
|
|
|
/// or a member class of a template.
|
2009-05-14 00:28:22 +04:00
|
|
|
void
|
|
|
|
Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
|
2009-09-05 02:48:11 +04:00
|
|
|
CXXRecordDecl *Instantiation,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs,
|
|
|
|
TemplateSpecializationKind TSK) {
|
2009-06-30 06:36:12 +04:00
|
|
|
for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
|
|
|
|
DEnd = Instantiation->decls_end();
|
2009-05-14 00:28:22 +04:00
|
|
|
D != DEnd; ++D) {
|
2009-10-27 21:42:08 +03:00
|
|
|
bool SuppressNew = false;
|
2009-05-14 00:28:22 +04:00
|
|
|
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
|
2009-10-27 21:42:08 +03:00
|
|
|
if (FunctionDecl *Pattern
|
|
|
|
= Function->getInstantiatedFromMemberFunction()) {
|
|
|
|
MemberSpecializationInfo *MSInfo
|
|
|
|
= Function->getMemberSpecializationInfo();
|
|
|
|
assert(MSInfo && "No member specialization information?");
|
|
|
|
if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
|
|
|
|
Function,
|
|
|
|
MSInfo->getTemplateSpecializationKind(),
|
|
|
|
MSInfo->getPointOfInstantiation(),
|
|
|
|
SuppressNew) ||
|
|
|
|
SuppressNew)
|
2009-10-08 19:14:33 +04:00
|
|
|
continue;
|
|
|
|
|
2009-10-27 21:42:08 +03:00
|
|
|
if (Function->getBody())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (TSK == TSK_ExplicitInstantiationDefinition) {
|
|
|
|
// C++0x [temp.explicit]p8:
|
|
|
|
// An explicit instantiation definition that names a class template
|
|
|
|
// specialization explicitly instantiates the class template
|
|
|
|
// specialization and is only an explicit instantiation definition
|
|
|
|
// of members whose definition is visible at the point of
|
|
|
|
// instantiation.
|
|
|
|
if (!Pattern->getBody())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
|
|
|
|
|
|
|
|
InstantiateFunctionDefinition(PointOfInstantiation, Function);
|
|
|
|
} else {
|
|
|
|
Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
|
|
|
|
}
|
2009-10-08 19:14:33 +04:00
|
|
|
}
|
2009-05-14 00:28:22 +04:00
|
|
|
} else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
|
2009-10-08 11:24:58 +04:00
|
|
|
if (Var->isStaticDataMember()) {
|
2009-10-27 21:42:08 +03:00
|
|
|
MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
|
|
|
|
assert(MSInfo && "No member specialization information?");
|
|
|
|
if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
|
|
|
|
Var,
|
|
|
|
MSInfo->getTemplateSpecializationKind(),
|
|
|
|
MSInfo->getPointOfInstantiation(),
|
|
|
|
SuppressNew) ||
|
|
|
|
SuppressNew)
|
2009-10-08 19:14:33 +04:00
|
|
|
continue;
|
|
|
|
|
2009-10-27 21:42:08 +03:00
|
|
|
if (TSK == TSK_ExplicitInstantiationDefinition) {
|
|
|
|
// C++0x [temp.explicit]p8:
|
|
|
|
// An explicit instantiation definition that names a class template
|
|
|
|
// specialization explicitly instantiates the class template
|
|
|
|
// specialization and is only an explicit instantiation definition
|
|
|
|
// of members whose definition is visible at the point of
|
|
|
|
// instantiation.
|
|
|
|
if (!Var->getInstantiatedFromStaticDataMember()
|
|
|
|
->getOutOfLineDefinition())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
|
2009-10-08 11:24:58 +04:00
|
|
|
InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
|
2009-10-27 21:42:08 +03:00
|
|
|
} else {
|
|
|
|
Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
|
|
|
|
}
|
|
|
|
}
|
2009-05-14 00:28:22 +04:00
|
|
|
} else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
|
2009-10-08 03:56:10 +04:00
|
|
|
if (Record->isInjectedClassName())
|
|
|
|
continue;
|
|
|
|
|
2009-10-27 21:42:08 +03:00
|
|
|
MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
|
|
|
|
assert(MSInfo && "No member specialization information?");
|
|
|
|
if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
|
|
|
|
Record,
|
|
|
|
MSInfo->getTemplateSpecializationKind(),
|
|
|
|
MSInfo->getPointOfInstantiation(),
|
|
|
|
SuppressNew) ||
|
|
|
|
SuppressNew)
|
2009-10-08 19:14:33 +04:00
|
|
|
continue;
|
|
|
|
|
2009-10-27 21:42:08 +03:00
|
|
|
CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
|
|
|
|
assert(Pattern && "Missing instantiated-from-template information");
|
|
|
|
|
|
|
|
if (!Record->getDefinition(Context)) {
|
|
|
|
if (!Pattern->getDefinition(Context)) {
|
|
|
|
// C++0x [temp.explicit]p8:
|
|
|
|
// An explicit instantiation definition that names a class template
|
|
|
|
// specialization explicitly instantiates the class template
|
|
|
|
// specialization and is only an explicit instantiation definition
|
|
|
|
// of members whose definition is visible at the point of
|
|
|
|
// instantiation.
|
|
|
|
if (TSK == TSK_ExplicitInstantiationDeclaration) {
|
|
|
|
MSInfo->setTemplateSpecializationKind(TSK);
|
|
|
|
MSInfo->setPointOfInstantiation(PointOfInstantiation);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstantiateClass(PointOfInstantiation, Record, Pattern,
|
2009-09-05 02:48:11 +04:00
|
|
|
TemplateArgs,
|
|
|
|
TSK);
|
2009-10-27 21:42:08 +03:00
|
|
|
}
|
2009-10-08 05:19:17 +04:00
|
|
|
|
2009-10-27 21:42:08 +03:00
|
|
|
Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
|
|
|
|
if (Pattern)
|
|
|
|
InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
|
|
|
|
TSK);
|
2009-05-14 00:28:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Instantiate the definitions of all of the members of the
|
|
|
|
/// given class template specialization, which was named as part of an
|
|
|
|
/// explicit instantiation.
|
2009-09-09 19:08:12 +04:00
|
|
|
void
|
2009-09-05 02:48:11 +04:00
|
|
|
Sema::InstantiateClassTemplateSpecializationMembers(
|
2009-05-14 00:28:22 +04:00
|
|
|
SourceLocation PointOfInstantiation,
|
2009-09-05 02:48:11 +04:00
|
|
|
ClassTemplateSpecializationDecl *ClassTemplateSpec,
|
|
|
|
TemplateSpecializationKind TSK) {
|
2009-05-14 00:28:22 +04:00
|
|
|
// C++0x [temp.explicit]p7:
|
|
|
|
// An explicit instantiation that names a class template
|
|
|
|
// specialization is an explicit instantion of the same kind
|
|
|
|
// (declaration or definition) of each of its members (not
|
|
|
|
// including members inherited from base classes) that has not
|
|
|
|
// been previously explicitly specialized in the translation unit
|
|
|
|
// containing the explicit instantiation, except as described
|
|
|
|
// below.
|
|
|
|
InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
|
2009-09-05 02:48:11 +04:00
|
|
|
getTemplateInstantiationArgs(ClassTemplateSpec),
|
|
|
|
TSK);
|
2009-05-14 00:28:22 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::OwningStmtResult
|
2009-08-29 00:31:08 +04:00
|
|
|
Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-08-20 11:17:43 +04:00
|
|
|
if (!S)
|
|
|
|
return Owned(S);
|
|
|
|
|
|
|
|
TemplateInstantiator Instantiator(*this, TemplateArgs,
|
|
|
|
SourceLocation(),
|
|
|
|
DeclarationName());
|
|
|
|
return Instantiator.TransformStmt(S);
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
Sema::OwningExprResult
|
2009-08-29 00:31:08 +04:00
|
|
|
Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-08-11 09:31:07 +04:00
|
|
|
if (!E)
|
|
|
|
return Owned(E);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-11 09:31:07 +04:00
|
|
|
TemplateInstantiator Instantiator(*this, TemplateArgs,
|
|
|
|
SourceLocation(),
|
|
|
|
DeclarationName());
|
|
|
|
return Instantiator.TransformExpr(E);
|
|
|
|
}
|
|
|
|
|
2009-08-26 02:02:44 +04:00
|
|
|
/// \brief Do template substitution on a nested-name-specifier.
|
2009-03-27 02:50:42 +03:00
|
|
|
NestedNameSpecifier *
|
2009-08-26 02:02:44 +04:00
|
|
|
Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
|
2009-08-29 00:31:08 +04:00
|
|
|
SourceRange Range,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-08-06 09:28:30 +04:00
|
|
|
TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
|
|
|
|
DeclarationName());
|
|
|
|
return Instantiator.TransformNestedNameSpecifier(NNS, Range);
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67308 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-19 20:26:29 +03:00
|
|
|
}
|
2009-03-31 22:38:02 +04:00
|
|
|
|
|
|
|
TemplateName
|
2009-08-26 02:02:44 +04:00
|
|
|
Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
|
2009-08-29 00:31:08 +04:00
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-08-06 10:41:21 +04:00
|
|
|
TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
|
|
|
|
DeclarationName());
|
|
|
|
return Instantiator.TransformTemplateName(Name);
|
2009-03-31 22:38:02 +04:00
|
|
|
}
|
2009-06-11 04:06:24 +04:00
|
|
|
|
2009-10-29 11:12:44 +03:00
|
|
|
bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
|
|
|
|
const MultiLevelTemplateArgumentList &TemplateArgs) {
|
2009-08-05 02:27:00 +04:00
|
|
|
TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
|
|
|
|
DeclarationName());
|
2009-10-29 11:12:44 +03:00
|
|
|
|
|
|
|
return Instantiator.TransformTemplateArgument(Input, Output);
|
2009-06-11 04:06:24 +04:00
|
|
|
}
|