Skip to content

util: Replace uses of namespacet::follow #8236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/util/lower_byte_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,10 @@ static array_exprt unpack_struct(
const std::size_t bits_per_byte,
const namespacet &ns)
{
const struct_typet &struct_type = to_struct_type(ns.follow(src.type()));
const struct_typet &struct_type =
src.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(src.type()))
: to_struct_type(src.type());
const struct_typet::componentst &components = struct_type.components();

std::optional<mp_integer> offset_in_member;
Expand Down Expand Up @@ -963,7 +966,10 @@ static exprt unpack_rec(
}
else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
{
const union_typet &union_type = to_union_type(ns.follow(src.type()));
const union_typet &union_type =
src.type().id() == ID_union_tag
? ns.follow_tag(to_union_tag_type(src.type()))
: to_union_type(src.type());

const auto widest_member = union_type.find_widest_union_component(ns);

Expand Down Expand Up @@ -1316,7 +1322,10 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
}
else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
{
const struct_typet &struct_type = to_struct_type(ns.follow(src.type()));
const struct_typet &struct_type =
src.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(src.type()))
: to_struct_type(src.type());
const struct_typet::componentst &components = struct_type.components();

bool failed = false;
Expand Down Expand Up @@ -1361,7 +1370,10 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
}
else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
{
const union_typet &union_type = to_union_type(ns.follow(src.type()));
const union_typet &union_type =
src.type().id() == ID_union_tag
? ns.follow_tag(to_union_tag_type(src.type()))
: to_union_type(src.type());

const auto widest_member = union_type.find_widest_union_component(ns);

Expand Down Expand Up @@ -2355,23 +2367,23 @@ static exprt lower_byte_update(
}
else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
{
const struct_typet &struct_type =
src.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(src.type()))
: to_struct_type(src.type());
exprt result = lower_byte_update_struct(
src,
to_struct_type(ns.follow(src.type())),
value_as_byte_array,
non_const_update_bound,
ns);
src, struct_type, value_as_byte_array, non_const_update_bound, ns);
result.type() = src.type();
return result;
}
else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
{
const union_typet &union_type =
src.type().id() == ID_union_tag
? ns.follow_tag(to_union_tag_type(src.type()))
: to_union_type(src.type());
exprt result = lower_byte_update_union(
src,
to_union_type(ns.follow(src.type())),
value_as_byte_array,
non_const_update_bound,
ns);
src, union_type, value_as_byte_array, non_const_update_bound, ns);
result.type() = src.type();
return result;
}
Expand Down
44 changes: 29 additions & 15 deletions src/util/pointer_offset_size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,17 @@
member_offset_expr(const member_exprt &member_expr, const namespacet &ns)
{
// need to distinguish structs and unions
const typet &type=ns.follow(member_expr.struct_op().type());
if(type.id()==ID_struct)
const typet &compound_type = member_expr.struct_op().type();
if(compound_type.id() == ID_struct || compound_type.id() == ID_struct_tag)
{
const struct_typet &struct_type =
compound_type.id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(compound_type))
: to_struct_type(compound_type);
return member_offset_expr(
to_struct_type(type), member_expr.get_component_name(), ns);
else if(type.id()==ID_union)
struct_type, member_expr.get_component_name(), ns);
}
else if(compound_type.id() == ID_union || compound_type.id() == ID_union_tag)
return from_integer(0, size_type());
else
return {};
Expand Down Expand Up @@ -544,17 +550,20 @@
{
const member_exprt &member_expr=to_member_expr(expr);
const exprt &op=member_expr.struct_op();
const struct_union_typet &type=to_struct_union_type(ns.follow(op.type()));

auto o = compute_pointer_offset(op, ns);

if(o.has_value())
{
if(type.id()==ID_union)
if(op.type().id() == ID_union || op.type().id() == ID_union_tag)
return *o;

auto member_offset = ::member_offset(
to_struct_type(type), member_expr.get_component_name(), ns);
const struct_typet &struct_type =
op.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(op.type()))
: to_struct_type(op.type());

Check warning on line 564 in src/util/pointer_offset_size.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/pointer_offset_size.cpp#L564

Added line #L564 was not covered by tests
auto member_offset =
::member_offset(struct_type, member_expr.get_component_name(), ns);

if(member_offset.has_value())
return *o + *member_offset;
Expand Down Expand Up @@ -589,14 +598,16 @@
return typecast_exprt(expr, target_type_raw);
}

const typet &source_type = ns.follow(expr.type());
const auto target_size_bits = pointer_offset_bits(target_type_raw, ns);
if(!target_size_bits.has_value())
return {};

if(source_type.id()==ID_struct)
if(expr.type().id() == ID_struct || expr.type().id() == ID_struct_tag)
{
const struct_typet &struct_type = to_struct_type(source_type);
const struct_typet &struct_type =
expr.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(expr.type()))
: to_struct_type(expr.type());

mp_integer m_offset_bits = 0;
for(const auto &component : struct_type.components())
Expand Down Expand Up @@ -624,9 +635,9 @@
m_offset_bits += *m_size_bits;
}
}
else if(source_type.id()==ID_array)
else if(expr.type().id() == ID_array)
{
const array_typet &array_type = to_array_type(source_type);
const array_typet &array_type = to_array_type(expr.type());

const auto elem_size_bits =
pointer_offset_bits(array_type.element_type(), ns);
Expand Down Expand Up @@ -663,9 +674,12 @@
}
else if(
object_descriptor_exprt(expr).root_object().id() == ID_union &&
source_type.id() == ID_union)
(expr.type().id() == ID_union || expr.type().id() == ID_union_tag))
{
const union_typet &union_type = to_union_type(source_type);
const union_typet &union_type =
expr.type().id() == ID_union_tag
? ns.follow_tag(to_union_tag_type(expr.type()))
: to_union_type(expr.type());

for(const auto &component : union_type.components())
{
Expand Down
72 changes: 45 additions & 27 deletions src/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,11 +1449,11 @@
// copy
auto with_expr = expr;

const typet old_type_followed = ns.follow(with_expr.old().type());

// now look at first operand

if(old_type_followed.id() == ID_struct)
if(
with_expr.old().type().id() == ID_struct ||
with_expr.old().type().id() == ID_struct_tag)
{
if(with_expr.old().id() == ID_struct || with_expr.old().is_constant())
{
Expand All @@ -1462,11 +1462,14 @@
const irep_idt &component_name =
with_expr.where().get(ID_component_name);

if(!to_struct_type(old_type_followed).has_component(component_name))
const struct_typet &old_type_followed =
with_expr.old().type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(with_expr.old().type()))
: to_struct_type(with_expr.old().type());
if(!old_type_followed.has_component(component_name))
return unchanged(expr);

std::size_t number =
to_struct_type(old_type_followed).component_number(component_name);
std::size_t number = old_type_followed.component_number(component_name);

if(number >= with_expr.old().operands().size())
return unchanged(expr);
Expand Down Expand Up @@ -1530,8 +1533,6 @@

for(const auto &e : designator)
{
const typet &value_ptr_type=ns.follow(value_ptr->type());

if(e.id()==ID_index_designator &&
value_ptr->id()==ID_array)
{
Expand All @@ -1551,7 +1552,9 @@
const irep_idt &component_name=
e.get(ID_component_name);
const struct_typet &value_ptr_struct_type =
to_struct_type(value_ptr_type);
value_ptr->type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(value_ptr->type()))
: to_struct_type(value_ptr->type());

Check warning on line 1557 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L1555-L1557

Added lines #L1555 - L1557 were not covered by tests
if(!value_ptr_struct_type.has_component(component_name))
return unchanged(expr);
auto &designator_as_struct_expr = to_struct_expr(*value_ptr);
Expand Down Expand Up @@ -1788,14 +1791,18 @@
}

if(
(expr.type().id() == ID_union || expr.type().id() == ID_union_tag) &&
to_union_type(ns.follow(expr.type())).components().empty())
(expr.type().id() == ID_union &&
to_union_type(expr.type()).components().empty()) ||
(expr.type().id() == ID_union_tag &&
ns.follow_tag(to_union_tag_type(expr.type())).components().empty()))
{
return empty_union_exprt{expr.type()};
}
else if(
(expr.type().id() == ID_struct || expr.type().id() == ID_struct_tag) &&
to_struct_type(ns.follow(expr.type())).components().empty())
(expr.type().id() == ID_struct &&
to_struct_type(expr.type()).components().empty()) ||
(expr.type().id() == ID_struct_tag &&
ns.follow_tag(to_struct_tag_type(expr.type())).components().empty()))
{
return struct_exprt{{}, expr.type()};
}
Expand Down Expand Up @@ -1870,7 +1877,9 @@
if(type.id() != ID_struct && type.id() != ID_struct_tag)
return false;

const struct_typet &st = to_struct_type(ns.follow(type));
const struct_typet &st = type.id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(type))
: to_struct_type(type);
const auto &comps = st.components();
if(comps.empty() || comps.back().type().id() != ID_array)
return false;
Expand Down Expand Up @@ -1905,7 +1914,10 @@
{
if(expr.type().id() == ID_struct || expr.type().id() == ID_struct_tag)
{
const struct_typet &struct_type = to_struct_type(ns.follow(expr.type()));
const struct_typet &struct_type =
expr.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(expr.type()))
: to_struct_type(expr.type());

Check warning on line 1920 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L1920

Added line #L1920 was not covered by tests
const struct_typet::componentst &components = struct_type.components();

bool failed = false;
Expand Down Expand Up @@ -1950,7 +1962,10 @@
}
else if(expr.type().id() == ID_union || expr.type().id() == ID_union_tag)
{
const union_typet &union_type = to_union_type(ns.follow(expr.type()));
const union_typet &union_type =
expr.type().id() == ID_union_tag
? ns.follow_tag(to_union_tag_type(expr.type()))
: to_union_type(expr.type());

Check warning on line 1968 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L1968

Added line #L1968 was not covered by tests
auto widest_member_opt = union_type.find_widest_union_component(ns);
if(widest_member_opt.has_value())
{
Expand Down Expand Up @@ -2150,10 +2165,12 @@
if(!(offset==extract.offset()))
return unchanged(expr);

const typet &tp=ns.follow(with.type());
if(tp.id()==ID_struct)
if(with.type().id() == ID_struct || with.type().id() == ID_struct_tag)

Check warning on line 2168 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L2168

Added line #L2168 was not covered by tests
{
const struct_typet &struct_type=to_struct_type(tp);
const struct_typet &struct_type =
with.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(with.type()))
: to_struct_type(with.type());

Check warning on line 2173 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L2170-L2173

Added lines #L2170 - L2173 were not covered by tests
const irep_idt &component_name=with.where().get(ID_component_name);
const typet &c_type = struct_type.get_component(component_name).type();

Expand All @@ -2178,9 +2195,10 @@
}
}
}
else if(tp.id()==ID_array)
else if(with.type().id() == ID_array)

Check warning on line 2198 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L2198

Added line #L2198 was not covered by tests
{
auto i = pointer_offset_size(to_array_type(tp).element_type(), ns);
auto i =
pointer_offset_size(to_array_type(with.type()).element_type(), ns);

Check warning on line 2201 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L2200-L2201

Added lines #L2200 - L2201 were not covered by tests
if(i.has_value())
{
const exprt &index=with.where();
Expand Down Expand Up @@ -2209,23 +2227,23 @@
if(!offset_int.has_value() || *offset_int < 0)
return unchanged(expr);

const typet &op_type=ns.follow(root.type());

// size must be known
if(!val_size.has_value() || *val_size == 0)
return unchanged(expr);

// Are we updating (parts of) a struct? Do individual member updates
// instead, unless there are non-byte-sized bit fields
if(op_type.id()==ID_struct)
if(root.type().id() == ID_struct || root.type().id() == ID_struct_tag)
{
exprt result_expr;
result_expr.make_nil();

auto update_size = pointer_offset_size(value.type(), ns);

const struct_typet &struct_type=
to_struct_type(op_type);
const struct_typet &struct_type =
root.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(root.type()))
: to_struct_type(root.type());

Check warning on line 2246 in src/util/simplify_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr.cpp#L2246

Added line #L2246 was not covered by tests
const struct_typet::componentst &components=
struct_type.components();

Expand Down Expand Up @@ -2316,7 +2334,7 @@
if(root.id()==ID_array)
{
auto el_size =
pointer_offset_bits(to_type_with_subtype(op_type).subtype(), ns);
pointer_offset_bits(to_type_with_subtype(root.type()).subtype(), ns);

if(
!el_size.has_value() || *el_size == 0 ||
Expand Down
10 changes: 7 additions & 3 deletions src/util/simplify_expr_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,22 @@
no_change = false;
}

const typet &op_type = ns.follow(new_member_expr.struct_op().type());
const typet &op_type = new_member_expr.struct_op().type();

if(op_type.id() == ID_struct)
if(op_type.id() == ID_struct || op_type.id() == ID_struct_tag)
{
// rewrite NULL -> member by
// pushing the member inside

mp_integer address;
if(is_dereference_integer_object(new_member_expr.struct_op(), address))
{
const struct_typet &struct_type =
op_type.id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(op_type))
: to_struct_type(op_type);

Check warning on line 165 in src/util/simplify_expr_pointer.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr_pointer.cpp#L165

Added line #L165 was not covered by tests
const irep_idt &member = to_member_expr(expr).get_component_name();
auto offset = member_offset(to_struct_type(op_type), member, ns);
auto offset = member_offset(struct_type, member, ns);
if(offset.has_value())
{
pointer_typet pointer_type = to_pointer_type(
Expand Down
Loading
Loading