aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Sema')
-rw-r--r--lib/Sema/SemaTemplateInstantiate.cpp82
-rw-r--r--lib/Sema/SemaTemplateInstantiateDecl.cpp55
-rw-r--r--lib/Sema/SemaTemplateVariadic.cpp80
-rw-r--r--lib/Sema/TreeTransform.h5
4 files changed, 145 insertions, 77 deletions
diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index 95ccae28f5..37d8d8cb53 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -595,7 +595,14 @@ namespace {
const UnexpandedParameterPack *Unexpanded,
unsigned NumUnexpanded,
bool &ShouldExpand,
- unsigned &NumExpansions);
+ unsigned &NumExpansions) {
+ return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
+ PatternRange, Unexpanded,
+ NumUnexpanded,
+ TemplateArgs,
+ ShouldExpand,
+ NumExpansions);
+ }
/// \brief Transform the given declaration by instantiating a reference to
/// this declaration.
@@ -663,79 +670,6 @@ bool TemplateInstantiator::AlreadyTransformed(QualType T) {
return true;
}
-bool TemplateInstantiator::TryExpandParameterPacks(SourceLocation EllipsisLoc,
- SourceRange PatternRange,
- const UnexpandedParameterPack *Unexpanded,
- unsigned NumUnexpanded,
- bool &ShouldExpand,
- unsigned &NumExpansions) {
- ShouldExpand = true;
- std::pair<IdentifierInfo *, SourceLocation> FirstPack;
- bool HaveFirstPack = false;
-
- for (unsigned I = 0; I != NumUnexpanded; ++I) {
- // Compute the depth and index for this parameter pack.
- unsigned Depth;
- unsigned Index;
- IdentifierInfo *Name;
-
- if (const TemplateTypeParmType *TTP
- = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
- Depth = TTP->getDepth();
- Index = TTP->getIndex();
- Name = TTP->getName();
- } else {
- NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
- if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) {
- Depth = TTP->getDepth();
- Index = TTP->getIndex();
- } else if (NonTypeTemplateParmDecl *NTTP
- = dyn_cast<NonTypeTemplateParmDecl>(ND)) {
- Depth = NTTP->getDepth();
- Index = NTTP->getIndex();
- } else {
- TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
- Depth = TTP->getDepth();
- Index = TTP->getIndex();
- }
- // FIXME: Variadic templates function parameter packs?
- Name = ND->getIdentifier();
- }
-
- // If we don't have a template argument at this depth/index, then we
- // cannot expand the pack expansion. Make a note of this, but we still
- // want to check that any parameter packs we *do* have arguments for.
- if (!TemplateArgs.hasTemplateArgument(Depth, Index)) {
- ShouldExpand = false;
- continue;
- }
-
- // Determine the size of the argument pack.
- unsigned NewPackSize = TemplateArgs(Depth, Index).pack_size();
- if (!HaveFirstPack) {
- // The is the first pack we've seen for which we have an argument.
- // Record it.
- NumExpansions = NewPackSize;
- FirstPack.first = Name;
- FirstPack.second = Unexpanded[I].second;
- HaveFirstPack = true;
- continue;
- }
-
- if (NewPackSize != NumExpansions) {
- // C++0x [temp.variadic]p5:
- // All of the parameter packs expanded by a pack expansion shall have
- // the same number of arguments specified.
- getSema().Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
- << FirstPack.first << Name << NumExpansions << NewPackSize
- << SourceRange(FirstPack.second) << SourceRange(Unexpanded[I].second);
- return true;
- }
- }
-
- return false;
-}
-
Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
if (!D)
return 0;
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index b6ef498874..115cdc4545 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1983,6 +1983,61 @@ TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
llvm::SmallVector<QualType, 4> Exceptions;
for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
// FIXME: Poor location information!
+ if (const PackExpansionType *PackExpansion
+ = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
+ // We have a pack expansion. Instantiate it.
+ llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
+ SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
+ Unexpanded);
+ assert(!Unexpanded.empty() &&
+ "Pack expansion without parameter packs?");
+
+ bool Expand = false;
+ unsigned NumExpansions = 0;
+ if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
+ SourceRange(),
+ Unexpanded.data(),
+ Unexpanded.size(),
+ TemplateArgs,
+ Expand, NumExpansions))
+ break;
+
+ if (!Expand) {
+ // We can't expand this pack expansion into separate arguments yet;
+ // just substitute into the argument pack.
+ Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
+ QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
+ TemplateArgs,
+ New->getLocation(), New->getDeclName());
+ if (T.isNull())
+ break;
+
+ Exceptions.push_back(T);
+ continue;
+ }
+
+ // Substitute into the pack expansion pattern for each template
+ bool Invalid = false;
+ for (unsigned ArgIdx = 0; ArgIdx != NumExpansions; ++ArgIdx) {
+ Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
+
+ QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
+ TemplateArgs,
+ New->getLocation(), New->getDeclName());
+ if (T.isNull()) {
+ Invalid = true;
+ break;
+ }
+
+ Exceptions.push_back(T);
+ }
+
+ if (Invalid)
+ break;
+
+ continue;
+ }
+
QualType T
= SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
New->getLocation(), New->getDeclName());
diff --git a/lib/Sema/SemaTemplateVariadic.cpp b/lib/Sema/SemaTemplateVariadic.cpp
index 681bc8d072..f2651ef65c 100644
--- a/lib/Sema/SemaTemplateVariadic.cpp
+++ b/lib/Sema/SemaTemplateVariadic.cpp
@@ -259,6 +259,11 @@ void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
.TraverseTemplateArgumentLoc(Arg);
}
+void Sema::collectUnexpandedParameterPacks(QualType T,
+ llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
+ CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
+}
+
ParsedTemplateArgument
Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
SourceLocation EllipsisLoc) {
@@ -327,3 +332,78 @@ TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern,
Pattern->getTypeLoc().getFullDataSize());
return TSResult;
}
+
+
+bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,
+ SourceRange PatternRange,
+ const UnexpandedParameterPack *Unexpanded,
+ unsigned NumUnexpanded,
+ const MultiLevelTemplateArgumentList &TemplateArgs,
+ bool &ShouldExpand,
+ unsigned &NumExpansions) {
+ ShouldExpand = true;
+ std::pair<IdentifierInfo *, SourceLocation> FirstPack;
+ bool HaveFirstPack = false;
+
+ for (unsigned I = 0; I != NumUnexpanded; ++I) {
+ // Compute the depth and index for this parameter pack.
+ unsigned Depth;
+ unsigned Index;
+ IdentifierInfo *Name;
+
+ if (const TemplateTypeParmType *TTP
+ = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
+ Depth = TTP->getDepth();
+ Index = TTP->getIndex();
+ Name = TTP->getName();
+ } else {
+ NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
+ if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) {
+ Depth = TTP->getDepth();
+ Index = TTP->getIndex();
+ } else if (NonTypeTemplateParmDecl *NTTP
+ = dyn_cast<NonTypeTemplateParmDecl>(ND)) {
+ Depth = NTTP->getDepth();
+ Index = NTTP->getIndex();
+ } else {
+ TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
+ Depth = TTP->getDepth();
+ Index = TTP->getIndex();
+ }
+ // FIXME: Variadic templates function parameter packs?
+ Name = ND->getIdentifier();
+ }
+
+ // If we don't have a template argument at this depth/index, then we
+ // cannot expand the pack expansion. Make a note of this, but we still
+ // want to check that any parameter packs we *do* have arguments for.
+ if (!TemplateArgs.hasTemplateArgument(Depth, Index)) {
+ ShouldExpand = false;
+ continue;
+ }
+
+ // Determine the size of the argument pack.
+ unsigned NewPackSize = TemplateArgs(Depth, Index).pack_size();
+ if (!HaveFirstPack) {
+ // The is the first pack we've seen for which we have an argument.
+ // Record it.
+ NumExpansions = NewPackSize;
+ FirstPack.first = Name;
+ FirstPack.second = Unexpanded[I].second;
+ HaveFirstPack = true;
+ continue;
+ }
+
+ if (NewPackSize != NumExpansions) {
+ // C++0x [temp.variadic]p5:
+ // All of the parameter packs expanded by a pack expansion shall have
+ // the same number of arguments specified.
+ Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
+ << FirstPack.first << Name << NumExpansions << NewPackSize
+ << SourceRange(FirstPack.second) << SourceRange(Unexpanded[I].second);
+ return true;
+ }
+ }
+
+ return false;
+}
diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index ae55d32fb4..728ece3601 100644
--- a/lib/Sema/TreeTransform.h
+++ b/lib/Sema/TreeTransform.h
@@ -91,11 +91,10 @@ template<typename Derived>
class TreeTransform {
protected:
Sema &SemaRef;
- Sema::ArgumentPackSubstitutionIndexRAII SubstIndex;
public:
/// \brief Initializes a new tree transformer.
- TreeTransform(Sema &SemaRef) : SemaRef(SemaRef), SubstIndex(SemaRef, -1) { }
+ TreeTransform(Sema &SemaRef) : SemaRef(SemaRef) { }
/// \brief Retrieves a reference to the derived class.
Derived &getDerived() { return static_cast<Derived&>(*this); }
@@ -186,7 +185,7 @@ public:
/// given set of parameter packs into separate arguments by repeatedly
/// transforming the pattern.
///
- /// By default, the transformed never tries to expand pack expansions.
+ /// By default, the transformer never tries to expand pack expansions.
/// Subclasses can override this routine to provide different behavior.
///
/// \param EllipsisLoc The location of the ellipsis that identifies the