Skip to content

Commit 4e031ff

Browse files
Daniel Kroeningtautschnig
Daniel Kroening
authored andcommitted
the namespacet::follow_tag(...) methods now return the corresponding compound type
A struct_tag, union_tag, c_enum_tag always points to a struct, union, c_enum type, respectively. This is now reflected in the return type of the method, which implies that a number of explicit casts and checks can be removed.
1 parent f9c3236 commit 4e031ff

9 files changed

+28
-32
lines changed

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,8 +1888,7 @@ void c_typecheck_baset::typecheck_expr_side_effect(side_effect_exprt &expr)
18881888

18891889
if(final_type0.id()==ID_c_enum_tag)
18901890
{
1891-
if(to_c_enum_type(follow_tag(to_c_enum_tag_type(final_type0)))
1892-
.is_incomplete())
1891+
if(follow_tag(to_c_enum_tag_type(final_type0)).is_incomplete())
18931892
{
18941893
error().source_location = expr.source_location();
18951894
error() << "operator `" << statement
@@ -3159,15 +3158,15 @@ void c_typecheck_baset::typecheck_arithmetic_pointer(const exprt &expr)
31593158

31603159
if(
31613160
subtype.id() == ID_struct_tag &&
3162-
to_struct_type(follow_tag(to_struct_tag_type(subtype))).is_incomplete())
3161+
follow_tag(to_struct_tag_type(subtype)).is_incomplete())
31633162
{
31643163
error().source_location = expr.source_location();
31653164
error() << "pointer arithmetic with unknown object size" << eom;
31663165
throw 0;
31673166
}
31683167
else if(
31693168
subtype.id() == ID_union_tag &&
3170-
to_union_type(follow_tag(to_union_tag_type(subtype))).is_incomplete())
3169+
follow_tag(to_union_tag_type(subtype)).is_incomplete())
31713170
{
31723171
error().source_location = expr.source_location();
31733172
error() << "pointer arithmetic with unknown object size" << eom;
@@ -3371,8 +3370,7 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
33713370

33723371
if(underlying_type.id()==ID_c_enum_tag)
33733372
{
3374-
const typet &c_enum_type=
3375-
follow_tag(to_c_enum_tag_type(underlying_type));
3373+
const auto &c_enum_type = to_c_enum_tag_type(underlying_type);
33763374
underlying_type=c_enum_type.subtype();
33773375
}
33783376

src/ansi-c/padding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ underlying_width(const c_bit_field_typet &type, const namespacet &ns)
120120
// These point to an enum, which has a sub-subtype,
121121
// which may be smaller or larger than int, and we thus have
122122
// to check.
123-
const typet &c_enum_type = ns.follow_tag(to_c_enum_tag_type(subtype));
123+
const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(subtype));
124124

125-
if(c_enum_type.id() == ID_c_enum)
125+
if(!c_enum_type.is_incomplete())
126126
return to_bitvector_type(c_enum_type.subtype()).get_width();
127127
else
128128
return {};

src/goto-programs/goto_convert_side_effect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void goto_convertt::remove_assignment(
110110
// We convert c_enums to their underlying type, do the
111111
// operation, and then convert back
112112
const auto &enum_type = ns.follow_tag(to_c_enum_tag_type(op0_type));
113-
auto underlying_type = to_c_enum_type(enum_type).subtype();
113+
auto underlying_type = enum_type.subtype();
114114
auto op0 = typecast_exprt(expr.op0(), underlying_type);
115115
auto op1 = typecast_exprt(expr.op1(), underlying_type);
116116
binary_exprt tmp(op0, new_id, op1, underlying_type);

src/util/json_expr.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ json_objectt json(
158158
else if(type.id()==ID_c_enum_tag)
159159
{
160160
// we return the base type
161-
return json(
162-
to_c_enum_type(ns.follow_tag(to_c_enum_tag_type(type))).subtype(),
163-
ns,
164-
mode);
161+
return json(ns.follow_tag(to_c_enum_tag_type(type)).subtype(), ns, mode);
165162
}
166163
else if(type.id()==ID_fixedbv)
167164
{

src/util/namespace.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,36 @@ const typet &namespace_baset::follow(const typet &src) const
8787
/// Follow type tag of union type.
8888
/// \param src: The union tag type to dispatch on.
8989
/// \return The type of the union tag in the symbol table.
90-
const typet &namespace_baset::follow_tag(const union_tag_typet &src) const
90+
const union_typet &namespace_baset::follow_tag(const union_tag_typet &src) const
9191
{
9292
const symbolt &symbol=lookup(src.get_identifier());
9393
CHECK_RETURN(symbol.is_type);
9494
CHECK_RETURN(symbol.type.id() == ID_union);
95-
return symbol.type;
95+
return to_union_type(symbol.type);
9696
}
9797

9898
/// Follow type tag of struct type.
9999
/// \param src: The struct tag type to dispatch on.
100100
/// \return The type of the struct tag in the symbol table.
101-
const typet &namespace_baset::follow_tag(const struct_tag_typet &src) const
101+
const struct_typet &
102+
namespace_baset::follow_tag(const struct_tag_typet &src) const
102103
{
103104
const symbolt &symbol=lookup(src.get_identifier());
104105
CHECK_RETURN(symbol.is_type);
105106
CHECK_RETURN(symbol.type.id() == ID_struct);
106-
return symbol.type;
107+
return to_struct_type(symbol.type);
107108
}
108109

109110
/// Follow type tag of enum type.
110111
/// \param src: The enum tag type to dispatch on.
111112
/// \return The type of the enum tag in the symbol table.
112-
const typet &namespace_baset::follow_tag(const c_enum_tag_typet &src) const
113+
const c_enum_typet &
114+
namespace_baset::follow_tag(const c_enum_tag_typet &src) const
113115
{
114116
const symbolt &symbol=lookup(src.get_identifier());
115117
CHECK_RETURN(symbol.is_type);
116118
CHECK_RETURN(symbol.type.id() == ID_c_enum);
117-
return symbol.type;
119+
return to_c_enum_type(symbol.type);
118120
}
119121

120122
/// Follow macros to their values in a given expression.

src/util/namespace.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ class namespace_baset
6565

6666
// These produce union_typet, struct_typet, c_enum_typet or
6767
// the incomplete version.
68-
const typet &follow_tag(const union_tag_typet &) const;
69-
const typet &follow_tag(const struct_tag_typet &) const;
70-
const typet &follow_tag(const c_enum_tag_typet &) const;
68+
const union_typet &follow_tag(const union_tag_typet &) const;
69+
const struct_typet &follow_tag(const struct_tag_typet &) const;
70+
const c_enum_typet &follow_tag(const c_enum_tag_typet &) const;
7171

7272
/// Returns the minimal integer n such that there is no symbol (in any of the
7373
/// symbol tables) whose name is of the form "An" where A is \p prefix.

src/util/simplify_expr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
494494
}
495495
else if(expr_type_id==ID_c_enum_tag)
496496
{
497-
const typet &c_enum_type=ns.follow_tag(to_c_enum_tag_type(expr_type));
498-
if(c_enum_type.id()==ID_c_enum) // possibly incomplete
497+
const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(expr_type));
498+
if(!c_enum_type.is_incomplete()) // possibly incomplete
499499
{
500500
unsigned int_value = operand.is_true() ? 1u : 0u;
501501
exprt tmp=from_integer(int_value, c_enum_type);
@@ -564,8 +564,8 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
564564

565565
if(expr_type_id==ID_c_enum_tag)
566566
{
567-
const typet &c_enum_type=ns.follow_tag(to_c_enum_tag_type(expr_type));
568-
if(c_enum_type.id()==ID_c_enum) // possibly incomplete
567+
const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(expr_type));
568+
if(!c_enum_type.is_incomplete()) // possibly incomplete
569569
{
570570
exprt tmp=from_integer(int_value, c_enum_type);
571571
tmp.type()=expr_type; // we maintain the tag type
@@ -678,7 +678,7 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
678678
else if(op_type_id==ID_c_enum_tag) // enum to int
679679
{
680680
const typet &base_type =
681-
to_c_enum_type(ns.follow_tag(to_c_enum_tag_type(op_type))).subtype();
681+
ns.follow_tag(to_c_enum_tag_type(op_type)).subtype();
682682
if(base_type.id()==ID_signedbv || base_type.id()==ID_unsignedbv)
683683
{
684684
// enum constants use the representation of their base type
@@ -1574,7 +1574,7 @@ optionalt<std::string> simplify_exprt::expr2bits(
15741574
}
15751575
else if(type.id() == ID_c_enum_tag)
15761576
{
1577-
const typet &c_enum_type = ns.follow_tag(to_c_enum_tag_type(type));
1577+
const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(type));
15781578
return expr2bits(constant_exprt(value, c_enum_type), little_endian);
15791579
}
15801580
else if(type.id() == ID_c_enum)

src/util/simplify_expr_floatbv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
243243
{
244244
// go through underlying type
245245
const auto &enum_type = ns.follow_tag(to_c_enum_tag_type(src_type));
246-
exprt simplified_typecast = simplify_expr(
247-
typecast_exprt(casted_expr, to_c_enum_type(enum_type).subtype()), ns);
246+
exprt simplified_typecast =
247+
simplify_expr(typecast_exprt(casted_expr, enum_type.subtype()), ns);
248248
if(simplified_typecast.is_constant())
249249
{
250250
floatbv_typecast_exprt new_floatbv_typecast_expr =

src/util/xml_expr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ xmlt xml(
8585
else if(type.id()==ID_c_enum_tag)
8686
{
8787
// we return the base type
88-
return xml(
89-
to_c_enum_type(ns.follow_tag(to_c_enum_tag_type(type))).subtype(), ns);
88+
return xml(ns.follow_tag(to_c_enum_tag_type(type)).subtype(), ns);
9089
}
9190
else if(type.id()==ID_fixedbv)
9291
{

0 commit comments

Comments
 (0)