Skip to content

Commit 466a7e3

Browse files
Cleanup invariants in remove_complex
1 parent 4582171 commit 466a7e3

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

src/goto-programs/remove_complex.cpp

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ static exprt complex_member(const exprt &expr, irep_idt id)
3232
}
3333
else
3434
{
35-
assert(expr.type().id()==ID_struct);
35+
PRECONDITION(expr.type().id() == ID_struct);
3636
const struct_typet &struct_type=
3737
to_struct_type(expr.type());
38-
assert(struct_type.components().size()==2);
38+
PRECONDITION(struct_type.components().size() == 2);
3939
return member_exprt(expr, id, struct_type.components().front().type());
4040
}
4141
}
@@ -110,20 +110,22 @@ static void remove_complex(exprt &expr)
110110

111111
if(expr.id()==ID_typecast)
112112
{
113-
assert(expr.operands().size()==1);
114-
if(expr.op0().type().id()==ID_complex)
113+
auto const &typecast_expr = to_typecast_expr(expr);
114+
if(typecast_expr.op().type().id() == ID_complex)
115115
{
116-
if(expr.type().id()==ID_complex)
116+
if(typecast_expr.type().id() == ID_complex)
117117
{
118118
// complex to complex
119119
}
120120
else
121121
{
122122
// cast complex to non-complex is (T)__real__ x
123123
unary_exprt tmp(
124-
ID_complex_real, expr.op0(), expr.op0().type().subtype());
124+
ID_complex_real,
125+
typecast_expr.op(),
126+
typecast_expr.op().type().subtype());
125127

126-
expr=typecast_exprt(tmp, expr.type());
128+
expr = typecast_exprt(tmp, typecast_expr.type());
127129
}
128130
}
129131
}
@@ -136,7 +138,8 @@ static void remove_complex(exprt &expr)
136138
if(expr.id()==ID_plus || expr.id()==ID_minus ||
137139
expr.id()==ID_mult || expr.id()==ID_div)
138140
{
139-
assert(expr.operands().size()==2);
141+
DATA_INVARIANT(
142+
expr.operands().size() == 2, "binary operators have 2 arguments");
140143
// do component-wise:
141144
// x+y -> complex(x.r+y.r,x.i+y.i)
142145
struct_exprt struct_expr(expr.type());
@@ -158,62 +161,66 @@ static void remove_complex(exprt &expr)
158161
}
159162
else if(expr.id()==ID_unary_minus)
160163
{
161-
assert(expr.operands().size()==1);
164+
auto const &unary_minus_expr = to_unary_minus_expr(expr);
162165
// do component-wise:
163166
// -x -> complex(-x.r,-x.i)
164-
struct_exprt struct_expr(expr.type());
167+
struct_exprt struct_expr(unary_minus_expr.type());
165168
struct_expr.operands().resize(2);
166169

167-
struct_expr.op0()=
168-
unary_minus_exprt(complex_member(expr.op0(), ID_real));
170+
struct_expr.op0() =
171+
unary_minus_exprt(complex_member(unary_minus_expr.op(), ID_real));
169172

170-
struct_expr.op0().add_source_location()=expr.source_location();
173+
struct_expr.op0().add_source_location() =
174+
unary_minus_expr.source_location();
171175

172-
struct_expr.op1()=
173-
unary_minus_exprt(complex_member(expr.op0(), ID_imag));
176+
struct_expr.op1() =
177+
unary_minus_exprt(complex_member(unary_minus_expr.op(), ID_imag));
174178

175-
struct_expr.op1().add_source_location()=expr.source_location();
179+
struct_expr.op1().add_source_location() =
180+
unary_minus_expr.source_location();
176181

177182
expr=struct_expr;
178183
}
179184
else if(expr.id()==ID_complex)
180185
{
181-
assert(expr.operands().size()==2);
186+
static_cast<void>(to_complex_expr(expr));
182187
expr.id(ID_struct);
183188
}
184189
else if(expr.id()==ID_typecast)
185190
{
186-
assert(expr.operands().size()==1);
187-
typet subtype=expr.type().subtype();
191+
auto const &typecast_expr = to_typecast_expr(expr);
192+
typet subtype = typecast_expr.type().subtype();
188193

189-
if(expr.op0().type().id()==ID_struct)
194+
if(typecast_expr.op().type().id() == ID_struct)
190195
{
191196
// complex to complex -- do typecast per component
192197

193-
struct_exprt struct_expr(expr.type());
198+
struct_exprt struct_expr(typecast_expr.type());
194199
struct_expr.operands().resize(2);
195200

196-
struct_expr.op0()=
197-
typecast_exprt(complex_member(expr.op0(), ID_real), subtype);
201+
struct_expr.op0() =
202+
typecast_exprt(complex_member(typecast_expr.op(), ID_real), subtype);
198203

199-
struct_expr.op0().add_source_location()=expr.source_location();
204+
struct_expr.op0().add_source_location() =
205+
typecast_expr.source_location();
200206

201-
struct_expr.op1()=
202-
typecast_exprt(complex_member(expr.op0(), ID_imag), subtype);
207+
struct_expr.op1() =
208+
typecast_exprt(complex_member(typecast_expr.op(), ID_imag), subtype);
203209

204-
struct_expr.op1().add_source_location()=expr.source_location();
210+
struct_expr.op1().add_source_location() =
211+
typecast_expr.source_location();
205212

206213
expr=struct_expr;
207214
}
208215
else
209216
{
210217
// non-complex to complex
211-
struct_exprt struct_expr(expr.type());
218+
struct_exprt struct_expr(typecast_expr.type());
212219
struct_expr.operands().resize(2);
213220

214-
struct_expr.op0()=typecast_exprt(expr.op0(), subtype);
221+
struct_expr.op0() = typecast_exprt(typecast_expr.op(), subtype);
215222
struct_expr.op1()=from_integer(0, subtype);
216-
struct_expr.add_source_location()=expr.source_location();
223+
struct_expr.add_source_location() = typecast_expr.source_location();
217224

218225
expr=struct_expr;
219226
}
@@ -222,12 +229,12 @@ static void remove_complex(exprt &expr)
222229

223230
if(expr.id()==ID_complex_real)
224231
{
225-
assert(expr.operands().size()==1);
232+
static_cast<void>(to_complex_real_expr(expr));
226233
expr=complex_member(expr.op0(), ID_real);
227234
}
228235
else if(expr.id()==ID_complex_imag)
229236
{
230-
assert(expr.operands().size()==1);
237+
static_cast<void>(to_complex_imag_expr(expr));
231238
expr=complex_member(expr.op0(), ID_imag);
232239
}
233240

0 commit comments

Comments
 (0)