cprover
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 
14  const member_exprt &expr,
15  const bvt &struct_bv,
16  const std::function<std::size_t(const typet &)> boolbv_width,
17  const namespacet &ns)
18 {
19  const exprt &struct_op=expr.struct_op();
20  const typet &struct_op_type=ns.follow(struct_op.type());
21 
22  const irep_idt &component_name = expr.get_component_name();
23  const struct_typet::componentst &components =
24  to_struct_type(struct_op_type).components();
25 
26  std::size_t offset = 0;
27 
28  for(const auto &c : components)
29  {
30  const typet &subtype = c.type();
31  const std::size_t sub_width = boolbv_width(subtype);
32 
33  if(c.get_name() == component_name)
34  {
36  subtype == expr.type(),
37  "component type shall match the member expression type",
38  subtype.pretty(),
39  expr.type().pretty());
40  INVARIANT(
41  offset + sub_width <= struct_bv.size(),
42  "bitvector part corresponding to element shall be contained within the "
43  "full aggregate bitvector");
44 
45  return bvt(
46  struct_bv.begin() + offset, struct_bv.begin() + offset + sub_width);
47  }
48 
49  offset += sub_width;
50  }
51 
53  false,
54  "struct type shall contain component accessed by member expression",
55  expr.find_source_location(),
56  id2string(component_name));
57 }
58 
60  const member_exprt &expr,
61  const bvt &union_bv,
62  const boolbvt &boolbv,
63  const namespacet &ns)
64 {
65  const exprt &union_op = expr.compound();
66  const union_typet &union_op_type =
67  ns.follow_tag(to_union_tag_type(union_op.type()));
68 
69  const irep_idt &component_name = expr.get_component_name();
71  union_op_type.get_component(component_name);
73  component.is_not_nil(),
74  "union type shall contain component accessed by member expression",
75  expr.find_source_location(),
76  id2string(component_name));
77 
78  const typet &subtype = component.type();
79  const std::size_t sub_width = boolbv.boolbv_width(subtype);
80 
81  endianness_mapt map_u = boolbv.endianness_map(union_op_type);
82  endianness_mapt map_component = boolbv.endianness_map(subtype);
83 
84  bvt result(sub_width, literalt{});
85  for(std::size_t i = 0; i < sub_width; ++i)
86  result[map_u.map_bit(i)] = union_bv[map_component.map_bit(i)];
87 
88  return result;
89 }
90 
92 {
93  const bvt &compound_bv = convert_bv(expr.compound());
94 
95  if(expr.compound().type().id() == ID_struct_tag)
96  return convert_member_struct(
97  expr,
98  compound_bv,
99  [this](const typet &t) { return boolbv_width(t); },
100  ns);
101  else
102  {
103  PRECONDITION(expr.compound().type().id() == ID_union_tag);
104  return convert_member_union(expr, compound_bv, *this, ns);
105  }
106 }
static bvt convert_member_union(const member_exprt &expr, const bvt &union_bv, const boolbvt &boolbv, const namespacet &ns)
static bvt convert_member_struct(const member_exprt &expr, const bvt &struct_bv, const std::function< std::size_t(const typet &)> boolbv_width, 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:189
const namespacet & ns
Definition: arrays.h:54
Definition: boolbv.h:42
virtual const bvt & convert_bv(const exprt &expr, const optionalt< std::size_t > expected_width=nullopt)
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:40
virtual bvt convert_member(const member_exprt &expr)
virtual endianness_mapt endianness_map(const typet &type, bool little_endian) const
Definition: boolbv.h:103
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:97
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
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:54
const source_locationt & find_source_location() const
Get a source_locationt from the expression or from its operands (non-recursively).
Definition: expr.cpp:165
typet & type()
Return the type of the expression.
Definition: expr.h:82
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:495
const irep_idt & id() const
Definition: irep.h:407
Extract member of struct or union.
Definition: std_expr.h:2613
const exprt & compound() const
Definition: std_expr.h:2654
const exprt & struct_op() const
Definition: std_expr.h:2643
irep_idt get_component_name() const
Definition: std_expr.h:2627
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
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:91
const componentt & get_component(const irep_idt &component_name) const
Get the reference to a component with given name.
Definition: std_types.cpp:49
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:28
The union type.
Definition: c_types.h:112
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
std::vector< literalt > bvt
Definition: literal.h:201
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
#define INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Same as invariant, with one or more diagnostics attached Diagnostics can be of any type that has a sp...
Definition: invariant.h:437
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:511
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition: std_expr.cpp:48
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308