Teach PackExpansionExpr to keep track of the number of pack expansions

it will expand to, if known. Propagate this information throughout Sema.

llvm-svn: 123470
This commit is contained in:
Douglas Gregor 2011-01-14 21:20:45 +00:00
parent fa38041dac
commit b884000ba9
10 changed files with 86 additions and 21 deletions

View File

@ -2612,16 +2612,27 @@ public:
/// or more function arguments to the function object \c f.
class PackExpansionExpr : public Expr {
SourceLocation EllipsisLoc;
/// \brief The number of expansions that will be produced by this pack
/// expansion expression, if known.
///
/// When zero, the number of expansions is not known. Otherwise, this value
/// is the number of expansions + 1.
unsigned NumExpansions;
Stmt *Pattern;
friend class ASTStmtReader;
friend class ASTStmtWriter;
public:
PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc)
PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc,
llvm::Optional<unsigned> NumExpansions)
: Expr(PackExpansionExprClass, T, Pattern->getValueKind(),
Pattern->getObjectKind(), /*TypeDependent=*/true,
/*ValueDependent=*/true, /*ContainsUnexpandedParameterPack=*/false),
EllipsisLoc(EllipsisLoc),
NumExpansions(NumExpansions? *NumExpansions + 1 : 0),
Pattern(Pattern) { }
PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) { }
@ -2636,6 +2647,15 @@ public:
/// expansion.
SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
/// \brief Determine the number of expansions that will be produced when
/// this pack expansion is instantiated, if already known.
llvm::Optional<unsigned> getNumExpansions() const {
if (NumExpansions)
return NumExpansions - 1;
return llvm::Optional<unsigned>();
}
virtual SourceRange getSourceRange() const;
static bool classof(const Stmt *T) {

View File

@ -3348,7 +3348,17 @@ public:
///
/// \param EllipsisLoc The location of the ellipsis.
ExprResult ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc);
/// \brief Invoked when parsing an expression followed by an ellipsis, which
/// creates a pack expansion.
///
/// \param Pattern The expression preceding the ellipsis, which will become
/// the pattern of the pack expansion.
///
/// \param EllipsisLoc The location of the ellipsis.
ExprResult CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
llvm::Optional<unsigned> NumExpansions);
/// \brief Determine whether we could expand a pack expansion with the
/// given set of parameter packs into separate arguments by repeatedly
/// transforming the pattern.

View File

@ -332,7 +332,8 @@ ClassTemplateDecl::getInjectedClassNameSpecialization() {
if (NTTP->isParameterPack())
E = new (Context) PackExpansionExpr(Context.DependentTy, E,
NTTP->getLocation());
NTTP->getLocation(),
llvm::Optional<unsigned>());
Arg = TemplateArgument(E);
} else {
TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);

View File

@ -384,7 +384,7 @@ TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis,
= cast<PackExpansionExpr>(Argument.getAsExpr());
Expr *Pattern = Expansion->getPattern();
Ellipsis = Expansion->getEllipsisLoc();
// FIXME: Variadic templates num expansions
NumExpansions = Expansion->getNumExpansions();
return TemplateArgumentLoc(Pattern, Pattern);
}

View File

@ -1463,9 +1463,6 @@ DeduceTemplateArguments(Sema &S,
unsigned ArgIdx = 0, ParamIdx = 0;
for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
++ParamIdx) {
// FIXME: Variadic templates.
// What do we do if the argument is a pack expansion?
if (!Params[ParamIdx].isPackExpansion()) {
// The simple case: deduce template arguments by matching Pi and Ai.

View File

@ -415,6 +415,11 @@ QualType Sema::CheckPackExpansion(QualType Pattern,
}
ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>());
}
ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
llvm::Optional<unsigned> NumExpansions) {
if (!Pattern)
return ExprError();
@ -430,7 +435,7 @@ ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
// Create the pack expansion expression and source-location information.
return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
EllipsisLoc));
EllipsisLoc, NumExpansions));
}
/// \brief Retrieve the depth and index of a parameter pack.
@ -459,10 +464,6 @@ bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,
std::pair<IdentifierInfo *, SourceLocation> FirstPack;
bool HaveFirstPack = false;
// FIXME: Variadic templates. Even if we don't expand, we'd still like to
// return the number of expansions back to the caller, perhaps as an
// llvm::Optional, so that it can be embedded in the pack expansion. This
// is important for the multi-level substitution case.
for (unsigned I = 0; I != NumUnexpanded; ++I) {
// Compute the depth and index for this parameter pack.
unsigned Depth;

View File

@ -2176,8 +2176,8 @@ public:
switch (Pattern.getArgument().getKind()) {
case TemplateArgument::Expression: {
ExprResult Result
= getSema().ActOnPackExpansion(Pattern.getSourceExpression(),
EllipsisLoc);
= getSema().CheckPackExpansion(Pattern.getSourceExpression(),
EllipsisLoc, NumExpansions);
if (Result.isInvalid())
return TemplateArgumentLoc();
@ -2217,8 +2217,9 @@ public:
/// By default, performs semantic analysis to build a new pack expansion
/// for an expression. Subclasses may override this routine to provide
/// different behavior.
ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
return getSema().ActOnPackExpansion(Pattern, EllipsisLoc);
ExprResult RebuildPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
llvm::Optional<unsigned> NumExpansions) {
return getSema().CheckPackExpansion(Pattern, EllipsisLoc, NumExpansions);
}
private:
@ -2308,7 +2309,9 @@ bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
// be expanded.
bool Expand = true;
bool RetainExpansion = false;
llvm::Optional<unsigned> NumExpansions;
llvm::Optional<unsigned> OrigNumExpansions
= Expansion->getNumExpansions();
llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
if (getDerived().TryExpandParameterPacks(Expansion->getEllipsisLoc(),
Pattern->getSourceRange(),
Unexpanded.data(),
@ -2326,9 +2329,9 @@ bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
if (OutPattern.isInvalid())
return true;
// FIXME: Variadic templates NumExpansions
ExprResult Out = getDerived().RebuildPackExpansion(OutPattern.get(),
Expansion->getEllipsisLoc());
Expansion->getEllipsisLoc(),
NumExpansions);
if (Out.isInvalid())
return true;
@ -2347,7 +2350,8 @@ bool TreeTransform<Derived>::TransformExprs(Expr **Inputs,
return true;
if (Out.get()->containsUnexpandedParameterPack()) {
Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc());
Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc(),
OrigNumExpansions);
if (Out.isInvalid())
return true;
}
@ -6818,7 +6822,8 @@ TreeTransform<Derived>::TransformPackExpansionExpr(PackExpansionExpr *E) {
if (!getDerived().AlwaysRebuild() && Pattern.get() == E->getPattern())
return SemaRef.Owned(E);
return getDerived().RebuildPackExpansion(Pattern.get(), E->getEllipsisLoc());
return getDerived().RebuildPackExpansion(Pattern.get(), E->getEllipsisLoc(),
E->getNumExpansions());
}
template<typename Derived>

View File

@ -1294,6 +1294,7 @@ void ASTStmtReader::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
void ASTStmtReader::VisitPackExpansionExpr(PackExpansionExpr *E) {
VisitExpr(E);
E->EllipsisLoc = ReadSourceLocation(Record, Idx);
E->NumExpansions = Record[Idx++];
E->Pattern = Reader.ReadSubExpr();
}

View File

@ -1301,6 +1301,7 @@ void ASTStmtWriter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
void ASTStmtWriter::VisitPackExpansionExpr(PackExpansionExpr *E) {
VisitExpr(E);
Writer.AddSourceLocation(E->getEllipsisLoc(), Record);
Record.push_back(E->NumExpansions);
Writer.AddStmt(E->getPattern());
Code = serialization::EXPR_PACK_EXPANSION;
}

View File

@ -37,4 +37,33 @@ namespace PacksAtDifferentLevels {
pair<int, unsigned int>,
pair<long, unsigned long>>
>::value == 1? 1 : -1];
template<unsigned ...Values> struct unsigned_tuple { };
template<typename ...Types>
struct X1 {
template<typename, typename> struct Inner {
static const unsigned value = 0;
};
template<typename ...YTypes>
struct Inner<tuple<pair<Types, YTypes>...>,
unsigned_tuple<sizeof(Types) + sizeof(YTypes)...>> {
static const unsigned value = 1;
};
};
int check2[X1<short, int, long>::Inner<tuple<pair<short, unsigned short>,
pair<int, unsigned int>,
pair<long, unsigned long>>,
unsigned_tuple<sizeof(short) + sizeof(unsigned short),
sizeof(int) + sizeof(unsigned int),
sizeof(long) + sizeof(unsigned long)>
>::value == 1? 1 : -1];
int check3[X1<short, int>::Inner<tuple<pair<short, unsigned short>,
pair<int, unsigned int>,
pair<long, unsigned long>>,
unsigned_tuple<sizeof(short) + sizeof(unsigned short),
sizeof(int) + sizeof(unsigned int),
sizeof(long) + sizeof(unsigned long)>
>::value == 0? 1 : -1];
}