Skip to content

Commit bb4a135

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

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

regression/cbmc/complex1/main.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
int main()
44
{
5-
#ifdef __GNUC__
5+
#ifdef __GNUC__
66
_Complex c; // this is usually '_Complex double'
77
c=1.0i+2;
88

@@ -21,10 +21,10 @@ int main()
2121

2222
// The real part is stored first in memory on i386.
2323
// Need to find out about other architectures.
24-
#if defined(__i386__) || defined(__amd64__)
24+
#if defined(__i386__) || defined(__amd64__)
2525
assert(((signed char *)&char_complex)[0]==-2);
2626
assert(((signed char *)&char_complex)[1]==3);
27-
#endif
27+
#endif
2828

2929
assert(__real__ char_complex == -2);
3030
assert(__imag__ char_complex == 3);
@@ -44,18 +44,35 @@ 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

56-
#else
73+
#else
5774

5875
// Visual studio doesn't have it
5976

60-
#endif
77+
#endif
6178
}

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)