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  const union_typet &union_op_type =
22  ns.follow_tag(to_union_tag_type(union_op.type()));
23 
24  const irep_idt &component_name = expr.get_component_name();
26  union_op_type.get_component(component_name);
28  component.is_not_nil(),
29  "union type shall contain component accessed by member expression",
30  expr.find_source_location(),
31  id2string(component_name));
32 
33  const typet &subtype = component.type();
34  const std::size_t sub_width = boolbv.boolbv_width(subtype);
35 
36  endianness_mapt map_u = boolbv.endianness_map(union_op_type);
37  endianness_mapt map_component = boolbv.endianness_map(subtype);
38 
39  bvt result(sub_width, literalt{});
40  for(std::size_t i = 0; i < sub_width; ++i)
41  result[map_u.map_bit(i)] = union_bv[map_component.map_bit(i)];
42 
43  return result;
44 }
45 
47 {
48  const bvt &compound_bv = convert_bv(expr.compound());
49 
50  const typet &compound_type = expr.compound().type();
51 
52  if(compound_type.id() == ID_struct_tag || compound_type.id() == ID_struct)
53  {
54  const struct_typet &struct_op_type =
55  compound_type.id() == ID_struct_tag
56  ? ns.follow_tag(to_struct_tag_type(compound_type))
57  : to_struct_type(compound_type);
58 
59  const auto &member_bits =
60  bv_width.get_member(struct_op_type, expr.get_component_name());
61 
62  INVARIANT(
63  member_bits.offset + member_bits.width <= compound_bv.size(),
64  "bitvector part corresponding to element shall be contained within the "
65  "full aggregate bitvector");
66 
67  return bvt(
68  compound_bv.begin() + member_bits.offset,
69  compound_bv.begin() + member_bits.offset + member_bits.width);
70  }
71  else
72  {
74  compound_type.id() == ID_union_tag || compound_type.id() == ID_union);
75  return convert_member_union(expr, compound_bv, *this, ns);
76  }
77 }
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 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:37
virtual bvt convert_member(const member_exprt &expr)
boolbv_widtht bv_width
Definition: boolbv.h:115
virtual endianness_mapt endianness_map(const typet &type, bool little_endian) const
Definition: boolbv.h:107
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:101
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:384
Extract member of struct or union.
Definition: std_expr.h:2841
const exprt & compound() const
Definition: std_expr.h:2890
irep_idt get_component_name() const
Definition: std_expr.h:2863
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:63
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:40
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:80
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