Constant expression evaluation: support for default arguments.

llvm-svn: 144156
This commit is contained in:
Richard Smith 2011-11-09 02:12:41 +00:00
parent 8c2418594e
commit f8120cad16
2 changed files with 32 additions and 0 deletions

View File

@ -935,6 +935,8 @@ public:
{ return StmtVisitorTy::Visit(E->getResultExpr()); }
RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
{ return StmtVisitorTy::Visit(E->getReplacement()); }
RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
{ return StmtVisitorTy::Visit(E->getExpr()); }
RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon());

View File

@ -66,6 +66,36 @@ namespace MemberEnum {
static_assert_fold(wme.A == 42, "");
}
namespace DefaultArguments {
const int z = int();
constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
return a + b + *c + d;
}
const int four = 4;
constexpr int eight = 8;
constexpr const int twentyseven = 27;
static_assert_fold(Sum() == 0, "");
static_assert_fold(Sum(1) == 1, "");
static_assert_fold(Sum(1, four) == 5, "");
static_assert_fold(Sum(1, eight, &twentyseven) == 36, "");
static_assert_fold(Sum(1, 2, &four, eight) == 15, "");
}
namespace Ellipsis {
// Note, values passed through an ellipsis can't actually be used.
constexpr int F(int a, ...) { return a; }
static_assert_fold(F(0) == 0, "");
static_assert_fold(F(1, 0) == 1, "");
static_assert_fold(F(2, "test") == 2, "");
static_assert_fold(F(3, &F) == 3, "");
int k = 0;
static_assert_fold(F(4, k) == 3, ""); // expected-error {{constant expression}}
}
namespace Recursion {
constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
static_assert_fold(fib(11) == 89, "");