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
24 use output
qw($output);
31 my $class = ref($proto) || $proto;
42 sub set_find_align_callback($$)
44 my ($self, $find_align) = @_;
45 $self->{FIND_ALIGN} = $find_align;
48 sub set_find_kind_callback($$)
50 my ($self, $find_kind) = @_;
51 $self->{FIND_KIND} = $find_kind;
54 sub set_find_size_callback($$)
56 my ($self, $find_size) = @_;
57 $self->{FIND_SIZE} = $find_size;
60 sub set_find_count_callback($$)
62 my ($self, $find_count) = @_;
63 $self->{FIND_COUNT} = $find_count;
68 # Property setter / getter functions (each does both)
73 my ($self, $kind) = @_;
76 $self->{KIND} = $kind;
79 $self->_refresh() if (!defined $self->{KIND});
85 my ($self, $_name) = @_;
88 $self->{_NAME} = $_name;
91 return $self->{_NAME};
96 my ($self, $name) = @_;
99 $self->{NAME} = $name;
102 return $self->{NAME} if ($self->{NAME});
103 return "$self->{KIND} $self->{_NAME}";
108 my ($self, $pack) = @_;
111 $self->{PACK} = $pack;
114 return $self->{PACK};
121 return $self->{ALIGN};
128 my $count = $self->field_count;
131 for (my $n = 0; $n < $count; $n++) {
132 my $field = 'c_type_field'->new($self, $n);
133 push @fields, $field;
138 sub field_base_sizes($)
142 return $self->{FIELD_BASE_SIZES};
149 return $self->{FIELD_ALIGNS};
155 return scalar @{$self->{FIELD_TYPE_NAMES}};
160 my ($self, $field_names) = @_;
161 if (defined $field_names)
163 $self->{FIELD_NAMES} = $field_names;
166 return $self->{FIELD_NAMES};
173 return $self->{FIELD_OFFSETS};
180 return $self->{FIELD_SIZES};
183 sub field_type_names($;$)
185 my ($self, $field_type_names) = @_;
186 if (defined $field_type_names)
188 $self->{FIELD_TYPE_NAMES} = $field_type_names;
191 return $self->{FIELD_TYPE_NAMES};
198 return $self->{SIZE};
204 return if (!$self->{DIRTY});
206 my $pack = $self->pack;
207 $pack = 8 if !defined($pack);
209 my $max_field_align = 0;
212 my $bitfield_size = 0;
213 my $bitfield_bits = 0;
216 foreach my $field ($self->fields())
218 my $type_name = $field->type_name;
222 if ($type_name =~ s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/)
228 if ($type_name =~ s/\s+DECLSPEC_ALIGN\((\d+)\)//)
232 my $base_size = $self->{FIND_SIZE}($type_name);
233 my $type_size=$base_size;
236 $count=$self->{FIND_COUNT}($count) if ($count !~ /^\d+$/);
243 if (!defined $type_size)
245 print STDERR "$type_name -> type_size=undef, count=$count\n";
249 $type_size *= int($count);
253 if ($bitfield_size != 0)
255 if (($type_name eq "" and defined $bits and $bits == 0) or
256 (defined $type_size and $bitfield_size != $type_size) or
258 $bitfield_bits + $bits > 8 * $bitfield_size)
260 # This marks the end of the previous bitfield
266 $bitfield_bits+=$bits;
272 $self->{ALIGN} = $self->{FIND_ALIGN}($type_name);
273 $self->{ALIGN} = $declspec_align if (defined $declspec_align);
275 if (defined $self->{ALIGN})
277 $self->{ALIGN} = $pack if ($self->{ALIGN} > $pack);
278 $max_field_align = $self->{ALIGN} if ($self->{ALIGN}) > $max_field_align;
280 if ($offset % $self->{ALIGN} != 0) {
281 $offset = (int($offset / $self->{ALIGN}) + 1) * $self->{ALIGN};
285 if ($self->{KIND} !~ /^(?:struct|union)$/)
287 $self->{KIND} = $self->{FIND_KIND}($type_name) || "";
292 $self->{ALIGN} = undef;
293 $self->{SIZE} = undef;
297 $self->{FIELD_ALIGNS}->[$n] = $self->{ALIGN};
298 $self->{FIELD_BASE_SIZES}->[$n] = $base_size;
299 $self->{FIELD_OFFSETS}->[$n] = $offset;
300 $self->{FIELD_SIZES}->[$n] = $type_size;
301 $offset += $type_size;
305 $bitfield_size=$type_size;
306 $bitfield_bits=$bits;
311 $self->{ALIGN} = $pack;
312 $self->{ALIGN} = $max_field_align if ($max_field_align < $pack);
314 $self->{SIZE} = $offset;
315 if ($self->{KIND} =~ /^(?:struct|union)$/) {
316 if ($self->{SIZE} % $self->{ALIGN} != 0) {
317 $self->{SIZE} = (int($self->{SIZE} / $self->{ALIGN}) + 1) * $self->{ALIGN};
324 package c_type_field;
328 my ($proto, $type, $number) = @_;
329 my $class = ref($proto) || $proto;
330 my $self = {TYPE=> $type,
340 return undef unless defined $self->{TYPE}->field_aligns();
341 return $self->{TYPE}->field_aligns()->[$self->{NUMBER}];
347 return undef unless defined $self->{TYPE}->field_base_sizes();
348 return $self->{TYPE}->field_base_sizes()->[$self->{NUMBER}];
354 return undef unless defined $self->{TYPE}->field_names();
355 return $self->{TYPE}->field_names()->[$self->{NUMBER}];
361 return undef unless defined $self->{TYPE}->field_offsets();
362 return $self->{TYPE}->field_offsets()->[$self->{NUMBER}];
368 return undef unless defined $self->{TYPE}->field_sizes();
369 return $self->{TYPE}->field_sizes()->[$self->{NUMBER}];
375 return $self->{TYPE}->field_type_names()->[$self->{NUMBER}];