Skip to content

Commit 195458a

Browse files
authored
Merge pull request #6588 from diffblue/c_enum_underlying_type
c_enum_typet::underlying_type() and c_bitfield_typet::underlying_type()
2 parents ded98de + 6776f0c commit 195458a

18 files changed

+109
-79
lines changed

src/ansi-c/c_typecast.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,17 @@ c_typecastt::c_typet c_typecastt::get_c_type(
365365
// of bits given
366366
typet underlying_type;
367367

368-
if(bit_field_type.subtype().id() == ID_c_enum_tag)
368+
if(bit_field_type.underlying_type().id() == ID_c_enum_tag)
369369
{
370370
const auto &followed =
371-
ns.follow_tag(to_c_enum_tag_type(bit_field_type.subtype()));
371+
ns.follow_tag(to_c_enum_tag_type(bit_field_type.underlying_type()));
372372
if(followed.is_incomplete())
373373
return INT;
374374
else
375-
underlying_type = followed.subtype();
375+
underlying_type = followed.underlying_type();
376376
}
377377
else
378-
underlying_type = bit_field_type.subtype();
378+
underlying_type = bit_field_type.underlying_type();
379379

380380
const bitvector_typet new_type(
381381
underlying_type.id(), bit_field_type.get_width());

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,13 +1849,14 @@ void c_typecheck_baset::typecheck_expr_side_effect(side_effect_exprt &expr)
18491849
}
18501850

18511851
// increment/decrement on underlying type
1852-
to_unary_expr(expr).op() = typecast_exprt(op0, enum_type.subtype());
1853-
expr.type() = enum_type.subtype();
1852+
to_unary_expr(expr).op() =
1853+
typecast_exprt(op0, enum_type.underlying_type());
1854+
expr.type() = enum_type.underlying_type();
18541855
}
18551856
else if(type0.id() == ID_c_bit_field)
18561857
{
18571858
// promote to underlying type
1858-
typet underlying_type = to_c_bit_field_type(type0).subtype();
1859+
typet underlying_type = to_c_bit_field_type(type0).underlying_type();
18591860
to_unary_expr(expr).op() = typecast_exprt(op0, underlying_type);
18601861
expr.type()=underlying_type;
18611862
}
@@ -3057,7 +3058,7 @@ exprt c_typecheck_baset::do_special_functions(
30573058

30583059
// use underlying type for bit fields
30593060
if(type.id() == ID_c_bit_field)
3060-
type = to_c_bit_field_type(type).subtype();
3061+
type = to_c_bit_field_type(type).underlying_type();
30613062

30623063
unsigned type_number;
30633064

src/ansi-c/c_typecheck_type.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ void c_typecheck_baset::typecheck_type(typet &type)
134134
// but we'll try to interpret it the GCC way
135135
if(underlying_type.id()==ID_c_enum_tag)
136136
{
137-
underlying_type=
138-
follow_tag(to_c_enum_tag_type(underlying_type)).subtype();
137+
underlying_type =
138+
follow_tag(to_c_enum_tag_type(underlying_type)).underlying_type();
139139

140140
assert(underlying_type.id()==ID_signedbv ||
141141
underlying_type.id()==ID_unsignedbv);
@@ -1480,7 +1480,7 @@ void c_typecheck_baset::typecheck_c_enum_tag_type(c_enum_tag_typet &type)
14801480

14811481
void c_typecheck_baset::typecheck_c_bit_field_type(c_bit_field_typet &type)
14821482
{
1483-
typecheck_type(type.subtype());
1483+
typecheck_type(type.underlying_type());
14841484

14851485
mp_integer i;
14861486

@@ -1508,28 +1508,28 @@ void c_typecheck_baset::typecheck_c_bit_field_type(c_bit_field_typet &type)
15081508
type.remove(ID_size);
15091509
}
15101510

1511-
const typet &subtype = type.subtype();
1511+
const typet &underlying_type = type.underlying_type();
15121512

15131513
std::size_t sub_width=0;
15141514

1515-
if(subtype.id()==ID_bool)
1515+
if(underlying_type.id() == ID_bool)
15161516
{
15171517
// This is the 'proper' bool.
15181518
sub_width=1;
15191519
}
1520-
else if(subtype.id()==ID_signedbv ||
1521-
subtype.id()==ID_unsignedbv ||
1522-
subtype.id()==ID_c_bool)
1520+
else if(
1521+
underlying_type.id() == ID_signedbv ||
1522+
underlying_type.id() == ID_unsignedbv || underlying_type.id() == ID_c_bool)
15231523
{
1524-
sub_width=to_bitvector_type(subtype).get_width();
1524+
sub_width = to_bitvector_type(underlying_type).get_width();
15251525
}
1526-
else if(subtype.id()==ID_c_enum_tag)
1526+
else if(underlying_type.id() == ID_c_enum_tag)
15271527
{
15281528
// These point to an enum, which has a sub-subtype,
15291529
// which may be smaller or larger than int, and we thus have
15301530
// to check.
15311531
const auto &c_enum_type =
1532-
to_c_enum_type(follow_tag(to_c_enum_tag_type(subtype)));
1532+
to_c_enum_type(follow_tag(to_c_enum_tag_type(underlying_type)));
15331533

15341534
if(c_enum_type.is_incomplete())
15351535
{
@@ -1538,13 +1538,13 @@ void c_typecheck_baset::typecheck_c_bit_field_type(c_bit_field_typet &type)
15381538
throw 0;
15391539
}
15401540

1541-
sub_width = to_bitvector_type(c_enum_type.subtype()).get_width();
1541+
sub_width = to_bitvector_type(c_enum_type.underlying_type()).get_width();
15421542
}
15431543
else
15441544
{
15451545
error().source_location=type.source_location();
1546-
error() << "bit field with non-integer type: "
1547-
<< to_string(subtype) << eom;
1546+
error() << "bit field with non-integer type: " << to_string(underlying_type)
1547+
<< eom;
15481548
throw 0;
15491549
}
15501550

src/ansi-c/expr2c.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,9 @@ std::string expr2ct::convert_rec(
412412
if(!to_c_enum_type(src).is_incomplete())
413413
{
414414
const c_enum_typet &c_enum_type = to_c_enum_type(src);
415-
const bool is_signed = c_enum_type.subtype().id() == ID_signedbv;
416-
const auto width = to_bitvector_type(c_enum_type.subtype()).get_width();
415+
const bool is_signed = c_enum_type.underlying_type().id() == ID_signedbv;
416+
const auto width =
417+
to_bitvector_type(c_enum_type.underlying_type()).get_width();
417418

418419
result += '{';
419420

src/ansi-c/padding.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,29 @@ mp_integer alignment(const typet &type, const namespacet &ns)
104104
static optionalt<std::size_t>
105105
underlying_width(const c_bit_field_typet &type, const namespacet &ns)
106106
{
107-
const typet &subtype = type.subtype();
107+
const typet &underlying_type = type.underlying_type();
108108

109-
if(subtype.id() == ID_bool)
109+
if(underlying_type.id() == ID_bool)
110110
{
111111
// This is the 'proper' bool.
112112
return 1;
113113
}
114114
else if(
115-
subtype.id() == ID_signedbv || subtype.id() == ID_unsignedbv ||
116-
subtype.id() == ID_c_bool)
115+
underlying_type.id() == ID_signedbv ||
116+
underlying_type.id() == ID_unsignedbv || underlying_type.id() == ID_c_bool)
117117
{
118-
return to_bitvector_type(subtype).get_width();
118+
return to_bitvector_type(underlying_type).get_width();
119119
}
120-
else if(subtype.id() == ID_c_enum_tag)
120+
else if(underlying_type.id() == ID_c_enum_tag)
121121
{
122122
// These point to an enum, which has a sub-subtype,
123123
// which may be smaller or larger than int, and we thus have
124124
// to check.
125-
const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(subtype));
125+
const auto &c_enum_type =
126+
ns.follow_tag(to_c_enum_tag_type(underlying_type));
126127

127128
if(!c_enum_type.is_incomplete())
128-
return to_bitvector_type(c_enum_type.subtype()).get_width();
129+
return to_bitvector_type(c_enum_type.underlying_type()).get_width();
129130
else
130131
return {};
131132
}
@@ -352,7 +353,7 @@ static void add_padding_gcc(struct_typet &type, const namespacet &ns)
352353

353354
if(it_type.id()==ID_c_bit_field)
354355
{
355-
a=alignment(to_c_bit_field_type(it_type).subtype(), ns);
356+
a = alignment(to_c_bit_field_type(it_type).underlying_type(), ns);
356357

357358
// A zero-width bit-field causes alignment to the base-type.
358359
if(to_c_bit_field_type(it_type).get_width()==0)

src/cpp/cpp_typecheck_enum_type.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void cpp_typecheckt::typecheck_enum_body(symbolt &enum_symbol)
3838
{
3939
exprt &value = static_cast<exprt &>(component.add(ID_value));
4040
typecheck_expr(value);
41-
implicit_typecast(value, c_enum_type.subtype());
41+
implicit_typecast(value, c_enum_type.underlying_type());
4242
make_constant(value);
4343
if(to_integer(to_constant_expr(value), i))
4444
{
@@ -48,7 +48,7 @@ void cpp_typecheckt::typecheck_enum_body(symbolt &enum_symbol)
4848
}
4949
}
5050

51-
exprt value_expr=from_integer(i, c_enum_type.subtype());
51+
exprt value_expr = from_integer(i, c_enum_type.underlying_type());
5252
value_expr.type()=enum_tag_type; // override type
5353

5454
symbolt symbol;

src/goto-instrument/goto_program2code.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,9 @@ void goto_program2codet::remove_const(typet &type)
16041604
remove_const(it->type());
16051605
}
16061606
else if(type.id() == ID_c_bit_field)
1607-
to_c_bit_field_type(type).subtype().remove(ID_C_constant);
1607+
{
1608+
to_c_bit_field_type(type).underlying_type().remove(ID_C_constant);
1609+
}
16081610
}
16091611

16101612
static bool has_labels(const codet &code)

src/goto-programs/goto_trace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static std::string numeric_representation(
170170

171171
const typet &underlying_type =
172172
expr_type.id() == ID_c_enum_tag
173-
? ns.follow_tag(to_c_enum_tag_type(expr_type)).subtype()
173+
? ns.follow_tag(to_c_enum_tag_type(expr_type)).underlying_type()
174174
: expr_type;
175175

176176
const irep_idt &value = expr.get_value();

src/goto-programs/json_expr.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ json_objectt json(const typet &type, const namespacet &ns, const irep_idt &mode)
132132
}
133133
else if(type.id() == ID_c_enum_tag)
134134
{
135-
// we return the base type
136-
return json(ns.follow_tag(to_c_enum_tag_type(type)).subtype(), ns, mode);
135+
// we return the underlying type
136+
return json(
137+
ns.follow_tag(to_c_enum_tag_type(type)).underlying_type(), ns, mode);
137138
}
138139
else if(type.id() == ID_fixedbv)
139140
{
@@ -206,7 +207,8 @@ static std::string binary(const constant_exprt &src)
206207
{
207208
std::size_t width;
208209
if(src.type().id() == ID_c_enum)
209-
width = to_bitvector_type(to_c_enum_type(src.type()).subtype()).get_width();
210+
width = to_bitvector_type(to_c_enum_type(src.type()).underlying_type())
211+
.get_width();
210212
else
211213
width = to_bitvector_type(src.type()).get_width();
212214
const auto int_val = bvrep2integer(src.get_value(), width, false);
@@ -236,7 +238,8 @@ json_objectt json(const exprt &expr, const namespacet &ns, const irep_idt &mode)
236238
lang = std::unique_ptr<languaget>(get_default_language());
237239

238240
const typet &underlying_type =
239-
type.id() == ID_c_bit_field ? to_c_bit_field_type(type).subtype() : type;
241+
type.id() == ID_c_bit_field ? to_c_bit_field_type(type).underlying_type()
242+
: type;
240243

241244
std::string type_string;
242245
bool error = lang->from_type(underlying_type, type_string, ns);
@@ -262,7 +265,7 @@ json_objectt json(const exprt &expr, const namespacet &ns, const irep_idt &mode)
262265
result["name"] = json_stringt("integer");
263266
result["binary"] = json_stringt(binary(constant_expr));
264267
result["width"] = json_numbert(std::to_string(
265-
to_bitvector_type(to_c_enum_type(type).subtype()).get_width()));
268+
to_bitvector_type(to_c_enum_type(type).underlying_type()).get_width()));
266269
result["type"] = json_stringt("enum");
267270
result["data"] = json_stringt(value_string);
268271
}

src/goto-programs/xml_expr.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ xmlt xml(const typet &type, const namespacet &ns)
5454
}
5555
else if(type.id() == ID_c_enum_tag)
5656
{
57-
// we return the base type
58-
return xml(ns.follow_tag(to_c_enum_tag_type(type)).subtype(), ns);
57+
// we return the underlying type
58+
return xml(ns.follow_tag(to_c_enum_tag_type(type)).underlying_type(), ns);
5959
}
6060
else if(type.id() == ID_fixedbv)
6161
{
@@ -148,9 +148,10 @@ xmlt xml(const exprt &expr, const namespacet &ns)
148148
result.set_attribute("binary", integer2binary(i, width));
149149
result.set_attribute("width", width);
150150

151-
const typet &underlying_type = type.id() == ID_c_bit_field
152-
? to_c_bit_field_type(type).subtype()
153-
: type;
151+
const typet &underlying_type =
152+
type.id() == ID_c_bit_field
153+
? to_c_bit_field_type(type).underlying_type()
154+
: type;
154155

155156
bool is_signed = underlying_type.id() == ID_signedbv;
156157

@@ -174,7 +175,7 @@ xmlt xml(const exprt &expr, const namespacet &ns)
174175
else if(type.id() == ID_c_enum)
175176
{
176177
const auto width =
177-
to_bitvector_type(to_c_enum_type(type).subtype()).get_width();
178+
to_bitvector_type(to_c_enum_type(type).underlying_type()).get_width();
178179

179180
const auto integer_value = bvrep2integer(value, width, false);
180181
result.name = "integer";

src/solvers/flattening/c_bit_field_replacement_type.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ typet c_bit_field_replacement_type(
1616
const c_bit_field_typet &src,
1717
const namespacet &ns)
1818
{
19-
const typet &subtype=src.subtype();
19+
const typet &underlying_type = src.underlying_type();
2020

21-
if(subtype.id()==ID_unsignedbv ||
22-
subtype.id()==ID_signedbv ||
23-
subtype.id()==ID_c_bool)
21+
if(
22+
underlying_type.id() == ID_unsignedbv ||
23+
underlying_type.id() == ID_signedbv || underlying_type.id() == ID_c_bool)
2424
{
25-
bitvector_typet result=to_bitvector_type(subtype);
25+
bitvector_typet result = to_bitvector_type(underlying_type);
2626
result.set_width(src.get_width());
2727
return std::move(result);
2828
}
2929
else
3030
{
31-
PRECONDITION(subtype.id() == ID_c_enum_tag);
31+
PRECONDITION(underlying_type.id() == ID_c_enum_tag);
3232

33-
const typet &sub_subtype=
34-
ns.follow_tag(to_c_enum_tag_type(subtype)).subtype();
33+
const typet &sub_subtype =
34+
ns.follow_tag(to_c_enum_tag_type(underlying_type)).underlying_type();
3535

3636
PRECONDITION(
3737
sub_subtype.id() == ID_signedbv || sub_subtype.id() == ID_unsignedbv);

src/solvers/smt2/smt2_conv.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ std::string smt2_convt::type2id(const typet &type) const
931931
}
932932
else if(type.id()==ID_c_enum_tag)
933933
{
934-
return type2id(ns.follow_tag(to_c_enum_tag_type(type)).subtype());
934+
return type2id(ns.follow_tag(to_c_enum_tag_type(type)).underlying_type());
935935
}
936936
else if(type.id() == ID_pointer)
937937
{
@@ -2775,10 +2775,8 @@ void smt2_convt::convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
27752775

27762776
// We first convert to 'underlying type'
27772777
floatbv_typecast_exprt tmp=expr;
2778-
tmp.op()=
2779-
typecast_exprt(
2780-
src,
2781-
ns.follow_tag(to_c_enum_tag_type(src_type)).subtype());
2778+
tmp.op() = typecast_exprt(
2779+
src, ns.follow_tag(to_c_enum_tag_type(src_type)).underlying_type());
27822780
convert_floatbv_typecast(tmp);
27832781
}
27842782
else

src/util/arith_tools.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ bool to_integer(const constant_exprt &expr, mp_integer &int_value)
5656
}
5757
else if(type_id==ID_c_enum)
5858
{
59-
const typet &subtype=to_c_enum_type(type).subtype();
60-
if(subtype.id()==ID_signedbv)
59+
const typet &underlying_type = to_c_enum_type(type).underlying_type();
60+
if(underlying_type.id() == ID_signedbv)
6161
{
6262
const auto width = to_signedbv_type(type).get_width();
6363
int_value = bvrep2integer(value, width, true);
6464
return false;
6565
}
66-
else if(subtype.id()==ID_unsignedbv)
66+
else if(underlying_type.id() == ID_unsignedbv)
6767
{
6868
const auto width = to_unsignedbv_type(type).get_width();
6969
int_value = bvrep2integer(value, width, false);
@@ -74,19 +74,19 @@ bool to_integer(const constant_exprt &expr, mp_integer &int_value)
7474
{
7575
const auto &c_bit_field_type = to_c_bit_field_type(type);
7676
const auto width = c_bit_field_type.get_width();
77-
const typet &subtype = c_bit_field_type.subtype();
77+
const typet &underlying_type = c_bit_field_type.underlying_type();
7878

79-
if(subtype.id()==ID_signedbv)
79+
if(underlying_type.id() == ID_signedbv)
8080
{
8181
int_value = bvrep2integer(value, width, true);
8282
return false;
8383
}
84-
else if(subtype.id()==ID_unsignedbv)
84+
else if(underlying_type.id() == ID_unsignedbv)
8585
{
8686
int_value = bvrep2integer(value, width, false);
8787
return false;
8888
}
89-
else if(subtype.id() == ID_c_bool)
89+
else if(underlying_type.id() == ID_c_bool)
9090
{
9191
int_value = bvrep2integer(value, width, false);
9292
return false;
@@ -129,7 +129,7 @@ constant_exprt from_integer(
129129
else if(type_id==ID_c_enum)
130130
{
131131
const std::size_t width =
132-
to_bitvector_type(to_c_enum_type(type).subtype()).get_width();
132+
to_bitvector_type(to_c_enum_type(type).underlying_type()).get_width();
133133
return constant_exprt(integer2bvrep(int_value, width), type);
134134
}
135135
else if(type_id==ID_c_bool)

0 commit comments

Comments
 (0)