CBMC
boolbv_struct.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/namespace.h>
12 
14 {
15  const struct_typet &struct_type =
16  expr.type().id() == ID_struct_tag
18  : to_struct_type(expr.type());
19 
20  std::size_t width=boolbv_width(struct_type);
21 
22  const struct_typet::componentst &components=struct_type.components();
23 
25  expr.operands().size() == components.size(),
26  "number of operands of a struct expression shall equal the number of"
27  "components as indicated by its type",
28  expr.find_source_location());
29 
30  bvt bv;
31  bv.resize(width);
32 
33  std::size_t bit_idx = 0;
34 
35  exprt::operandst::const_iterator op_it=expr.operands().begin();
36  for(const auto &comp : components)
37  {
38  const typet &subtype=comp.type();
39  const exprt &op=*op_it;
40 
42  subtype == op.type(),
43  "type of a struct expression operand shall equal the type of the "
44  "corresponding struct component",
45  expr.find_source_location(),
46  subtype.pretty(),
47  op.type().pretty());
48 
49  std::size_t subtype_width=boolbv_width(subtype);
50 
51  if(subtype_width!=0)
52  {
53  const bvt &op_bv = convert_bv(op, subtype_width);
54 
55  INVARIANT(
56  bit_idx + op_bv.size() <= width, "bit index shall be within bounds");
57 
58  for(const auto &bit : op_bv)
59  {
60  bv[bit_idx] = bit;
61  bit_idx++;
62  }
63  }
64 
65  ++op_it;
66  }
67 
68  INVARIANT(
69  bit_idx == width, "all bits in the bitvector shall have been assigned");
70 
71  return bv;
72 }
const namespacet & ns
Definition: arrays.h:56
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:39
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:102
virtual bvt convert_struct(const struct_exprt &expr)
Base class for all expressions.
Definition: expr.h:56
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition: expr.cpp:147
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:482
const irep_idt & id() const
Definition: irep.h:388
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:63
Struct constructor from list of elements.
Definition: std_expr.h:1877
Structure type, corresponds to C style structs.
Definition: std_types.h:231
const componentst & components() const
Definition: std_types.h:147
std::vector< componentt > componentst
Definition: std_types.h:140
The type of an expression, extends irept.
Definition: type.h:29
std::vector< literalt > bvt
Definition: literal.h:201
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:535
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:518