CBMC
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 
17 {
19  expr.type().id() == ID_array, "array_of expression shall have array type");
20 
21  const array_typet &array_type = expr.type();
22 
23  if(is_unbounded_array(array_type))
24  return conversion_failed(expr);
25 
26  std::size_t width=boolbv_width(array_type);
27  if(width == 0)
28  return bvt{};
29 
30  const exprt &array_size=array_type.size();
31 
32  const auto size = numeric_cast_v<mp_integer>(to_constant_expr(array_size));
33 
34  const bvt &tmp = convert_bv(expr.what());
35 
36  INVARIANT(
37  size * tmp.size() == width,
38  "total array bit width shall equal the number of elements times the "
39  "element bit with");
40 
41  bvt bv;
42  bv.resize(width);
43 
44  auto b_it = tmp.begin();
45 
46  for(auto &b : bv)
47  {
48  b = *b_it;
49 
50  b_it++;
51 
52  if(b_it == tmp.end())
53  b_it = tmp.begin();
54  }
55 
56  return bv;
57 }
Array constructor from single element.
Definition: std_expr.h:1553
exprt & what()
Definition: std_expr.h:1570
const array_typet & type() const
Definition: std_expr.h:1560
Arrays with given size.
Definition: std_types.h:807
const exprt & size() const
Definition: std_types.h:840
virtual const bvt & convert_bv(const exprt &expr, const std::optional< std::size_t > expected_width={})
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:37
bool is_unbounded_array(const typet &type) const override
Definition: boolbv.cpp:518
virtual bvt convert_array_of(const array_of_exprt &expr)
Flatten arrays constructed from a single element.
bvt conversion_failed(const exprt &expr)
Print that the expression of x has failed conversion, then return a vector of x's width.
Definition: boolbv.cpp:83
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:101
Base class for all expressions.
Definition: expr.h:56
const irep_idt & id() const
Definition: irep.h:384
std::vector< literalt > bvt
Definition: literal.h:201
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:534
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:3037
Pre-defined types.