Douglas Gregor dgregor at apple.com
Fri Jan 14 13:20:45 PST 2011

```Author: dgregor
Date: Fri Jan 14 15:20:45 2011
New Revision: 123470

URL: http://llvm.org/viewvc/llvm-project?rev=123470&view=rev
Log:
Teach PackExpansionExpr to keep track of the number of pack expansions
it will expand to, if known. Propagate this information throughout Sema.

Modified:
cfe/trunk/include/clang/AST/ExprCXX.h
cfe/trunk/include/clang/Sema/Sema.h
cfe/trunk/lib/AST/DeclTemplate.cpp
cfe/trunk/lib/AST/TemplateBase.cpp
cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
cfe/trunk/lib/Sema/TreeTransform.h
cfe/trunk/lib/Serialization/ASTWriterStmt.cpp

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=123470&r1=123469&r2=123470&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Fri Jan 14 15:20:45 2011
@@ -2612,16 +2612,27 @@
/// 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 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 @@
/// 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) {

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=123470&r1=123469&r2=123470&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Fri Jan 14 15:20:45 2011
@@ -3348,7 +3348,17 @@
///
/// \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.

Modified: cfe/trunk/lib/AST/DeclTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclTemplate.cpp?rev=123470&r1=123469&r2=123470&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclTemplate.cpp (original)
+++ cfe/trunk/lib/AST/DeclTemplate.cpp Fri Jan 14 15:20:45 2011
@@ -332,7 +332,8 @@

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);

Modified: cfe/trunk/lib/AST/TemplateBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TemplateBase.cpp?rev=123470&r1=123469&r2=123470&view=diff
==============================================================================
--- cfe/trunk/lib/AST/TemplateBase.cpp (original)
+++ cfe/trunk/lib/AST/TemplateBase.cpp Fri Jan 14 15:20:45 2011
@@ -384,7 +384,7 @@
= cast<PackExpansionExpr>(Argument.getAsExpr());
Expr *Pattern = Expansion->getPattern();
Ellipsis = Expansion->getEllipsisLoc();
-    // FIXME: Variadic templates num expansions
+    NumExpansions = Expansion->getNumExpansions();
return TemplateArgumentLoc(Pattern, Pattern);
}

Modified: cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp?rev=123470&r1=123469&r2=123470&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp Fri Jan 14 15:20:45 2011
@@ -1463,9 +1463,6 @@
unsigned ArgIdx = 0, ParamIdx = 0;
for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
++ParamIdx) {
-    // 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.

==============================================================================
+++ cfe/trunk/lib/Sema/SemaTemplateVariadic.cpp Fri Jan 14 15:20:45 2011
@@ -415,6 +415,11 @@
}

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 @@

// 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 @@
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;

Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=123470&r1=123469&r2=123470&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Fri Jan 14 15:20:45 2011
@@ -2176,8 +2176,8 @@
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 @@
/// 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 @@
// 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 @@
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 @@
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 @@
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>

==============================================================================
+++ cfe/trunk/lib/Serialization/ASTReaderStmt.cpp Fri Jan 14 15:20:45 2011
@@ -1294,6 +1294,7 @@
VisitExpr(E);
+  E->NumExpansions = Record[Idx++];
}

Modified: cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriterStmt.cpp?rev=123470&r1=123469&r2=123470&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterStmt.cpp Fri Jan 14 15:20:45 2011
@@ -1301,6 +1301,7 @@
void ASTStmtWriter::VisitPackExpansionExpr(PackExpansionExpr *E) {
VisitExpr(E);
+  Record.push_back(E->NumExpansions);
Code = serialization::EXPR_PACK_EXPANSION;
}

==============================================================================
+++ cfe/trunk/test/CXX/temp/temp.decls/temp.variadic/multi-level-substitution.cpp Fri Jan 14 15:20:45 2011
@@ -37,4 +37,33 @@
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];
}

```