Skip to content

Commit 90bcd52

Browse files
authored
Merge pull request #6552 from diffblue/index_type
introduce index_type and element_type for arrays and vectors
2 parents 7c44c10 + 5ba2935 commit 90bcd52

File tree

8 files changed

+73
-27
lines changed

8 files changed

+73
-27
lines changed

src/goto-instrument/goto_program2code.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ 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(),
409+
from_integer(i++, type.index_type()),
410+
type.element_type());
411411
convert_assign_rec(code_assignt(index, *it), dest);
412412
}
413413
}

src/goto-symex/field_sensitivity.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Author: Michael Tautschnig
99
#include "field_sensitivity.h"
1010

1111
#include <util/arith_tools.h>
12-
#include <util/c_types.h>
1312
#include <util/simplify_expr.h>
1413
#include <util/std_expr.h>
1514

@@ -204,7 +203,7 @@ exprt field_sensitivityt::get_fields(
204203

205204
for(std::size_t i = 0; i < array_size; ++i)
206205
{
207-
const index_exprt index(array, from_integer(i, index_type()));
206+
const index_exprt index(array, from_integer(i, type.index_type()));
208207
ssa_exprt tmp = ssa_expr;
209208
bool was_l2 = !tmp.get_level_2().empty();
210209
tmp.remove_level_2();
@@ -318,7 +317,7 @@ void field_sensitivityt::field_assignments_rec(
318317
exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
319318
for(std::size_t i = 0; i < array_size; ++i)
320319
{
321-
const index_exprt index_rhs(lhs, from_integer(i, index_type()));
320+
const index_exprt index_rhs(lhs, from_integer(i, type->index_type()));
322321
const exprt &index_lhs = *fs_it;
323322

324323
field_assignments_rec(

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ 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()));
201-
rhs = address_of_exprt(index_expr, pointer_type(array_type.subtype()));
200+
value_symbol.symbol_expr(), from_integer(0, array_type.index_type()));
201+
rhs = address_of_exprt(index_expr, pointer_type(array_type.element_type()));
202202
}
203203
else
204204
{
@@ -277,10 +277,11 @@ void goto_symext::symex_va_start(
277277
va_list_entry(parameter, to_pointer_type(lhs.type()), ns));
278278
}
279279
const std::size_t va_arg_size = va_arg_operands.size();
280-
exprt array =
281-
array_exprt{std::move(va_arg_operands),
282-
array_typet{pointer_type(empty_typet{}),
283-
from_integer(va_arg_size, size_type())}};
280+
281+
const auto array_type = array_typet{pointer_type(empty_typet{}),
282+
from_integer(va_arg_size, size_type())};
283+
284+
exprt array = array_exprt{std::move(va_arg_operands), array_type};
284285

285286
symbolt &va_array = get_fresh_aux_symbol(
286287
array.type(),
@@ -296,8 +297,8 @@ void goto_symext::symex_va_start(
296297
do_simplify(array);
297298
symex_assign(state, va_array.symbol_expr(), std::move(array));
298299

299-
exprt rhs = address_of_exprt{
300-
index_exprt{va_array.symbol_expr(), from_integer(0, index_type())}};
300+
exprt rhs = address_of_exprt{index_exprt{
301+
va_array.symbol_expr(), from_integer(0, array_type.index_type())}};
301302
rhs = state.rename(std::move(rhs), ns).get();
302303
symex_assign(state, lhs, typecast_exprt::conditional_cast(rhs, lhs.type()));
303304
}
@@ -517,8 +518,9 @@ void goto_symext::symex_cpp_new(
517518

518519
if(do_array)
519520
{
520-
rhs.add_to_operands(
521-
index_exprt(symbol.symbol_expr(), from_integer(0, index_type())));
521+
rhs.add_to_operands(index_exprt(
522+
symbol.symbol_expr(),
523+
from_integer(0, to_array_type(symbol.type).index_type())));
522524
}
523525
else
524526
rhs.copy_to_operands(symbol.symbol_expr());

src/solvers/flattening/boolbv_get.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ exprt boolbvt::bv_get_rec(
7070
{
7171
if(type.id()==ID_array)
7272
{
73+
const auto &array_type = to_array_type(type);
74+
7375
if(is_unbounded_array(type))
7476
return bv_get_unbounded_array(expr);
7577

76-
const typet &subtype=type.subtype();
78+
const typet &subtype = array_type.element_type();
7779
std::size_t sub_width=boolbv_width(subtype);
7880

7981
if(sub_width!=0)
@@ -86,7 +88,8 @@ exprt boolbvt::bv_get_rec(
8688
new_offset+=sub_width)
8789
{
8890
const index_exprt index{
89-
expr, from_integer(new_offset / sub_width, index_type())};
91+
expr,
92+
from_integer(new_offset / sub_width, array_type.index_type())};
9093
op.push_back(bv_get_rec(index, bv, offset + new_offset, subtype));
9194
}
9295

@@ -96,7 +99,7 @@ exprt boolbvt::bv_get_rec(
9699
}
97100
else
98101
{
99-
return array_exprt{{}, to_array_type(type)};
102+
return array_exprt{{}, array_type};
100103
}
101104
}
102105
else if(type.id()==ID_struct_tag)
@@ -157,20 +160,22 @@ exprt boolbvt::bv_get_rec(
157160
}
158161
else if(type.id()==ID_vector)
159162
{
160-
const typet &subtype = type.subtype();
161-
std::size_t sub_width=boolbv_width(subtype);
163+
const auto &vector_type = to_vector_type(type);
164+
const typet &element_type = vector_type.element_type();
165+
std::size_t element_width = boolbv_width(element_type);
162166

163-
if(sub_width!=0 && width%sub_width==0)
167+
if(element_width != 0 && width % element_width == 0)
164168
{
165-
std::size_t size=width/sub_width;
166-
vector_exprt value({}, to_vector_type(type));
169+
std::size_t size = width / element_width;
170+
vector_exprt value({}, vector_type);
167171
value.reserve_operands(size);
168172

169173
for(std::size_t i=0; i<size; i++)
170174
{
171-
const index_exprt index{expr, from_integer(i, index_type())};
175+
const index_exprt index{expr,
176+
from_integer(i, vector_type.index_type())};
172177
value.operands().push_back(
173-
bv_get_rec(index, bv, i * sub_width, subtype));
178+
bv_get_rec(index, bv, i * element_width, element_type));
174179
}
175180

176181
return std::move(value);

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_expr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,9 @@ inline notequal_exprt &to_notequal_expr(exprt &expr)
13271327
class index_exprt:public binary_exprt
13281328
{
13291329
public:
1330+
// When _array has array_type, the type of _index
1331+
// must be array_type.index_type().
1332+
// This will eventually be enforced using a precondition.
13301333
index_exprt(const exprt &_array, exprt _index)
13311334
: binary_exprt(_array, ID_index, std::move(_index), _array.type().subtype())
13321335
{

src/util/std_types.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include "std_types.h"
1414

15+
#include "c_types.h"
1516
#include "namespace.h"
1617
#include "std_expr.h"
1718

@@ -28,6 +29,12 @@ void array_typet::check(const typet &type, const validation_modet vm)
2829
}
2930
}
3031

32+
typet array_typet::index_type() const
33+
{
34+
// we may wish to make the index type part of the array type
35+
return ::index_type();
36+
}
37+
3138
/// Return the sequence number of the component with given name.
3239
std::size_t struct_union_typet::component_number(
3340
const irep_idt &component_name) const
@@ -236,6 +243,12 @@ vector_typet::vector_typet(const typet &_subtype, const constant_exprt &_size)
236243
size() = _size;
237244
}
238245

246+
typet vector_typet::index_type() const
247+
{
248+
// we may wish to make the index type part of the vector type
249+
return ::index_type();
250+
}
251+
239252
const constant_exprt &vector_typet::size() const
240253
{
241254
return static_cast<const constant_exprt &>(find(ID_size));

src/util/std_types.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,17 @@ 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+
typet index_type() const;
773+
774+
/// The type of the elements of the array.
775+
/// This method is preferred over .subtype(),
776+
/// which will eventually be deprecated.
777+
const typet &element_type() const
778+
{
779+
return subtype();
780+
}
781+
771782
const exprt &size() const
772783
{
773784
return static_cast<const exprt &>(find(ID_size));
@@ -976,6 +987,17 @@ class vector_typet:public type_with_subtypet
976987
public:
977988
vector_typet(const typet &_subtype, const constant_exprt &_size);
978989

990+
/// The type of any index expression into an instance of this type.
991+
typet index_type() const;
992+
993+
/// The type of the elements of the vector.
994+
/// This method is preferred over .subtype(),
995+
/// which will eventually be deprecated.
996+
const typet &element_type() const
997+
{
998+
return subtype();
999+
}
1000+
9791001
const constant_exprt &size() const;
9801002
constant_exprt &size();
9811003
};

0 commit comments

Comments
 (0)