21
21
22
22
#include " pointer_offset_size.h"
23
23
24
+ member_offset_iterator::member_offset_iterator (const struct_typet _type,
25
+ const namespacet _ns) :
26
+ type(_type),
27
+ ns(_ns),
28
+ bit_field_bits(0 ),
29
+ current({0 ,0 })
30
+ {
31
+ }
32
+
33
+ member_offset_iterator& member_offset_iterator::operator ++()
34
+ {
35
+ if (current.second !=-1 ) // Already failed?
36
+ {
37
+ const auto & comp=type.components ()[current.first ];
38
+ if (comp.type ().id ()==ID_c_bit_field)
39
+ {
40
+ // take the extra bytes needed
41
+ std::size_t w=to_c_bit_field_type (comp.type ()).get_width ();
42
+ for (; w>bit_field_bits; ++current.second , bit_field_bits+=8 );
43
+ bit_field_bits-=w;
44
+ }
45
+ else
46
+ {
47
+ const typet &subtype=comp.type ();
48
+ mp_integer sub_size=pointer_offset_size (subtype, ns);
49
+ if (sub_size==-1 ) current.second =-1 ; // give up
50
+ else current.second +=sub_size;
51
+ }
52
+ }
53
+ ++current.first ;
54
+ return *this ;
55
+ }
56
+
24
57
/* ******************************************************************\
25
58
26
59
Function: member_offset
@@ -39,35 +72,18 @@ mp_integer member_offset(
39
72
const namespacet &ns)
40
73
{
41
74
const struct_typet::componentst &components=type.components ();
42
-
43
- mp_integer result=0 ;
44
- std::size_t bit_field_bits=0 ;
75
+ member_offset_iterator offsets (type,ns);
45
76
46
77
for (struct_typet::componentst::const_iterator
47
78
it=components.begin ();
48
- it!=components.end ();
49
- it++ )
79
+ it!=components.end () && offsets-> second !=- 1 ;
80
+ ++it, ++offsets )
50
81
{
51
82
if (it->get_name ()==member)
52
83
break ;
53
-
54
- if (it->type ().id ()==ID_c_bit_field)
55
- {
56
- // take the extra bytes needed
57
- std::size_t w=to_c_bit_field_type (it->type ()).get_width ();
58
- for (; w>bit_field_bits; ++result, bit_field_bits+=8 );
59
- bit_field_bits-=w;
60
- }
61
- else
62
- {
63
- const typet &subtype=it->type ();
64
- mp_integer sub_size=pointer_offset_size (subtype, ns);
65
- if (sub_size==-1 ) return -1 ; // give up
66
- result+=sub_size;
67
- }
68
84
}
69
85
70
- return result ;
86
+ return offsets-> second ;
71
87
}
72
88
73
89
/* ******************************************************************\
0 commit comments