r181184 - C++1y: support range-based for loops in constant expressions.

Richard Smith richard-llvm at metafoo.co.uk
Sun May 5 23:51:17 PDT 2013


Author: rsmith
Date: Mon May  6 01:51:17 2013
New Revision: 181184

URL: http://llvm.org/viewvc/llvm-project?rev=181184&view=rev
Log:
C++1y: support range-based for loops in constant expressions.

Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=181184&r1=181183&r2=181184&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon May  6 01:51:17 2013
@@ -2636,6 +2636,45 @@ static EvalStmtResult EvaluateStmt(APVal
     return ESR_Succeeded;
   }
 
+  case Stmt::CXXForRangeStmtClass: {
+    const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S);
+
+    // Initialize the __range variable.
+    EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt());
+    if (ESR != ESR_Succeeded)
+      return ESR;
+
+    // Create the __begin and __end iterators.
+    ESR = EvaluateStmt(Result, Info, FS->getBeginEndStmt());
+    if (ESR != ESR_Succeeded)
+      return ESR;
+
+    while (true) {
+      // Condition: __begin != __end.
+      bool Continue = true;
+      if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info))
+        return ESR_Failed;
+      if (!Continue)
+        break;
+
+      // User's variable declaration, initialized by *__begin.
+      ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt());
+      if (ESR != ESR_Succeeded)
+        return ESR;
+
+      // Loop body.
+      ESR = EvaluateLoopBody(Result, Info, FS->getBody());
+      if (ESR != ESR_Continue)
+        return ESR;
+
+      // Increment: ++__begin
+      if (!EvaluateIgnoredValue(Info, FS->getInc()))
+        return ESR_Failed;
+    }
+
+    return ESR_Succeeded;
+  }
+
   case Stmt::ContinueStmtClass:
     return ESR_Continue;
 

Modified: cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp?rev=181184&r1=181183&r2=181184&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp (original)
+++ cfe/trunk/test/SemaCXX/constant-expression-cxx1y.cpp Mon May  6 01:51:17 2013
@@ -402,4 +402,58 @@ namespace loops {
     return true;
   }
   static_assert(cond(), "");
+
+  constexpr int range_for() {
+    int arr[] = { 1, 2, 3, 4, 5 };
+    int sum = 0;
+    for (int x : arr)
+      sum = sum + x;
+    return sum;
+  }
+  static_assert(range_for() == 15, "");
+
+  template<int...N> struct ints {};
+  template<typename A, typename B> struct join_ints;
+  template<int...As, int...Bs> struct join_ints<ints<As...>, ints<Bs...>> {
+    using type = ints<As..., sizeof...(As) + Bs...>;
+  };
+  template<unsigned N> struct make_ints {
+    using type = typename join_ints<typename make_ints<N/2>::type, typename make_ints<(N+1)/2>::type>::type;
+  };
+  template<> struct make_ints<0> { using type = ints<>; };
+  template<> struct make_ints<1> { using type = ints<0>; };
+
+  struct ignore { template<typename ...Ts> constexpr ignore(Ts &&...) {} };
+
+  template<typename T, unsigned N> struct array {
+    constexpr array() : arr{} {}
+    template<typename ...X>
+    constexpr array(X ...x) : arr{} {
+      init(typename make_ints<sizeof...(X)>::type{}, x...);
+    }
+    template<int ...I, typename ...X> constexpr void init(ints<I...>, X ...x) {
+      ignore{arr[I] = x ...};
+    }
+    T arr[N];
+    struct iterator {
+      T *p;
+      constexpr explicit iterator(T *p) : p(p) {}
+      constexpr bool operator!=(iterator o) { return p != o.p; }
+      constexpr iterator &operator++() { ++p; return *this; }
+      constexpr T &operator*() { return *p; }
+    };
+    constexpr iterator begin() { return iterator(arr); }
+    constexpr iterator end() { return iterator(arr + N); }
+  };
+
+  constexpr int range_for_2() {
+    array<int, 5> arr { 1, 2, 3, 4, 5 };
+    int sum = 0;
+    for (int k : arr) {
+      sum = sum + k;
+      if (sum > 8) break;
+    }
+    return sum;
+  }
+  static_assert(range_for_2() == 10, "");
 }





More information about the cfe-commits mailing list