CBMC
boolbv_member.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/c_types.h>
12 #include <util/namespace.h>
13 
15  const member_exprt &expr,
16  const bvt &union_bv,
17  const boolbvt &boolbv,
18  const namespacet &ns)
19 {
20  const exprt &union_op = expr.compound();
21 
22  const union_typet &union_op_type =
23  union_op.type().id() == ID_union_tag
24  ? ns.follow_tag(to_union_tag_type(union_op.type()))
25  : to_union_type(union_op.type());
26 
27  const irep_idt &component_name = expr.get_component_name();
29  union_op_type.get_component(component_name);
31  component.is_not_nil(),
32  "union type shall contain component accessed by member expression",
33  expr.find_source_location(),
34  id2string(component_name));
35 
36  const typet &subtype = component.type();
37  const std::size_t sub_width = boolbv.boolbv_width(subtype);
38 
39  endianness_mapt map_u = boolbv.endianness_map(union_op_type);
40  endianness_mapt map_component = boolbv.endianness_map(subtype);
41 
42  bvt result(sub_width, literalt{});
43  for(std::size_t i = 0; i < sub_width; ++i)
44  result[map_u.map_bit(i)] = union_bv[map_component.map_bit(i)];
45 
46  return result;
47 }
48 
50 {
51  const bvt &compound_bv = convert_bv(expr.compound());
52 
53  const typet &compound_type = expr.compound().type();
54 
55  if(compound_type.id() == ID_struct_tag || compound_type.id() == ID_struct)
56  {
57  const struct_typet &struct_op_type =
58  compound_type.id() == ID_struct_tag
59  ? ns.follow_tag(to_struct_tag_type(compound_type))
60  : to_struct_type(compound_type);
61 
62  const auto &member_bits =
63  bv_width.get_member(struct_op_type, expr.get_component_name());
64 
65  INVARIANT(
66  member_bits.offset + member_bits.width <= compound_bv.size(),
67  "bitvector part corresponding to element shall be contained within the "
68  "full aggregate bitvector");
69 
70  return bvt(
71  compound_bv.begin() + member_bits.offset,
72  compound_bv.begin() + member_bits.offset + member_bits.width);
73  }
74  else
75  {
77  compound_type.id() == ID_union_tag || compound_type.id() == ID_union);
78  return convert_member_union(expr, compound_bv, *this, ns);
79  }
80 }
static bvt convert_member_union(const member_exprt &expr, const bvt &union_bv, const boolbvt &boolbv, const namespacet &ns)
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition: c_types.h:224
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition: c_types.h:184
const namespacet & ns
Definition: arrays.h:56
const membert & get_member(const struct_typet &type, const irep_idt &member) const
Definition: boolbv.h:46
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 bvt convert_member(const member_exprt &expr)
boolbv_widtht bv_width
Definition: boolbv.h:116
virtual endianness_mapt endianness_map(const typet &type, bool little_endian) const
Definition: boolbv.h:108
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:102
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Maps a big-endian offset to a little-endian offset.
size_t map_bit(size_t bit) const
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
const irep_idt & id() const
Definition: irep.h:388
Extract member of struct or union.
Definition: std_expr.h:2844
const exprt & compound() const
Definition: std_expr.h:2893
irep_idt get_component_name() const
Definition: std_expr.h:2866
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:63
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
Structure type, corresponds to C style structs.
Definition: std_types.h:231
const componentt & get_component(const irep_idt &component_name) const
Get the reference to a component with given name.
Definition: std_types.cpp:64
The type of an expression, extends irept.
Definition: type.h:29
The union type.
Definition: c_types.h:147
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
std::vector< literalt > bvt
Definition: literal.h:201
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:535
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition: std_expr.cpp:97
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