Skip to content

Commit 35262aa

Browse files
committed
Re-use validate_expr in mathematical_expr.h
This provides two advantages: 1) Member access such as testing the number of operands does not break sharing, because it is done in a const context. 2) Code re-use: we no longer implement the same check in two different ways. With the exception of {exist,forall,quantifier}_exprt, all changes in this commit were applied automatically via text replacement. For quantifiers, additional checks were inserted (use of can_cast_expr, validate_expr uses quantifier_exprt's validate_expr).
1 parent 10d61b2 commit 35262aa

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

src/util/mathematical_expr.h

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ inline void validate_expr(const transt &value)
7979
inline const transt &to_trans_expr(const exprt &expr)
8080
{
8181
PRECONDITION(expr.id() == ID_trans);
82-
DATA_INVARIANT(
83-
expr.operands().size() == 3, "Transition systems must have three operands");
84-
return static_cast<const transt &>(expr);
82+
const transt &ret = static_cast<const transt &>(expr);
83+
validate_expr(ret);
84+
return ret;
8585
}
8686

8787
/// \copydoc to_trans_expr(const exprt &)
8888
inline transt &to_trans_expr(exprt &expr)
8989
{
9090
PRECONDITION(expr.id() == ID_trans);
91-
DATA_INVARIANT(
92-
expr.operands().size() == 3, "Transition systems must have three operands");
93-
return static_cast<transt &>(expr);
91+
transt &ret = static_cast<transt &>(expr);
92+
validate_expr(ret);
93+
return ret;
9494
}
9595

9696
/// \brief Exponentiation
@@ -128,16 +128,18 @@ inline void validate_expr(const power_exprt &value)
128128
inline const power_exprt &to_power_expr(const exprt &expr)
129129
{
130130
PRECONDITION(expr.id() == ID_power);
131-
DATA_INVARIANT(expr.operands().size() == 2, "Power must have two operands");
132-
return static_cast<const power_exprt &>(expr);
131+
const power_exprt &ret = static_cast<const power_exprt &>(expr);
132+
validate_expr(ret);
133+
return ret;
133134
}
134135

135136
/// \copydoc to_power_expr(const exprt &)
136137
inline power_exprt &to_power_expr(exprt &expr)
137138
{
138139
PRECONDITION(expr.id() == ID_power);
139-
DATA_INVARIANT(expr.operands().size() == 2, "Power must have two operands");
140-
return static_cast<power_exprt &>(expr);
140+
power_exprt &ret = static_cast<power_exprt &>(expr);
141+
validate_expr(ret);
142+
return ret;
141143
}
142144

143145
/// \brief Falling factorial power
@@ -175,18 +177,19 @@ inline void validate_expr(const factorial_power_exprt &value)
175177
inline const factorial_power_exprt &to_factorial_power_expr(const exprt &expr)
176178
{
177179
PRECONDITION(expr.id() == ID_factorial_power);
178-
DATA_INVARIANT(
179-
expr.operands().size() == 2, "Factorial power must have two operands");
180-
return static_cast<const factorial_power_exprt &>(expr);
180+
const factorial_power_exprt &ret =
181+
static_cast<const factorial_power_exprt &>(expr);
182+
validate_expr(ret);
183+
return ret;
181184
}
182185

183186
/// \copydoc to_factorial_power_expr(const exprt &)
184187
inline factorial_power_exprt &to_factorial_expr(exprt &expr)
185188
{
186189
PRECONDITION(expr.id() == ID_factorial_power);
187-
DATA_INVARIANT(
188-
expr.operands().size() == 2, "Factorial power must have two operands");
189-
return static_cast<factorial_power_exprt &>(expr);
190+
factorial_power_exprt &ret = static_cast<factorial_power_exprt &>(expr);
191+
validate_expr(ret);
192+
return ret;
190193
}
191194

192195
class tuple_exprt : public multi_ary_exprt
@@ -263,18 +266,20 @@ inline const function_application_exprt &
263266
to_function_application_expr(const exprt &expr)
264267
{
265268
PRECONDITION(expr.id() == ID_function_application);
266-
DATA_INVARIANT(
267-
expr.operands().size() == 2, "Function application must have two operands");
268-
return static_cast<const function_application_exprt &>(expr);
269+
const function_application_exprt &ret =
270+
static_cast<const function_application_exprt &>(expr);
271+
validate_expr(ret);
272+
return ret;
269273
}
270274

271275
/// \copydoc to_function_application_expr(const exprt &)
272276
inline function_application_exprt &to_function_application_expr(exprt &expr)
273277
{
274278
PRECONDITION(expr.id() == ID_function_application);
275-
DATA_INVARIANT(
276-
expr.operands().size() == 2, "Function application must have two operands");
277-
return static_cast<function_application_exprt &>(expr);
279+
function_application_exprt &ret =
280+
static_cast<function_application_exprt &>(expr);
281+
validate_expr(ret);
282+
return ret;
278283
}
279284

280285
/// \brief A base class for quantifier expressions
@@ -319,6 +324,8 @@ inline bool can_cast_expr<quantifier_exprt>(const exprt &base)
319324
inline void validate_expr(const quantifier_exprt &value)
320325
{
321326
validate_operands(value, 2, "quantifier expressions must have two operands");
327+
DATA_INVARIANT(
328+
value.op0().id() == ID_symbol, "quantified variable shall be a symbol");
322329
}
323330

324331
/// \brief Cast an exprt to a \ref quantifier_exprt
@@ -329,23 +336,19 @@ inline void validate_expr(const quantifier_exprt &value)
329336
/// \return Object of type \ref quantifier_exprt
330337
inline const quantifier_exprt &to_quantifier_expr(const exprt &expr)
331338
{
332-
DATA_INVARIANT(
333-
expr.operands().size() == 2,
334-
"quantifier expressions must have two operands");
335-
DATA_INVARIANT(
336-
expr.op0().id() == ID_symbol, "quantified variable shall be a symbol");
337-
return static_cast<const quantifier_exprt &>(expr);
339+
PRECONDITION(can_cast_expr<quantifier_exprt>(expr));
340+
const quantifier_exprt &ret = static_cast<const quantifier_exprt &>(expr);
341+
validate_expr(ret);
342+
return ret;
338343
}
339344

340345
/// \copydoc to_quantifier_expr(const exprt &)
341346
inline quantifier_exprt &to_quantifier_expr(exprt &expr)
342347
{
343-
DATA_INVARIANT(
344-
expr.operands().size() == 2,
345-
"quantifier expressions must have two operands");
346-
DATA_INVARIANT(
347-
expr.op0().id() == ID_symbol, "quantified variable shall be a symbol");
348-
return static_cast<quantifier_exprt &>(expr);
348+
PRECONDITION(can_cast_expr<quantifier_exprt>(expr));
349+
quantifier_exprt &ret = static_cast<quantifier_exprt &>(expr);
350+
validate_expr(ret);
351+
return ret;
349352
}
350353

351354
/// \brief A forall expression
@@ -361,19 +364,17 @@ class forall_exprt : public quantifier_exprt
361364
inline const forall_exprt &to_forall_expr(const exprt &expr)
362365
{
363366
PRECONDITION(expr.id() == ID_forall);
364-
DATA_INVARIANT(
365-
expr.operands().size() == 2,
366-
"forall expressions have exactly two operands");
367-
return static_cast<const forall_exprt &>(expr);
367+
const forall_exprt &ret = static_cast<const forall_exprt &>(expr);
368+
validate_expr(static_cast<const quantifier_exprt &>(ret));
369+
return ret;
368370
}
369371

370372
inline forall_exprt &to_forall_expr(exprt &expr)
371373
{
372374
PRECONDITION(expr.id() == ID_forall);
373-
DATA_INVARIANT(
374-
expr.operands().size() == 2,
375-
"forall expressions have exactly two operands");
376-
return static_cast<forall_exprt &>(expr);
375+
forall_exprt &ret = static_cast<forall_exprt &>(expr);
376+
validate_expr(static_cast<const quantifier_exprt &>(ret));
377+
return ret;
377378
}
378379

379380
/// \brief An exists expression
@@ -389,19 +390,17 @@ class exists_exprt : public quantifier_exprt
389390
inline const exists_exprt &to_exists_expr(const exprt &expr)
390391
{
391392
PRECONDITION(expr.id() == ID_exists);
392-
DATA_INVARIANT(
393-
expr.operands().size() == 2,
394-
"exists expressions have exactly two operands");
395-
return static_cast<const exists_exprt &>(expr);
393+
const exists_exprt &ret = static_cast<const exists_exprt &>(expr);
394+
validate_expr(static_cast<const quantifier_exprt &>(ret));
395+
return ret;
396396
}
397397

398398
inline exists_exprt &to_exists_expr(exprt &expr)
399399
{
400400
PRECONDITION(expr.id() == ID_exists);
401-
DATA_INVARIANT(
402-
expr.operands().size() == 2,
403-
"exists expressions have exactly two operands");
404-
return static_cast<exists_exprt &>(expr);
401+
exists_exprt &ret = static_cast<exists_exprt &>(expr);
402+
validate_expr(static_cast<const quantifier_exprt &>(ret));
403+
return ret;
405404
}
406405

407406
#endif // CPROVER_UTIL_MATHEMATICAL_EXPR_H

0 commit comments

Comments
 (0)