[cfe-commits] r143298 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/SemaCXX/constant-expression-cxx11.cpp
Richard Smith
richard-llvm at metafoo.co.uk
Sat Oct 29 14:53:17 PDT 2011
Author: rsmith
Date: Sat Oct 29 16:53:17 2011
New Revision: 143298
URL: http://llvm.org/viewvc/llvm-project?rev=143298&view=rev
Log:
constexpr evaluation: allow lvalue-to-rvalue conversion on any literal type, not
just integers and floating point types. Since we don't support evaluating class
types or performing lvalue-to-rvalue conversions on array elements yet, this
just means pointer types right now.
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=143298&r1=143297&r2=143298&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sat Oct 29 16:53:17 2011
@@ -457,9 +457,6 @@
// parameters are constant expressions even if they're non-const.
// In C, such things can also be folded, although they are not ICEs.
//
- // FIXME: Allow folding any const variable of literal type initialized with
- // a constant expression. For now, we only allow variables with integral and
- // floating types to be folded.
// FIXME: volatile-qualified ParmVarDecls need special handling. A literal
// interpretation of C++11 suggests that volatile parameters are OK if
// they're never read (there's no prohibition against constructing volatile
@@ -467,7 +464,7 @@
// them are not permitted.
const VarDecl *VD = dyn_cast<VarDecl>(D);
if (!VD || !(IsConstNonVolatile(VD->getType()) || isa<ParmVarDecl>(VD)) ||
- !(Type->isIntegralOrEnumerationType() || Type->isRealFloatingType()) ||
+ !Type->isLiteralType() ||
!EvaluateVarDeclInit(Info, VD, LVal.CallIndex, RVal))
return false;
Modified: cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp?rev=143298&r1=143297&r2=143298&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp (original)
+++ cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp Sat Oct 29 16:53:17 2011
@@ -119,3 +119,41 @@
using check_value = int[9];
}
+
+namespace Pointers {
+
+ constexpr int f(int n, const int *a, const int *b, const int *c) {
+ return n == 0 ? 0 : *a + f(n-1, b, c, a);
+ }
+
+ const int x = 1, y = 10, z = 100;
+ constexpr int n1 = f(23, &x, &y, &z);
+ // FIXME: this isn't an ICE yet.
+ using check_value_1 = int[n1];
+ using check_value_1 = int[788];
+
+ constexpr int g(int n, int a, int b, int c) {
+ return f(n, &a, &b, &c);
+ }
+ constexpr int n2 = g(23, x, y, z);
+ using check_value_1 = int[n2];
+
+}
+
+namespace FunctionPointers {
+
+ constexpr int Double(int n) { return 2 * n; }
+ constexpr int Triple(int n) { return 3 * n; }
+ constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
+ constexpr int Quadruple(int n) { return Twice(Double, n); }
+ constexpr auto Select(int n) -> int (*)(int) {
+ return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
+ }
+ constexpr int Apply(int (*F)(int), int n) { return F(n); }
+
+ using check_value = int[1 + Apply(Select(4), 5) + Apply(Select(3), 7)];
+ using check_value = int[42];
+
+ constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}}
+
+}
More information about the cfe-commits
mailing list