12
12
#include < util/byte_operators.h>
13
13
#include < util/arith_tools.h>
14
14
#include < util/c_types.h>
15
+ #include < util/invariant.h>
15
16
16
17
bvt boolbvt::convert_member (const member_exprt &expr)
17
18
{
@@ -20,67 +21,34 @@ bvt boolbvt::convert_member(const member_exprt &expr)
20
21
21
22
const bvt &struct_bv=convert_bv (struct_op);
22
23
23
- if (struct_op_type.id ()==ID_union)
24
- {
25
- return convert_bv (
26
- byte_extract_exprt (byte_extract_id (),
27
- struct_op,
28
- from_integer (0 , index_type ()),
29
- expr.type ()));
30
- }
31
- else if (struct_op_type.id ()==ID_struct)
32
- {
33
- const irep_idt &component_name=expr.get_component_name ();
34
- const struct_typet::componentst &components=
35
- to_struct_type (struct_op_type).components ();
36
-
37
- std::size_t offset=0 ;
38
-
39
- for (struct_typet::componentst::const_iterator
40
- it=components.begin ();
41
- it!=components.end ();
42
- it++)
43
- {
44
- const typet &subtype=it->type ();
45
- std::size_t sub_width=boolbv_width (subtype);
46
-
47
- if (it->get_name ()==component_name)
48
- {
49
- if (!base_type_eq (subtype, expr.type (), ns))
50
- {
51
- #if 0
52
- std::cout << "DEBUG " << expr.pretty() << "\n";
53
- #endif
24
+ const irep_idt &component_name = expr.get_component_name ();
25
+ const struct_union_typet::componentst &components =
26
+ to_struct_union_type (struct_op_type).components ();
54
27
55
- error ().source_location =expr.find_source_location ();
56
- error () << " member: component type does not match: "
57
- << subtype.pretty () << " vs. "
58
- << expr.type ().pretty () << eom;
59
- throw 0 ;
60
- }
28
+ std::size_t offset = 0 ;
61
29
62
- bvt bv;
63
- bv.resize (sub_width);
64
- assert (offset+sub_width<=struct_bv.size ());
65
-
66
- for (std::size_t i=0 ; i<sub_width; i++)
67
- bv[i]=struct_bv[offset+i];
68
-
69
- return bv;
70
- }
30
+ for (const auto &comp : components)
31
+ {
32
+ const typet &subtype = comp.type ();
33
+ const std::size_t sub_width = boolbv_width (subtype);
71
34
72
- offset+=sub_width;
35
+ if (comp.get_name () == component_name)
36
+ {
37
+ DATA_INVARIANT (
38
+ base_type_eq (subtype, expr.type (), ns),
39
+ " component type expected to match expression type:\n " + expr.pretty ());
40
+ DATA_INVARIANT (
41
+ offset + sub_width <= struct_bv.size (), " member access outside struct" );
42
+
43
+ return
44
+ bvt (struct_bv.begin () + offset, struct_bv.begin () + offset + sub_width);
73
45
}
74
46
75
- error ().source_location =expr.find_source_location ();
76
- error () << " component " << component_name
77
- << " not found in structure" << eom;
78
- throw 0 ;
79
- }
80
- else
81
- {
82
- error ().source_location =expr.find_source_location ();
83
- error () << " member takes struct or union operand" << eom;
84
- throw 0 ;
47
+ if (struct_op_type.id () == ID_struct)
48
+ offset += sub_width;
85
49
}
50
+
51
+ error ().source_location = expr.find_source_location ();
52
+ error () << " component " << component_name << " not found in structure" << eom;
53
+ throw 0 ;
86
54
}
0 commit comments