/// 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) { }
/// 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) {
///
/// \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.
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);
= cast<PackExpansionExpr>(Argument.getAsExpr());
Expr *Pattern = Expansion->getPattern();
Ellipsis = Expansion->getEllipsisLoc();
- // FIXME: Variadic templates num expansions
+ NumExpansions = Expansion->getNumExpansions();
return TemplateArgumentLoc(Pattern, Pattern);
}
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.
}
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();
// 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.
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;
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();
/// 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:
// 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(),
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;
return true;
if (Out.get()->containsUnexpandedParameterPack()) {
- Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc());
+ Out = RebuildPackExpansion(Out.get(), Expansion->getEllipsisLoc(),
+ OrigNumExpansions);
if (Out.isInvalid())
return true;
}
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>
void ASTStmtReader::VisitPackExpansionExpr(PackExpansionExpr *E) {
VisitExpr(E);
E->EllipsisLoc = ReadSourceLocation(Record, Idx);
+ E->NumExpansions = Record[Idx++];
E->Pattern = Reader.ReadSubExpr();
}
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;
}
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];
}