2 # Copyright 2002 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 use output qw($output);
30 my $class = ref($proto) || $proto;
41 sub set_find_align_callback($$)
43 my ($self, $find_align) = @_;
44 $self->{FIND_ALIGN} = $find_align;
47 sub set_find_kind_callback($$)
49 my ($self, $find_kind) = @_;
50 $self->{FIND_KIND} = $find_kind;
53 sub set_find_size_callback($$)
55 my ($self, $find_size) = @_;
56 $self->{FIND_SIZE} = $find_size;
59 sub set_find_count_callback($$)
61 my ($self, $find_count) = @_;
62 $self->{FIND_COUNT} = $find_count;
67 # Property setter / getter functions (each does both)
72 my ($self, $kind) = @_;
75 $self->{KIND} = $kind;
78 $self->_refresh() if (!defined $self->{KIND});
84 my ($self, $_name) = @_;
87 $self->{_NAME} = $_name;
90 return $self->{_NAME};
95 my ($self, $name) = @_;
98 $self->{NAME} = $name;
101 return $self->{NAME} if ($self->{NAME});
102 return "$self->{KIND} $self->{_NAME}";
107 my ($self, $pack) = @_;
110 $self->{PACK} = $pack;
113 return $self->{PACK};
120 return $self->{ALIGN};
127 my $count = $self->field_count;
130 for (my $n = 0; $n < $count; $n++) {
131 my $field = 'c_type_field'->new($self, $n);
132 push @fields, $field;
137 sub field_base_sizes($)
141 return $self->{FIELD_BASE_SIZES};
148 return $self->{FIELD_ALIGNS};
154 return scalar @{$self->{FIELD_TYPE_NAMES}};
159 my ($self, $field_names) = @_;
160 if (defined $field_names)
162 $self->{FIELD_NAMES} = $field_names;
165 return $self->{FIELD_NAMES};
172 return $self->{FIELD_OFFSETS};
179 return $self->{FIELD_SIZES};
182 sub field_type_names($;$)
184 my ($self, $field_type_names) = @_;
185 if (defined $field_type_names)
187 $self->{FIELD_TYPE_NAMES} = $field_type_names;
190 return $self->{FIELD_TYPE_NAMES};
197 return $self->{SIZE};
203 return if (!$self->{DIRTY});
205 my $pack = $self->pack;
206 $pack = 8 if !defined($pack);
208 my $max_field_align = 0;
211 my $bitfield_size = 0;
212 my $bitfield_bits = 0;
215 foreach my $field ($self->fields())
217 my $type_name = $field->type_name;
221 if ($type_name =~ s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/)
227 if ($type_name =~ s/\s+DECLSPEC_ALIGN\((\d+)\)//)
231 my $base_size = $self->{FIND_SIZE}($type_name);
232 my $type_size=$base_size;
235 $count=$self->{FIND_COUNT}($count) if ($count !~ /^\d+$/);
242 print STDERR "$type_name -> type_size=undef, count=$count\n" if (!defined $type_size);
243 $type_size *= int($count);
246 if ($bitfield_size != 0)
248 if (($type_name eq "" and defined $bits and $bits == 0) or
249 (defined $type_size and $bitfield_size != $type_size) or
251 $bitfield_bits + $bits > 8 * $bitfield_size)
253 # This marks the end of the previous bitfield
259 $bitfield_bits+=$bits;
265 $self->{ALIGN} = $self->{FIND_ALIGN}($type_name);
266 $self->{ALIGN} = $declspec_align if (defined $declspec_align);
268 if (defined $self->{ALIGN})
270 $self->{ALIGN} = $pack if ($self->{ALIGN} > $pack);
271 $max_field_align = $self->{ALIGN} if ($self->{ALIGN}) > $max_field_align;
273 if ($offset % $self->{ALIGN} != 0) {
274 $offset = (int($offset / $self->{ALIGN}) + 1) * $self->{ALIGN};
278 if ($self->{KIND} !~ /^(?:struct|union)$/)
280 $self->{KIND} = $self->{FIND_KIND}($type_name) || "";
285 $self->{ALIGN} = undef;
286 $self->{SIZE} = undef;
290 $self->{FIELD_ALIGNS}->[$n] = $self->{ALIGN};
291 $self->{FIELD_BASE_SIZES}->[$n] = $base_size;
292 $self->{FIELD_OFFSETS}->[$n] = $offset;
293 $self->{FIELD_SIZES}->[$n] = $type_size;
294 $offset += $type_size;
298 $bitfield_size=$type_size;
299 $bitfield_bits=$bits;
304 $self->{ALIGN} = $pack;
305 $self->{ALIGN} = $max_field_align if ($max_field_align < $pack);
307 $self->{SIZE} = $offset;
308 if ($self->{KIND} =~ /^(?:struct|union)$/) {
309 if ($self->{SIZE} % $self->{ALIGN} != 0) {
310 $self->{SIZE} = (int($self->{SIZE} / $self->{ALIGN}) + 1) * $self->{ALIGN};
317 package c_type_field;
321 my ($proto, $type, $number) = @_;
322 my $class = ref($proto) || $proto;
323 my $self = {TYPE=> $type,
333 return undef unless defined $self->{TYPE}->field_aligns();
334 return $self->{TYPE}->field_aligns()->[$self->{NUMBER}];
340 return undef unless defined $self->{TYPE}->field_base_sizes();
341 return $self->{TYPE}->field_base_sizes()->[$self->{NUMBER}];
347 return undef unless defined $self->{TYPE}->field_names();
348 return $self->{TYPE}->field_names()->[$self->{NUMBER}];
354 return undef unless defined $self->{TYPE}->field_offsets();
355 return $self->{TYPE}->field_offsets()->[$self->{NUMBER}];
361 return undef unless defined $self->{TYPE}->field_sizes();
362 return $self->{TYPE}->field_sizes()->[$self->{NUMBER}];
368 return $self->{TYPE}->field_type_names()->[$self->{NUMBER}];