Skip to content

Commit d28ef95

Browse files
author
Joel Allred
committed
Document util/expr.{cpp,h}
1 parent dbd6988 commit d28ef95

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

doc/assets/expr.dot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
graph {
2+
ID_plus -- 3 [ label = "sub[0]" ];
3+
ID_plus -- 5 [ label = "sub[1]" ];
4+
ID_plus -- ID_integer [ label = "named_sub[ID_type]" ];
5+
}

src/doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,7 @@ DOT_PATH =
23422342
# command).
23432343
# This tag requires that the tag HAVE_DOT is set to YES.
23442344

2345-
DOTFILE_DIRS =
2345+
DOTFILE_DIRS = ../doc/assets/
23462346

23472347
# The MSCFILE_DIRS tag can be used to specify one or more directories that
23482348
# contain msc files that are included in the documentation (see the \mscfile

src/util/expr.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ Author: Daniel Kroening, [email protected]
1919

2020
#include <stack>
2121

22+
/// Move the given argument to the end of `exprt`'s operands.
23+
/// The argument is destroyed and mutated to a reference to a nil `irept`.
24+
/// \param expr: `exprt` to append to the operands
2225
void exprt::move_to_operands(exprt &expr)
2326
{
2427
operandst &op=operands();
2528
op.push_back(static_cast<const exprt &>(get_nil_irep()));
2629
op.back().swap(expr);
2730
}
2831

32+
/// Move the given arguments to the end of `exprt`'s operands.
33+
/// The argument is destroyed and mutated to a reference to a nil `irept`.
34+
/// \param e1: first `exprt` to append to the operands
35+
/// \param e2: second `exprt` to append to the operands
2936
void exprt::move_to_operands(exprt &e1, exprt &e2)
3037
{
3138
operandst &op=operands();
@@ -38,6 +45,11 @@ void exprt::move_to_operands(exprt &e1, exprt &e2)
3845
op.back().swap(e2);
3946
}
4047

48+
/// Move the given arguments to the end of `exprt`'s operands.
49+
/// The argument is destroyed and mutated to a reference to a nil `irept`.
50+
/// \param e1: first `exprt` to append to the operands
51+
/// \param e2: second `exprt` to append to the operands
52+
/// \param e3: second `exprt` to append to the operands
4153
void exprt::move_to_operands(exprt &e1, exprt &e2, exprt &e3)
4254
{
4355
operandst &op=operands();
@@ -52,11 +64,16 @@ void exprt::move_to_operands(exprt &e1, exprt &e2, exprt &e3)
5264
op.back().swap(e3);
5365
}
5466

67+
/// Copy the given argument to the end of `exprt`'s operands.
68+
/// \param expr: `exprt` to append to the operands
5569
void exprt::copy_to_operands(const exprt &expr)
5670
{
5771
operands().push_back(expr);
5872
}
5973

74+
/// Copy the given argument to the end of `exprt`'s operands.
75+
/// \param e1: first `exprt` to append to the operands
76+
/// \param e2: second `exprt` to append to the operands
6077
void exprt::copy_to_operands(const exprt &e1, const exprt &e2)
6178
{
6279
operandst &op=operands();
@@ -67,6 +84,10 @@ void exprt::copy_to_operands(const exprt &e1, const exprt &e2)
6784
op.push_back(e2);
6885
}
6986

87+
/// Copy the given argument to the end of `exprt`'s operands.
88+
/// \param e1: first `exprt` to append to the operands
89+
/// \param e2: second `exprt` to append to the operands
90+
/// \param e3: second `exprt` to append to the operands
7091
void exprt::copy_to_operands(
7192
const exprt &e1,
7293
const exprt &e2,
@@ -81,13 +102,21 @@ void exprt::copy_to_operands(
81102
op.push_back(e3);
82103
}
83104

105+
/// Create a \ref typecast_exprt to the given type.
106+
/// \param _type: cast destination type
107+
/// \deprecated use constructors instead
84108
void exprt::make_typecast(const typet &_type)
85109
{
86110
typecast_exprt new_expr(*this, _type);
87111

88112
swap(new_expr);
89113
}
90114

115+
/// Negate the expression.
116+
/// Simplifications:
117+
/// - If the expression is trivially true, make it false, and vice versa.
118+
/// - If the expression is an `ID_not`, remove the not.
119+
/// \deprecated use constructors instead
91120
void exprt::make_not()
92121
{
93122
if(is_true())
@@ -116,48 +145,71 @@ void exprt::make_not()
116145
swap(new_expr);
117146
}
118147

148+
/// Return whether the expression is a constant.
149+
/// \return True if is a constant, false otherwise
119150
bool exprt::is_constant() const
120151
{
121152
return id()==ID_constant;
122153
}
123154

155+
/// Return whether the expression is a constant representing `true`.
156+
/// \return True if is a boolean constant representing `true`, false otherwise.
124157
bool exprt::is_true() const
125158
{
126159
return is_constant() &&
127160
type().id()==ID_bool &&
128161
get(ID_value)!=ID_false;
129162
}
130163

164+
/// Return whether the expression is a constant representing `false`.
165+
/// \return True if is a boolean constant representing `false`, false otherwise.
131166
bool exprt::is_false() const
132167
{
133168
return is_constant() &&
134169
type().id()==ID_bool &&
135170
get(ID_value)==ID_false;
136171
}
137172

173+
/// Replace the expression by a boolean expression representing \p value.
174+
/// \param value: the boolean value to give to the expression
175+
/// \deprecated use constructors instead
138176
void exprt::make_bool(bool value)
139177
{
140178
*this=exprt(ID_constant, typet(ID_bool));
141179
set(ID_value, value?ID_true:ID_false);
142180
}
143181

182+
/// Replace the expression by a boolean expression representing true.
183+
/// \deprecated use constructors instead
144184
void exprt::make_true()
145185
{
146186
*this=exprt(ID_constant, typet(ID_bool));
147187
set(ID_value, ID_true);
148188
}
149189

190+
/// Replace the expression by a boolean expression representing false.
191+
/// \deprecated use constructors instead
150192
void exprt::make_false()
151193
{
152194
*this=exprt(ID_constant, typet(ID_bool));
153195
set(ID_value, ID_false);
154196
}
155197

198+
/// Return whether the expression represents a boolean.
199+
/// \return True if is a boolean, false otherwise.
156200
bool exprt::is_boolean() const
157201
{
158202
return type().id()==ID_bool;
159203
}
160204

205+
/// Return whether the expression is a constant representing 0.
206+
/// Will consider the following types: ID_integer, ID_natural, ID_rational,
207+
/// ID_unsignedbv, ID_signedbv, ID_c_bool, ID_c_bit_field, ID_fixedbv,
208+
/// ID_floatbv, ID_pointer.<br>
209+
/// For ID_pointer, returns true iff the value is a zero string or a null
210+
/// pointer.
211+
/// For all other types, return false.
212+
/// \return True if has value 0, false otherwise.
161213
bool exprt::is_zero() const
162214
{
163215
if(is_constant())
@@ -202,6 +254,12 @@ bool exprt::is_zero() const
202254
return false;
203255
}
204256

257+
/// Return whether the expression is a constant representing 1.
258+
/// Will consider the following types: ID_integer, ID_natural, ID_rational,
259+
/// ID_unsignedbv, ID_signedbv, ID_c_bool, ID_c_bit_field, ID_fixedbv,
260+
/// ID_floatbv.<br>
261+
/// For all other types, return false.
262+
/// \return True if has value 1, false otherwise.
205263
bool exprt::is_one() const
206264
{
207265
if(is_constant())
@@ -243,6 +301,11 @@ bool exprt::is_one() const
243301
return false;
244302
}
245303

304+
/// Get a \ref source_locationt from the expression or from its operands
305+
/// (non-recursively).
306+
/// If no source location is found, a nil `source_locationt` is returned.
307+
/// \return A source location if found in the expression or its operands, nil
308+
/// otherwise.
246309
const source_locationt &exprt::find_source_location() const
247310
{
248311
const source_locationt &l=source_location();

src/util/expr.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,17 @@ class depth_iteratort;
3737
class const_depth_iteratort;
3838
class const_unique_depth_iteratort;
3939

40-
/*! \brief Base class for all expressions
41-
*/
40+
/// Base class for all expressions.
41+
/// Inherits from \ref irept and has operands (stored as unnamed children of
42+
/// `irept`), and a type (which is a named sub with identifier `ID_type`).
43+
/// The class contains functions to access and modify the operands, as well as
44+
/// visitor utilities.
45+
///
46+
/// The example below shows the irept structure of the sum of integers
47+
/// 3 and 5.
48+
/// \dotfile expr.dot
49+
/// Some context available here: \ref exprt_section
50+
/// "exprt section in util module"
4251
class exprt:public irept
4352
{
4453
public:
@@ -52,14 +61,14 @@ class exprt:public irept
5261
add(ID_type, _type);
5362
}
5463

55-
// returns the type of the expression
64+
/// Returns the type of the expression
5665
typet &type() { return static_cast<typet &>(add(ID_type)); }
5766
const typet &type() const
5867
{
5968
return static_cast<const typet &>(find(ID_type));
6069
}
6170

62-
// returns true if there is at least one operand
71+
/// Returns true if there is at least one operand.
6372
bool has_operands() const
6473
{ return !operands().empty(); }
6574

@@ -96,11 +105,10 @@ class exprt:public irept
96105
void reserve_operands(operandst::size_type n)
97106
{ operands().reserve(n) ; }
98107

99-
// destroys expr, e1, e2, e3
100108
void move_to_operands(exprt &expr);
101109
void move_to_operands(exprt &e1, exprt &e2);
102110
void move_to_operands(exprt &e1, exprt &e2, exprt &e3);
103-
// does not destroy expr, e1, e2, e3
111+
104112
void copy_to_operands(const exprt &expr);
105113
void copy_to_operands(const exprt &e1, const exprt &e2);
106114
void copy_to_operands(const exprt &e1, const exprt &e2, const exprt &e3);

0 commit comments

Comments
 (0)