Skip to content

Commit 31946bc

Browse files
committed
introduce index_type and element_type for arrays and vectors
This commit introduces convenience methods for retrieving the type of the elements and of the indices for array and vector types. This avoids using a global, architecture-dependent function to generate the index type.
1 parent 8c4d15b commit 31946bc

File tree

7 files changed

+47
-13
lines changed

7 files changed

+47
-13
lines changed

src/goto-instrument/goto_program2code.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,7 @@ void goto_program2codet::convert_assign_rec(
405405
forall_operands(it, assign.rhs())
406406
{
407407
index_exprt index(
408-
assign.lhs(),
409-
from_integer(i++, index_type()),
410-
type.subtype());
408+
assign.lhs(), from_integer(i++, type.index_type()), type.subtype());
411409
convert_assign_rec(code_assignt(index, *it), dest);
412410
}
413411
}

src/goto-symex/field_sensitivity.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ exprt field_sensitivityt::get_fields(
204204

205205
for(std::size_t i = 0; i < array_size; ++i)
206206
{
207-
const index_exprt index(array, from_integer(i, index_type()));
207+
const index_exprt index(array, from_integer(i, type.index_type()));
208208
ssa_exprt tmp = ssa_expr;
209209
bool was_l2 = !tmp.get_level_2().empty();
210210
tmp.remove_level_2();
@@ -318,7 +318,7 @@ void field_sensitivityt::field_assignments_rec(
318318
exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
319319
for(std::size_t i = 0; i < array_size; ++i)
320320
{
321-
const index_exprt index_rhs(lhs, from_integer(i, index_type()));
321+
const index_exprt index_rhs(lhs, from_integer(i, type->index_type()));
322322
const exprt &index_lhs = *fs_it;
323323

324324
field_assignments_rec(

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void goto_symext::symex_allocate(
197197
{
198198
const auto &array_type = to_array_type(*object_type);
199199
index_exprt index_expr(
200-
value_symbol.symbol_expr(), from_integer(0, index_type()));
200+
value_symbol.symbol_expr(), from_integer(0, array_type.index_type()));
201201
rhs = address_of_exprt(index_expr, pointer_type(array_type.subtype()));
202202
}
203203
else

src/solvers/flattening/boolbv_get.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ exprt boolbvt::bv_get_rec(
6969
{
7070
if(type.id()==ID_array)
7171
{
72+
const auto &array_type = to_array_type(type);
73+
7274
if(is_unbounded_array(type))
7375
return bv_get_unbounded_array(expr);
7476

75-
const typet &subtype=type.subtype();
77+
const typet &subtype = array_type.element_type();
7678
std::size_t sub_width=boolbv_width(subtype);
7779

7880
if(sub_width!=0)
@@ -85,7 +87,8 @@ exprt boolbvt::bv_get_rec(
8587
new_offset+=sub_width)
8688
{
8789
const index_exprt index{
88-
expr, from_integer(new_offset / sub_width, index_type())};
90+
expr,
91+
from_integer(new_offset / sub_width, array_type.index_type())};
8992
op.push_back(bv_get_rec(index, bv, offset + new_offset, subtype));
9093
}
9194

@@ -95,7 +98,7 @@ exprt boolbvt::bv_get_rec(
9598
}
9699
else
97100
{
98-
return array_exprt{{}, to_array_type(type)};
101+
return array_exprt{{}, array_type};
99102
}
100103
}
101104
else if(type.id()==ID_struct_tag)
@@ -156,18 +159,20 @@ exprt boolbvt::bv_get_rec(
156159
}
157160
else if(type.id()==ID_vector)
158161
{
159-
const typet &subtype = type.subtype();
162+
const auto &vector_type = to_vector_type(type);
163+
const typet &subtype = vector_type.subtype();
160164
std::size_t sub_width=boolbv_width(subtype);
161165

162166
if(sub_width!=0 && width%sub_width==0)
163167
{
164168
std::size_t size=width/sub_width;
165-
vector_exprt value({}, to_vector_type(type));
169+
vector_exprt value({}, vector_type);
166170
value.reserve_operands(size);
167171

168172
for(std::size_t i=0; i<size; i++)
169173
{
170-
const index_exprt index{expr, from_integer(i, index_type())};
174+
const index_exprt index{expr,
175+
from_integer(i, vector_type.index_type())};
171176
value.operands().push_back(
172177
bv_get_rec(index, bv, i * sub_width, subtype));
173178
}

src/util/pointer_offset_size.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,9 @@ optionalt<exprt> get_subexpression_at_offset(
675675
{
676676
return get_subexpression_at_offset(
677677
index_exprt(
678-
expr, from_integer(offset_bytes / elem_size_bytes, index_type())),
678+
expr,
679+
from_integer(
680+
offset_bytes / elem_size_bytes, array_type.index_type())),
679681
offset_inside_elem,
680682
target_type_raw,
681683
ns);

src/util/std_types.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ void array_typet::check(const typet &type, const validation_modet vm)
2828
}
2929
}
3030

31+
const typet &array_typet::index_type() const
32+
{
33+
PRECONDITION(is_complete());
34+
return size().type();
35+
}
36+
3137
/// Return the sequence number of the component with given name.
3238
std::size_t struct_union_typet::component_number(
3339
const irep_idt &component_name) const
@@ -236,6 +242,11 @@ vector_typet::vector_typet(const typet &_subtype, const constant_exprt &_size)
236242
size() = _size;
237243
}
238244

245+
const typet &vector_typet::index_type() const
246+
{
247+
return size().type();
248+
}
249+
239250
const constant_exprt &vector_typet::size() const
240251
{
241252
return static_cast<const constant_exprt &>(find(ID_size));

src/util/std_types.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,15 @@ class array_typet:public type_with_subtypet
768768
add(ID_size, std::move(_size));
769769
}
770770

771+
/// The type of the index expressions into any instance of this type.
772+
const typet &index_type() const;
773+
774+
/// The type of the elements of the array.
775+
const typet &element_type() const
776+
{
777+
return subtype();
778+
}
779+
771780
const exprt &size() const
772781
{
773782
return static_cast<const exprt &>(find(ID_size));
@@ -976,6 +985,15 @@ class vector_typet:public type_with_subtypet
976985
public:
977986
vector_typet(const typet &_subtype, const constant_exprt &_size);
978987

988+
/// The type of any index expression into an instance of this type.
989+
const typet &index_type() const;
990+
991+
/// The type of the elements of the vector.
992+
const typet &element_type() const
993+
{
994+
return subtype();
995+
}
996+
979997
const constant_exprt &size() const;
980998
constant_exprt &size();
981999
};

0 commit comments

Comments
 (0)