aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2013-05-06 06:51:17 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2013-05-06 06:51:17 +0000
commit692eafd2052fb6ca581530d6f3569eea9520a508 (patch)
treeeb574b7aab5fe2222357ba8188f852a945d497ce
parentc194c95036b7bf1281a6f2ed683f7c85ee5d2c20 (diff)
C++1y: support range-based for loops in constant expressions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181184 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/AST/ExprConstant.cpp39
-rw-r--r--test/SemaCXX/constant-expression-cxx1y.cpp54
2 files changed, 93 insertions, 0 deletions
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 14503f4752..95bfd63f4d 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -2636,6 +2636,45 @@ static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info,
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;
diff --git a/test/SemaCXX/constant-expression-cxx1y.cpp b/test/SemaCXX/constant-expression-cxx1y.cpp
index d8cfb1c297..60ec82062d 100644
--- a/test/SemaCXX/constant-expression-cxx1y.cpp
+++ b/test/SemaCXX/constant-expression-cxx1y.cpp
@@ -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, "");
}