Skip to content

Commit f52c19e

Browse files
author
Joel Allred
committed
Document util/expr.{cpp,h}
1 parent 12efeab commit f52c19e

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
lines changed

doc/assets/expr.dot

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
graph {
2+
graph [pad="0.5", nodesep="1", ranksep="1"];
3+
plus [ label="ID_plus", xlabel="exprt" ];
4+
const3 [ label="ID_constant", xlabel="exprt" ];
5+
const5 [ label="ID_constant", xlabel="exprt" ];
6+
val3 [ label="3", xlabel="dstringt" ];
7+
val5 [ label="5", xlabel="dstringt" ];
8+
ID_integer [ xlabel="typet" ];
9+
type3 [ label="ID_integer", xlabel="typet" ];
10+
type5 [ label="ID_integer", xlabel="typet" ];
11+
plus -- const3 [ label="sub[0]" ];
12+
plus -- const5 [ label="sub[1]" ];
13+
plus -- ID_integer [ label="named_sub[ID_type]" ];
14+
const3 -- val3 [ label="sub[ID_value]" ];
15+
const3 -- type3 [ label="sub[ID_type]" ];
16+
const5 -- val5 [ label="sub[ID_value]" ];
17+
const5 -- type5 [ label="sub[ID_type]" ];
18+
}

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Module: Expression Representation
44
55
Author: Daniel Kroening, [email protected]
6+
Joel Allred, [email protected]
67
78
\*******************************************************************/
89

@@ -19,13 +20,20 @@ Author: Daniel Kroening, [email protected]
1920

2021
#include <stack>
2122

23+
/// Move the given argument to the end of `exprt`'s operands.
24+
/// The argument is destroyed and mutated to a reference to a nil `irept`.
25+
/// \param expr: `exprt` to append to the operands
2226
void exprt::move_to_operands(exprt &expr)
2327
{
2428
operandst &op=operands();
2529
op.push_back(static_cast<const exprt &>(get_nil_irep()));
2630
op.back().swap(expr);
2731
}
2832

33+
/// Move the given arguments to the end of `exprt`'s operands.
34+
/// The arguments are destroyed and mutated to a reference to a nil `irept`.
35+
/// \param e1: first `exprt` to append to the operands
36+
/// \param e2: second `exprt` to append to the operands
2937
void exprt::move_to_operands(exprt &e1, exprt &e2)
3038
{
3139
operandst &op=operands();
@@ -38,6 +46,11 @@ void exprt::move_to_operands(exprt &e1, exprt &e2)
3846
op.back().swap(e2);
3947
}
4048

49+
/// Move the given arguments to the end of `exprt`'s operands.
50+
/// The arguments are destroyed and mutated to a reference to a nil `irept`.
51+
/// \param e1: first `exprt` to append to the operands
52+
/// \param e2: second `exprt` to append to the operands
53+
/// \param e3: second `exprt` to append to the operands
4154
void exprt::move_to_operands(exprt &e1, exprt &e2, exprt &e3)
4255
{
4356
operandst &op=operands();
@@ -52,11 +65,16 @@ void exprt::move_to_operands(exprt &e1, exprt &e2, exprt &e3)
5265
op.back().swap(e3);
5366
}
5467

68+
/// Copy the given argument to the end of `exprt`'s operands.
69+
/// \param expr: `exprt` to append to the operands
5570
void exprt::copy_to_operands(const exprt &expr)
5671
{
5772
operands().push_back(expr);
5873
}
5974

75+
/// Copy the given arguments to the end of `exprt`'s operands.
76+
/// \param e1: first `exprt` to append to the operands
77+
/// \param e2: second `exprt` to append to the operands
6078
void exprt::copy_to_operands(const exprt &e1, const exprt &e2)
6179
{
6280
operandst &op=operands();
@@ -67,6 +85,10 @@ void exprt::copy_to_operands(const exprt &e1, const exprt &e2)
6785
op.push_back(e2);
6886
}
6987

88+
/// Copy the given arguments to the end of `exprt`'s operands.
89+
/// \param e1: first `exprt` to append to the operands
90+
/// \param e2: second `exprt` to append to the operands
91+
/// \param e3: second `exprt` to append to the operands
7092
void exprt::copy_to_operands(
7193
const exprt &e1,
7294
const exprt &e2,
@@ -81,13 +103,21 @@ void exprt::copy_to_operands(
81103
op.push_back(e3);
82104
}
83105

106+
/// Create a \ref typecast_exprt to the given type.
107+
/// \param _type: cast destination type
108+
/// \deprecated use constructors instead
84109
void exprt::make_typecast(const typet &_type)
85110
{
86111
typecast_exprt new_expr(*this, _type);
87112

88113
swap(new_expr);
89114
}
90115

116+
/// Negate the expression.
117+
/// Simplifications:
118+
/// - If the expression is trivially true, make it false, and vice versa.
119+
/// - If the expression is an `ID_not`, remove the not.
120+
/// \deprecated use constructors instead
91121
void exprt::make_not()
92122
{
93123
if(is_true())
@@ -116,48 +146,71 @@ void exprt::make_not()
116146
swap(new_expr);
117147
}
118148

149+
/// Return whether the expression is a constant.
150+
/// \return True if is a constant, false otherwise
119151
bool exprt::is_constant() const
120152
{
121153
return id()==ID_constant;
122154
}
123155

156+
/// Return whether the expression is a constant representing `true`.
157+
/// \return True if is a boolean constant representing `true`, false otherwise.
124158
bool exprt::is_true() const
125159
{
126160
return is_constant() &&
127161
type().id()==ID_bool &&
128162
get(ID_value)!=ID_false;
129163
}
130164

165+
/// Return whether the expression is a constant representing `false`.
166+
/// \return True if is a boolean constant representing `false`, false otherwise.
131167
bool exprt::is_false() const
132168
{
133169
return is_constant() &&
134170
type().id()==ID_bool &&
135171
get(ID_value)==ID_false;
136172
}
137173

174+
/// Replace the expression by a boolean expression representing \p value.
175+
/// \param value: the boolean value to give to the expression
176+
/// \deprecated use constructors instead
138177
void exprt::make_bool(bool value)
139178
{
140179
*this=exprt(ID_constant, typet(ID_bool));
141180
set(ID_value, value?ID_true:ID_false);
142181
}
143182

183+
/// Replace the expression by a boolean expression representing true.
184+
/// \deprecated use constructors instead
144185
void exprt::make_true()
145186
{
146187
*this=exprt(ID_constant, typet(ID_bool));
147188
set(ID_value, ID_true);
148189
}
149190

191+
/// Replace the expression by a boolean expression representing false.
192+
/// \deprecated use constructors instead
150193
void exprt::make_false()
151194
{
152195
*this=exprt(ID_constant, typet(ID_bool));
153196
set(ID_value, ID_false);
154197
}
155198

199+
/// Return whether the expression represents a boolean.
200+
/// \return True if is a boolean, false otherwise.
156201
bool exprt::is_boolean() const
157202
{
158203
return type().id()==ID_bool;
159204
}
160205

206+
/// Return whether the expression is a constant representing 0.
207+
/// Will consider the following types: ID_integer, ID_natural, ID_rational,
208+
/// ID_unsignedbv, ID_signedbv, ID_c_bool, ID_c_bit_field, ID_fixedbv,
209+
/// ID_floatbv, ID_pointer.<br>
210+
/// For ID_pointer, returns true iff the value is a zero string or a null
211+
/// pointer.
212+
/// For everything not in the above list, return false.
213+
/// \return True if has value 0, false otherwise.
161214
bool exprt::is_zero() const
162215
{
163216
if(is_constant())
@@ -202,6 +255,12 @@ bool exprt::is_zero() const
202255
return false;
203256
}
204257

258+
/// Return whether the expression is a constant representing 1.
259+
/// Will consider the following types: ID_integer, ID_natural, ID_rational,
260+
/// ID_unsignedbv, ID_signedbv, ID_c_bool, ID_c_bit_field, ID_fixedbv,
261+
/// ID_floatbv.<br>
262+
/// For all other types, return false.
263+
/// \return True if has value 1, false otherwise.
205264
bool exprt::is_one() const
206265
{
207266
if(is_constant())
@@ -243,6 +302,11 @@ bool exprt::is_one() const
243302
return false;
244303
}
245304

305+
/// Get a \ref source_locationt from the expression or from its operands
306+
/// (non-recursively).
307+
/// If no source location is found, a nil `source_locationt` is returned.
308+
/// \return A source location if found in the expression or its operands, nil
309+
/// otherwise.
246310
const source_locationt &exprt::find_source_location() const
247311
{
248312
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+
/// Return 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+
/// Return 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)