Skip to content

Commit d46cf18

Browse files
committed
Support decrement operators over complex numbers
We already did so for increment, decrement just requires the same special case.
1 parent 3bb0589 commit d46cf18

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

regression/cbmc/complex1/main.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,31 @@ int main()
4444
char_complex++;
4545
assert(__real__ char_complex == 101);
4646
assert(__imag__ char_complex == 3);
47+
++char_complex;
48+
assert(__real__ char_complex == 102);
49+
assert(__imag__ char_complex == 3);
50+
char_complex+=1;
51+
assert(__real__ char_complex == 103);
52+
assert(__imag__ char_complex == 3);
4753

4854
// also separately
4955
(__real__ char_complex)++;
50-
assert(__real__ char_complex == 102);
56+
assert(__real__ char_complex == 104);
5157
assert(__imag__ char_complex == 3);
5258

5359
// casts to reals produce the real part
54-
assert((int) char_complex == 102);
60+
assert((int) char_complex == 104);
61+
62+
// can be decremented
63+
char_complex--;
64+
assert(__real__ char_complex == 103);
65+
assert(__imag__ char_complex == 3);
66+
--char_complex;
67+
assert(__real__ char_complex == 102);
68+
assert(__imag__ char_complex == 3);
69+
char_complex-=1;
70+
assert(__real__ char_complex == 101);
71+
assert(__imag__ char_complex == 3);
5572

5673
#else
5774

src/goto-programs/goto_convert_side_effect.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,20 @@ void goto_convertt::remove_pre(
167167
{
168168
UNREACHABLE;
169169
}
170-
else if(op_type.id()==ID_c_enum ||
171-
op_type.id()==ID_c_enum_tag)
170+
171+
exprt constant;
172+
173+
if(constant_type.id()==ID_complex)
172174
{
173-
rhs.copy_to_operands(expr.op0(), from_integer(1, signed_int_type()));
174-
rhs.op0().make_typecast(signed_int_type());
175-
rhs.type()=signed_int_type();
176-
rhs.make_typecast(op_type);
175+
exprt real=from_integer(1, constant_type.subtype());
176+
exprt imag=from_integer(0, constant_type.subtype());
177+
constant=complex_exprt(real, imag, to_complex_type(constant_type));
177178
}
178179
else
179-
{
180-
typet constant_type;
181-
182-
if(op_type.id()==ID_pointer)
183-
constant_type=index_type();
184-
else if(is_number(op_type) || op_type.id()==ID_c_bool)
185-
constant_type=op_type;
186-
else
187-
{
188-
UNREACHABLE;
189-
}
180+
constant=from_integer(1, constant_type);
190181

191-
rhs.add_to_operands(expr.op0(), from_integer(1, constant_type));
192-
rhs.type()=expr.op0().type();
193-
}
182+
rhs.add_to_operands(expr.op0(), std::move(constant));
183+
rhs.type()=expr.op0().type();
194184

195185
code_assignt assignment(expr.op0(), rhs);
196186
assignment.add_source_location()=expr.find_source_location();

0 commit comments

Comments
 (0)