Browse files

Make ifs and asserts tail-recursive

The local Value object prevented g++ from making a tail call.  Not
clear why.  In any case, not using a temporary makes g++ do the tail
call.
  • Loading branch information...
1 parent 4badd7e commit 273322c7732093a354e86df82cf75d6604b8bce8 @edolstra edolstra committed Nov 7, 2013
Showing with 11 additions and 4 deletions.
  1. +10 −4 src/libexpr/eval.cc
  2. +1 −0 src/libexpr/eval.hh
View
14 src/libexpr/eval.cc
@@ -480,16 +480,22 @@ void EvalState::eval(Expr * e, Value & v)
}
-inline bool EvalState::evalBool(Env & env, Expr * e)
+inline bool EvalState::evalBool(Env & env, Expr * e, Value & v)
{
- Value v;
e->eval(*this, env, v);
if (v.type != tBool)
throwTypeError("value is %1% while a Boolean was expected", v);
return v.boolean;
}
+inline bool EvalState::evalBool(Env & env, Expr * e)
+{
+ Value v;
+ return evalBool(env, e, v);
+}
+
+
inline void EvalState::evalAttrs(Env & env, Expr * e, Value & v)
{
e->eval(*this, env, v);
@@ -838,13 +844,13 @@ void ExprWith::eval(EvalState & state, Env & env, Value & v)
void ExprIf::eval(EvalState & state, Env & env, Value & v)
{
- (state.evalBool(env, cond) ? then : else_)->eval(state, env, v);
+ (state.evalBool(env, cond, v) ? then : else_)->eval(state, env, v);
}
void ExprAssert::eval(EvalState & state, Env & env, Value & v)
{
- if (!state.evalBool(env, cond))
+ if (!state.evalBool(env, cond, v))
throwAssertionError("assertion failed at %1%", pos);
body->eval(state, env, v);
}
View
1 src/libexpr/eval.hh
@@ -145,6 +145,7 @@ public:
/* Evaluation the expression, then verify that it has the expected
type. */
+ inline bool evalBool(Env & env, Expr * e, Value & v);
inline bool evalBool(Env & env, Expr * e);
inline void evalAttrs(Env & env, Expr * e, Value & v);

0 comments on commit 273322c

Please sign in to comment.