@@ -12,6 +12,7 @@ Author: Daniel Kroening, kroening@kroening.com
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,41 @@ 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 ();
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 ();
36
27
37
- std::size_t offset= 0 ;
28
+ std::size_t offset = 0 ;
38
29
39
- for (struct_typet::componentst::const_iterator
40
- it=components.begin ();
41
- it!=components.end ();
42
- it++)
30
+ for (struct_union_typet::componentst::const_iterator it = components.begin ();
31
+ it != components.end ();
32
+ ++it)
33
+ {
34
+ const typet &subtype = it->type ();
35
+ const std::size_t sub_width = boolbv_width (subtype);
36
+
37
+ if (it->get_name () == component_name)
43
38
{
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
54
-
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
- }
61
-
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
- }
71
-
72
- offset+=sub_width;
39
+ DATA_INVARIANT (
40
+ base_type_eq (subtype, expr.type (), ns),
41
+ " component type expected to match expression type:\n " + expr.pretty ());
42
+
43
+ bvt bv;
44
+ bv.resize (sub_width);
45
+ DATA_INVARIANT (
46
+ offset + sub_width <= struct_bv.size (), " member access outside struct" );
47
+
48
+ for (std::size_t i = 0 ; i < sub_width; i++)
49
+ bv[i] = struct_bv[offset + i];
50
+
51
+ return bv;
73
52
}
74
53
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 ;
54
+ if (struct_op_type.id () == ID_struct)
55
+ offset += sub_width;
85
56
}
57
+
58
+ error ().source_location = expr.find_source_location ();
59
+ error () << " component " << component_name << " not found in structure" << eom;
60
+ throw 0 ;
86
61
}
0 commit comments