зеркало из https://github.com/microsoft/clang-1.git
Implement "incremental" template instantiation for non-type template
parameters and template type parameters, which occurs when substituting into the declarations of member templates inside class templates. This eliminates errors about our inability to "reduce non-type template parameter depth", fixing PR5311. Also fixes a bug when instantiating a template type parameter declaration in a member template, where we weren't properly reducing the template parameter's depth. LLVM's StringSwitch header now parses. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85669 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Родитель
df7c3b955e
Коммит
550d9b28fd
|
@ -3117,7 +3117,7 @@ public:
|
||||||
llvm::DenseMap<const Decl *, Decl *> LocalDecls;
|
llvm::DenseMap<const Decl *, Decl *> LocalDecls;
|
||||||
|
|
||||||
/// \brief The outer scope, in which contains local variable
|
/// \brief The outer scope, in which contains local variable
|
||||||
/// definitions from some other instantiation (that is not
|
/// definitions from some other instantiation (that may not be
|
||||||
/// relevant to this particular scope).
|
/// relevant to this particular scope).
|
||||||
LocalInstantiationScope *Outer;
|
LocalInstantiationScope *Outer;
|
||||||
|
|
||||||
|
@ -3126,9 +3126,13 @@ public:
|
||||||
LocalInstantiationScope &operator=(const LocalInstantiationScope &);
|
LocalInstantiationScope &operator=(const LocalInstantiationScope &);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LocalInstantiationScope(Sema &SemaRef)
|
LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope = false)
|
||||||
: SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope) {
|
: SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope) {
|
||||||
SemaRef.CurrentInstantiationScope = this;
|
if (!CombineWithOuterScope)
|
||||||
|
SemaRef.CurrentInstantiationScope = this;
|
||||||
|
else
|
||||||
|
assert(SemaRef.CurrentInstantiationScope &&
|
||||||
|
"No outer instantiation scope?");
|
||||||
}
|
}
|
||||||
|
|
||||||
~LocalInstantiationScope() {
|
~LocalInstantiationScope() {
|
||||||
|
@ -3149,6 +3153,11 @@ public:
|
||||||
return cast<ParmVarDecl>(getInstantiationOf(cast<Decl>(Var)));
|
return cast<ParmVarDecl>(getInstantiationOf(cast<Decl>(Var)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NonTypeTemplateParmDecl *getInstantiationOf(
|
||||||
|
const NonTypeTemplateParmDecl *Var) {
|
||||||
|
return cast<NonTypeTemplateParmDecl>(getInstantiationOf(cast<Decl>(Var)));
|
||||||
|
}
|
||||||
|
|
||||||
void InstantiatedLocal(const Decl *D, Decl *Inst) {
|
void InstantiatedLocal(const Decl *D, Decl *Inst) {
|
||||||
Decl *&Stored = LocalDecls[D];
|
Decl *&Stored = LocalDecls[D];
|
||||||
assert(!Stored && "Already instantiated this local");
|
assert(!Stored && "Already instantiated this local");
|
||||||
|
|
|
@ -562,58 +562,59 @@ TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
|
||||||
// FIXME: Clean this up a bit
|
// FIXME: Clean this up a bit
|
||||||
NamedDecl *D = E->getDecl();
|
NamedDecl *D = E->getDecl();
|
||||||
if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
|
if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
|
||||||
if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
|
if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
|
||||||
assert(false && "Cannot reduce non-type template parameter depth yet");
|
|
||||||
return getSema().ExprError();
|
// 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());
|
||||||
|
|
||||||
// If the corresponding template argument is NULL or non-existent, it's
|
const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
|
||||||
// because we are performing instantiation from explicitly-specified
|
NTTP->getPosition());
|
||||||
// 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(),
|
// The template argument itself might be an expression, in which
|
||||||
NTTP->getPosition());
|
// case we just return that expression.
|
||||||
|
if (Arg.getKind() == TemplateArgument::Expression)
|
||||||
|
return SemaRef.Owned(Arg.getAsExpr()->Retain());
|
||||||
|
|
||||||
// The template argument itself might be an expression, in which
|
if (Arg.getKind() == TemplateArgument::Declaration) {
|
||||||
// case we just return that expression.
|
ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
|
||||||
if (Arg.getKind() == TemplateArgument::Expression)
|
|
||||||
return SemaRef.Owned(Arg.getAsExpr()->Retain());
|
|
||||||
|
|
||||||
if (Arg.getKind() == TemplateArgument::Declaration) {
|
VD = cast_or_null<ValueDecl>(
|
||||||
ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
|
getSema().FindInstantiatedDecl(VD, TemplateArgs));
|
||||||
|
if (!VD)
|
||||||
|
return SemaRef.ExprError();
|
||||||
|
|
||||||
VD = cast_or_null<ValueDecl>(
|
return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
|
||||||
getSema().FindInstantiatedDecl(VD, TemplateArgs));
|
/*FIXME:*/false, /*FIXME:*/false);
|
||||||
if (!VD)
|
}
|
||||||
return SemaRef.ExprError();
|
|
||||||
|
|
||||||
return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
|
assert(Arg.getKind() == TemplateArgument::Integral);
|
||||||
/*FIXME:*/false, /*FIXME:*/false);
|
QualType T = Arg.getIntegralType();
|
||||||
}
|
if (T->isCharType() || T->isWideCharType())
|
||||||
|
return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
|
||||||
assert(Arg.getKind() == TemplateArgument::Integral);
|
Arg.getAsIntegral()->getZExtValue(),
|
||||||
QualType T = Arg.getIntegralType();
|
T->isWideCharType(),
|
||||||
if (T->isCharType() || T->isWideCharType())
|
|
||||||
return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
|
|
||||||
Arg.getAsIntegral()->getZExtValue(),
|
|
||||||
T->isWideCharType(),
|
|
||||||
T,
|
|
||||||
E->getSourceRange().getBegin()));
|
|
||||||
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,
|
T,
|
||||||
E->getSourceRange().getBegin()));
|
E->getSourceRange().getBegin()));
|
||||||
|
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.
|
||||||
}
|
}
|
||||||
|
|
||||||
NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
|
NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
|
||||||
|
|
|
@ -409,6 +409,9 @@ namespace {
|
||||||
}
|
}
|
||||||
|
|
||||||
Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
|
Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
|
||||||
|
// Create a local instantiation scope for this class template, which
|
||||||
|
// will contain the instantiations of the template parameters.
|
||||||
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
||||||
TemplateParameterList *TempParams = D->getTemplateParameters();
|
TemplateParameterList *TempParams = D->getTemplateParameters();
|
||||||
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
||||||
if (!InstParams)
|
if (!InstParams)
|
||||||
|
@ -491,8 +494,12 @@ TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
|
||||||
|
|
||||||
Decl *
|
Decl *
|
||||||
TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
|
TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
|
||||||
// FIXME: Dig out the out-of-line definition of this function template?
|
// Create a local instantiation scope for this function template, which
|
||||||
|
// will contain the instantiations of the template parameters and then get
|
||||||
|
// merged with the local instantiation scope for the function template
|
||||||
|
// itself.
|
||||||
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
||||||
|
|
||||||
TemplateParameterList *TempParams = D->getTemplateParameters();
|
TemplateParameterList *TempParams = D->getTemplateParameters();
|
||||||
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
|
||||||
if (!InstParams)
|
if (!InstParams)
|
||||||
|
@ -582,7 +589,7 @@ Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
|
||||||
return Info->Function;
|
return Info->Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sema::LocalInstantiationScope Scope(SemaRef);
|
Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
|
||||||
|
|
||||||
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
||||||
QualType T = SubstFunctionType(D, Params);
|
QualType T = SubstFunctionType(D, Params);
|
||||||
|
@ -711,7 +718,7 @@ TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
|
||||||
return Info->Function;
|
return Info->Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sema::LocalInstantiationScope Scope(SemaRef);
|
Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
|
||||||
|
|
||||||
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
llvm::SmallVector<ParmVarDecl *, 4> Params;
|
||||||
QualType T = SubstFunctionType(D, Params);
|
QualType T = SubstFunctionType(D, Params);
|
||||||
|
@ -886,7 +893,7 @@ Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
|
||||||
|
|
||||||
TemplateTypeParmDecl *Inst =
|
TemplateTypeParmDecl *Inst =
|
||||||
TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
|
||||||
TTPT->getDepth(), TTPT->getIndex(),
|
TTPT->getDepth() - 1, TTPT->getIndex(),
|
||||||
TTPT->getName(),
|
TTPT->getName(),
|
||||||
D->wasDeclaredWithTypename(),
|
D->wasDeclaredWithTypename(),
|
||||||
D->isParameterPack());
|
D->isParameterPack());
|
||||||
|
@ -904,6 +911,10 @@ Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
|
||||||
D->defaultArgumentWasInherited() /* preserve? */);
|
D->defaultArgumentWasInherited() /* preserve? */);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Introduce this template parameter's instantiation into the instantiation
|
||||||
|
// scope.
|
||||||
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
|
||||||
|
|
||||||
return Inst;
|
return Inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -940,6 +951,10 @@ Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
|
||||||
Param->setInvalidDecl();
|
Param->setInvalidDecl();
|
||||||
|
|
||||||
Param->setDefaultArgument(D->getDefaultArgument());
|
Param->setDefaultArgument(D->getDefaultArgument());
|
||||||
|
|
||||||
|
// Introduce this template parameter's instantiation into the instantiation
|
||||||
|
// scope.
|
||||||
|
SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
|
||||||
return Param;
|
return Param;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1024,6 +1039,11 @@ bool
|
||||||
TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
|
TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
|
||||||
ClassTemplateDecl *ClassTemplate,
|
ClassTemplateDecl *ClassTemplate,
|
||||||
ClassTemplatePartialSpecializationDecl *PartialSpec) {
|
ClassTemplatePartialSpecializationDecl *PartialSpec) {
|
||||||
|
// Create a local instantiation scope for this class template partial
|
||||||
|
// specialization, which will contain the instantiations of the template
|
||||||
|
// parameters.
|
||||||
|
Sema::LocalInstantiationScope Scope(SemaRef);
|
||||||
|
|
||||||
// Substitute into the template parameters of the class template partial
|
// Substitute into the template parameters of the class template partial
|
||||||
// specialization.
|
// specialization.
|
||||||
TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
|
TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
|
||||||
|
@ -1773,7 +1793,9 @@ NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
|
||||||
}
|
}
|
||||||
|
|
||||||
DeclContext *ParentDC = D->getDeclContext();
|
DeclContext *ParentDC = D->getDeclContext();
|
||||||
if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
|
if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
|
||||||
|
isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
|
||||||
|
ParentDC->isFunctionOrMethod()) {
|
||||||
// D is a local of some kind. Look into the map of local
|
// D is a local of some kind. Look into the map of local
|
||||||
// declarations to their instantiations.
|
// declarations to their instantiations.
|
||||||
return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
|
return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
// RUN: clang-cc -fsyntax-only -verify %s
|
||||||
|
|
||||||
|
// PR5311
|
||||||
|
template<typename T>
|
||||||
|
class StringSwitch {
|
||||||
|
public:
|
||||||
|
template<unsigned N>
|
||||||
|
void Case(const char (&S)[N], const int & Value) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
(void)StringSwitch<int>();
|
||||||
|
}
|
|
@ -75,3 +75,21 @@ void test_X1(X1 x1) {
|
||||||
float* (*fp7)(int) = X1::f2<>;
|
float* (*fp7)(int) = X1::f2<>;
|
||||||
float* (*fp8)(float) = X1::f2<float>;
|
float* (*fp8)(float) = X1::f2<float>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<int A> struct X2 {
|
||||||
|
int m;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct X3 : T { };
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct X4 {
|
||||||
|
template<typename U>
|
||||||
|
void f(X2<sizeof(X3<U>().U::m)>);
|
||||||
|
};
|
||||||
|
|
||||||
|
void f(X4<X3<int> > x4i) {
|
||||||
|
X2<sizeof(int)> x2;
|
||||||
|
x4i.f<X2<sizeof(int)> >(x2);
|
||||||
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче