[c++1z] N4295: fold-expressions.

This is a new form of expression of the form:

  (expr op ... op expr)

where one of the exprs is a parameter pack. It expands into

  (expr1 op (expr2onwards op ... op expr))

(and likewise if the pack is on the right). The non-pack operand can be
omitted; in that case, an empty pack gives a fallback value or an error,
depending on the operator.

llvm-svn: 221573
diff --git a/clang/test/CodeGenCXX/cxx1z-fold-expression.cpp b/clang/test/CodeGenCXX/cxx1z-fold-expression.cpp
new file mode 100644
index 0000000..7677fc0
--- /dev/null
+++ b/clang/test/CodeGenCXX/cxx1z-fold-expression.cpp
@@ -0,0 +1,45 @@
+// RUN: %clang_cc1 -std=c++1z -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s
+
+template<int> struct A {};
+template<int ...N> void foldr(A<(N + ...)>);
+template<int ...N> void foldl(A<(... + N)>);
+template<int ...N> void foldr1(A<(N + ... + 1)>);
+template<int ...N> void foldl1(A<(1 + ... + N)>);
+void use() {
+  foldr<1, 2, 3>({});
+  foldl<1, 2, 3>({});
+  foldr1<1, 2, 3>({});
+  foldl1<1, 2, 3>({});
+  // CHECK-DAG: @_Z5foldrIJLi1ELi2ELi3EEEv1AIXfrplT_EE(
+  // CHECK-DAG: @_Z5foldlIJLi1ELi2ELi3EEEv1AIXflplT_EE(
+  // CHECK-DAG: @_Z6foldr1IJLi1ELi2ELi3EEEv1AIXfxplT_Li1EEE(
+  // CHECK-DAG: @_Z6foldl1IJLi1ELi2ELi3EEEv1AIXfxplLi1ET_EE(
+}
+
+template<int ...N> using Foldr = A<(N + ...)>;
+template<int ...N> using Foldl = A<(... + N)>;
+template<int ...N> using Foldr1 = A<(N + ... + 1)>;
+template<int ...N> using Foldl1 = A<(1 + ... + N)>;
+
+template<int ...A> struct Partial {
+  template<int ...B> void foldr(Foldr<A..., B..., A..., B...>);
+  template<int ...B> void foldl(Foldr<A..., B..., A..., B...>);
+  template<int ...B> void foldr1(Foldr1<A..., B..., A..., B...>);
+  template<int ...B> void foldl1(Foldl1<A..., B..., A..., B...>);
+};
+void use(Partial<1, 2> p) {
+  p.foldr<3, 4>({});
+  p.foldl<3, 4>({});
+  p.foldr1<3, 4>({});
+  p.foldl1<3, 4>({});
+  // CHECK-DAG: @_ZN7PartialIJLi1ELi2EEE5foldrIJLi3ELi4EEEEv1AIXplLi1EplLi2EfxplT_plLi1EplLi2EfrplT_EE(
+  // CHECK-DAG: @_ZN7PartialIJLi1ELi2EEE5foldlIJLi3ELi4EEEEv1AIXplLi1EplLi2EfxplT_plLi1EplLi2EfrplT_EE(
+  // CHECK-DAG: @_ZN7PartialIJLi1ELi2EEE6foldr1IJLi3ELi4EEEEv1AIXplLi1EplLi2EfxplT_plLi1EplLi2EfxplT_Li1EEE(
+  // CHECK-DAG: @_ZN7PartialIJLi1ELi2EEE6foldl1IJLi3ELi4EEEEv1AIXfxplplplfxplplplLi1ELi1ELi2ET_Li1ELi2ET_EE(
+}
+
+extern int n;
+template<int ...N> void f() {
+  (n = ... = N);
+}
+template void f<>();
diff --git a/clang/test/Parser/cxx1z-fold-expressions.cpp b/clang/test/Parser/cxx1z-fold-expressions.cpp
new file mode 100644
index 0000000..a908311
--- /dev/null
+++ b/clang/test/Parser/cxx1z-fold-expressions.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -std=c++1z -verify %s
+
+template<typename ...T> constexpr auto sum(T ...t) { return (... + t); }
+template<typename ...T> constexpr auto product(T ...t) { return (t * ...); }
+template<typename ...T> constexpr auto all(T ...t) { return (true && ... && t); }
+template<typename ...T> constexpr auto all2(T ...t) { return (t && ... && true); }
+
+int k1 = (1 + ... + 2); // expected-error {{does not contain any unexpanded parameter packs}}
+int k2 = (1 + ...); // expected-error {{does not contain any unexpanded parameter packs}}
+int k3 = (... + 2); // expected-error {{does not contain any unexpanded parameter packs}}
+
+template<int ...N> void bad1() { (N + ... + N); } // expected-error {{unexpanded parameter packs in both operands}}
+// FIXME: it would be reasonable to support this as an extension.
+template<int ...N> void bad2() { (2 * N + ... + 1); } // expected-error {{expression not permitted as operand}}
+template<int ...N> void bad3() { (2 + N * ... * 1); } // expected-error {{expression not permitted as operand}}
+template<int ...N, int ...M> void bad4(int (&...x)[N]) { (N + M * ... * 1); } // expected-error {{expression not permitted as operand}}
+template<int ...N, int ...M> void fixed4(int (&...x)[N]) { ((N + M) * ... * 1); }
+
+// Parens are mandatory.
+template<int ...N> void bad5() { N + ...; } // expected-error {{expected expression}} expected-error +{{}}
+template<int ...N> void bad6() { ... + N; } // expected-error {{expected expression}}
+template<int ...N> void bad7() { N + ... + N; } // expected-error {{expected expression}} expected-error +{{}}
+
+// Must have a fold-operator in the relevant places.
+template<int ...N> int bad8() { return (N + ... * 3); } // expected-error {{operators in fold expression must be the same}}
+template<int ...N> int bad9() { return (3 + ... * N); } // expected-error {{operators in fold expression must be the same}}
+template<int ...N> int bad10() { return (3 ? ... : N); } // expected-error +{{}} expected-note {{to match}}
+template<int ...N> int bad11() { return (N + ... 0); } // expected-error {{expected a foldable binary operator}} expected-error {{expected expression}}
+template<int ...N> int bad12() { return (... N); } // expected-error {{expected expression}}
diff --git a/clang/test/SemaCXX/cxx0x-compat.cpp b/clang/test/SemaCXX/cxx0x-compat.cpp
index ded51ab..bcf0cf1 100644
--- a/clang/test/SemaCXX/cxx0x-compat.cpp
+++ b/clang/test/SemaCXX/cxx0x-compat.cpp
@@ -41,9 +41,15 @@
 #define _x + 1
 char c = 'x'_x; // expected-warning {{will be treated as a user-defined literal suffix}}
 
+template<int ...N> int f() { // expected-warning {{C++11 extension}}
+  return (N + ...); // expected-warning {{C++1z extension}}
+}
+
 #else
 
 auto init_capture = [a(0)] {}; // expected-warning {{initialized lambda captures are incompatible with C++ standards before C++14}}
 static_assert(true); // expected-warning {{incompatible with C++ standards before C++1z}}
 
+template<int ...N> int f() { return (N + ...); } // expected-warning {{incompatible with C++ standards before C++1z}}
+
 #endif
diff --git a/clang/test/SemaTemplate/cxx1z-fold-expressions.cpp b/clang/test/SemaTemplate/cxx1z-fold-expressions.cpp
new file mode 100644
index 0000000..3934f01
--- /dev/null
+++ b/clang/test/SemaTemplate/cxx1z-fold-expressions.cpp
@@ -0,0 +1,78 @@
+// RUN: %clang_cc1 -std=c++1z -verify %s
+
+template<typename ...T> constexpr auto sum(T ...t) { return (... + t); }
+template<typename ...T> constexpr auto product(T ...t) { return (t * ...); }
+template<typename ...T> constexpr auto all(T ...t) { return (true && ... && t); }
+template<typename ...T> constexpr auto dumb(T ...t) { return (false && ... && t); }
+
+static_assert(sum(1, 2, 3, 4, 5) == 15);
+static_assert(product(1, 2, 3, 4, 5) == 120);
+static_assert(!all(true, true, false, true, false));
+static_assert(all(true, true, true, true, true));
+static_assert(!dumb(true, true, true, true, true));
+
+struct S {
+  int a, b, c, d, e;
+};
+template<typename ...T> constexpr auto increment_all(T &...t) {
+  (++t, ...);
+}
+constexpr bool check() {
+  S s = { 1, 2, 3, 4, 5 };
+  increment_all(s.a, s.b, s.c, s.d, s.e);
+  return s.a == 2 && s.b == 3 && s.c == 4 && s.d == 5 && s.e == 6;
+}
+static_assert(check());
+
+template<int ...N> void empty() {
+  static_assert((N + ...) == 0);
+  static_assert((N * ...) == 1);
+  static_assert((N | ...) == 0);
+  static_assert((N & ...) == -1);
+  static_assert((N || ...) == false);
+  static_assert((N && ...) == true);
+  (N, ...);
+}
+template void empty<>();
+
+// An empty fold-expression isn't a null pointer just because it's an integer
+// with value 0.
+template<int ...N> void null_ptr() {
+  void *p = (N + ...); // expected-error {{rvalue of type 'int'}}
+  void *q = (N | ...); // expected-error {{rvalue of type 'int'}}
+}
+template void null_ptr<>(); // expected-note {{in instantiation of}}
+
+template<int ...N> void bad_empty() {
+  (N - ...); // expected-error {{empty expansion for operator '-' with no fallback}}
+  (N / ...); // expected-error {{empty expansion for operator '/' with no fallback}}
+  (N % ...); // expected-error {{empty expansion for operator '%' with no fallback}}
+  (N = ...); // expected-error {{empty expansion for operator '=' with no fallback}}
+}
+template void bad_empty<>(); // expected-note {{in instantiation of}}
+
+template<int ...N> void empty_with_base() {
+  extern int k;
+  (k = ... = N); // expected-warning{{unused}}
+
+  // FIXME: We misparse these. The first one looks a lot loke a declaration;
+  // it's not clear what's happening in the second one.
+  void (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}}
+  (void) (k = ... = N); // expected-error {{expected ')'}} expected-note {{to match}}
+}
+template void empty_with_base<>(); // expected-note {{in instantiation of}}
+template void empty_with_base<1>();
+
+struct A {
+  struct B {
+    struct C {
+      struct D {
+        int e;
+      } d;
+    } c;
+  } b;
+} a;
+template<typename T, typename ...Ts> constexpr decltype(auto) apply(T &t, Ts ...ts) {
+  return (t.*....*ts);
+}
+static_assert(&apply(a, &A::b, &A::B::c, &A::B::C::d, &A::B::C::D::e) == &a.b.c.d.e);