cprover
boolbv_array_of.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "boolbv.h"
10 
11 #include <util/arith_tools.h>
12 #include <util/invariant.h>
13 #include <util/std_types.h>
14 
16 {
18  expr.type().id() == ID_array, "array_of expression shall have array type");
19 
20  const array_typet &array_type=to_array_type(expr.type());
21 
22  if(is_unbounded_array(array_type))
23  return conversion_failed(expr);
24 
25  std::size_t width=boolbv_width(array_type);
26 
27  if(width==0)
28  {
29  // A zero-length array is acceptable;
30  // an element with unknown size is not.
31  if(boolbv_width(array_type.subtype())==0)
32  return conversion_failed(expr);
33  else
34  return bvt();
35  }
36 
37  const exprt &array_size=array_type.size();
38 
39  mp_integer size;
40 
41  if(to_integer(array_size, size))
42  return conversion_failed(expr);
43 
44  const bvt &tmp=convert_bv(expr.op0());
45 
46  INVARIANT(
47  size * tmp.size() == width,
48  "total array bit width shall equal the number of elements times the "
49  "element bit with");
50 
51  bvt bv;
52  bv.resize(width);
53 
54  auto b_it = tmp.begin();
55 
56  for(auto &b : bv)
57  {
58  b = *b_it;
59 
60  b_it++;
61 
62  if(b_it == tmp.end())
63  b_it = tmp.begin();
64  }
65 
66  return bv;
67 }
BigInt mp_integer
Definition: mp_arith.h:22
virtual bvt convert_array_of(const array_of_exprt &expr)
exprt & op0()
Definition: expr.h:84
boolbv_widtht boolbv_width
Definition: boolbv.h:92
bool is_unbounded_array(const typet &type) const override
Definition: boolbv.cpp:624
typet & type()
Return the type of the expression.
Definition: expr.h:68
virtual const bvt & convert_bv(const exprt &expr, const optionalt< std::size_t > expected_width=nullopt)
Definition: boolbv.cpp:112
const irep_idt & id() const
Definition: irep.h:259
void conversion_failed(const exprt &expr, bvt &bv)
Definition: boolbv.h:110
Array constructor from single element.
Definition: std_expr.h:1678
Pre-defined types.
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:1048
Base class for all expressions.
Definition: expr.h:54
Arrays with given size.
Definition: std_types.h:1000
bool to_integer(const exprt &expr, mp_integer &int_value)
Definition: arith_tools.cpp:19
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:485
std::vector< literalt > bvt
Definition: literal.h:200