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);
29 my $class = ref($proto) || $proto;
31 bless ($self, $class);
36 ########################################################################
37 # set_find_align_callback
39 sub set_find_align_callback($$) {
42 my $find_align = \${$self->{FIND_ALIGN}};
47 ########################################################################
48 # set_find_kind_callback
50 sub set_find_kind_callback($$) {
53 my $find_kind = \${$self->{FIND_KIND}};
58 ########################################################################
59 # set_find_size_callback
61 sub set_find_size_callback($$) {
64 my $find_size = \${$self->{FIND_SIZE}};
69 ########################################################################
70 # set_find_count_callback
72 sub set_find_count_callback($$) {
75 my $find_count = \${$self->{FIND_COUNT}};
82 # Property setter / getter functions (each does both)
88 my $kind = \${$self->{KIND}};
89 my $dirty = \${$self->{DIRTY}};
93 if(defined($_)) { $$kind = $_; $$dirty = 1; }
95 if (!defined($$kind)) {
105 my $_name = \${$self->{_NAME}};
106 my $dirty = \${$self->{DIRTY}};
110 if(defined($_)) { $$_name = $_; $$dirty = 1; }
118 my $name = \${$self->{NAME}};
119 my $dirty = \${$self->{DIRTY}};
123 if(defined($_)) { $$name = $_; $$dirty = 1; }
128 my $kind = \${$self->{KIND}};
129 my $_name = \${$self->{_NAME}};
131 return "$$kind $$_name";
138 my $pack = \${$self->{PACK}};
139 my $dirty = \${$self->{DIRTY}};
143 if(defined($_)) { $$pack = $_; $$dirty = 1; }
151 my $align = \${$self->{ALIGN}};
161 my $count = $self->field_count;
164 for (my $n = 0; $n < $count; $n++) {
165 my $field = 'c_type_field'->new($self, $n);
166 push @fields, $field;
171 sub field_base_sizes($) {
173 my $field_base_sizes = \${$self->{FIELD_BASE_SIZES}};
177 return $$field_base_sizes;
180 sub field_aligns($) {
182 my $field_aligns = \${$self->{FIELD_ALIGNS}};
186 return $$field_aligns;
191 my $field_type_names = \${$self->{FIELD_TYPE_NAMES}};
193 my @field_type_names = @{$$field_type_names};
194 my $count = scalar(@field_type_names);
202 my $field_names = \${$self->{FIELD_NAMES}};
203 my $dirty = \${$self->{DIRTY}};
207 if(defined($_)) { $$field_names = $_; $$dirty = 1; }
209 return $$field_names;
212 sub field_offsets($) {
214 my $field_offsets = \${$self->{FIELD_OFFSETS}};
218 return $$field_offsets;
223 my $field_sizes = \${$self->{FIELD_SIZES}};
227 return $$field_sizes;
230 sub field_type_names($;$)
233 my $field_type_names = \${$self->{FIELD_TYPE_NAMES}};
234 my $dirty = \${$self->{DIRTY}};
238 if(defined($_)) { $$field_type_names = $_; $$dirty = 1; }
240 return $$field_type_names;
246 my $size = \${$self->{SIZE}};
256 my $dirty = \${$self->{DIRTY}};
260 my $find_align = \${$self->{FIND_ALIGN}};
261 my $find_kind = \${$self->{FIND_KIND}};
262 my $find_size = \${$self->{FIND_SIZE}};
263 my $find_count = \${$self->{FIND_COUNT}};
265 my $align = \${$self->{ALIGN}};
266 my $kind = \${$self->{KIND}};
267 my $size = \${$self->{SIZE}};
268 my $field_aligns = \${$self->{FIELD_ALIGNS}};
269 my $field_base_sizes = \${$self->{FIELD_BASE_SIZES}};
270 my $field_offsets = \${$self->{FIELD_OFFSETS}};
271 my $field_sizes = \${$self->{FIELD_SIZES}};
273 my $pack = $self->pack;
274 $pack = 8 if !defined($pack);
276 my $max_field_align = 0;
279 my $bitfield_size = 0;
280 my $bitfield_bits = 0;
283 foreach my $field ($self->fields) {
284 my $type_name = $field->type_name;
288 if ($type_name =~ s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/)
294 if ($type_name =~ s/\s+DECLSPEC_ALIGN\((\d+)\)//)
298 my $base_size = &$$find_size($type_name);
299 my $type_size=$base_size;
302 $count=&$$find_count($count) if ($count !~ /^\d+$/);
309 $type_size *= int($count);
312 if ($bitfield_size != 0)
314 if (($type_name eq "" and defined $bits and $bits == 0) or
315 (defined $type_size and $bitfield_size != $type_size) or
317 $bitfield_bits + $bits > 8 * $bitfield_size)
319 # This marks the end of the previous bitfield
325 $bitfield_bits+=$bits;
331 $$align = &$$find_align($type_name);
332 $$align=$declspec_align if (defined $declspec_align);
336 $$align = $pack if $$align > $pack;
337 $max_field_align = $$align if $$align > $max_field_align;
339 if ($offset % $$align != 0) {
340 $offset = (int($offset / $$align) + 1) * $$align;
344 if ($$kind !~ /^(?:struct|union)$/)
346 $$kind = &$$find_kind($type_name) || "";
356 $$$field_aligns[$n] = $$align;
357 $$$field_base_sizes[$n] = $base_size;
358 $$$field_offsets[$n] = $offset;
359 $$$field_sizes[$n] = $type_size;
360 $offset += $type_size;
364 $bitfield_size=$type_size;
365 $bitfield_bits=$bits;
371 $$align = $max_field_align if $max_field_align < $pack;
374 if ($$kind =~ /^(?:struct|union)$/) {
375 if ($$size % $$align != 0) {
376 $$size = (int($$size / $$align) + 1) * $$align;
383 package c_type_field;
387 my $class = ref($proto) || $proto;
389 bless ($self, $class);
391 my $type = \${$self->{TYPE}};
392 my $number = \${$self->{NUMBER}};
402 my $type = \${$self->{TYPE}};
403 my $number = \${$self->{NUMBER}};
405 my $field_aligns = $$type->field_aligns;
407 return $$field_aligns[$$number];
412 my $type = \${$self->{TYPE}};
413 my $number = \${$self->{NUMBER}};
415 my $field_base_sizes = $$type->field_base_sizes;
417 return $$field_base_sizes[$$number];
422 my $type = \${$self->{TYPE}};
423 my $number = \${$self->{NUMBER}};
425 my $field_names = $$type->field_names;
427 return $$field_names[$$number];
432 my $type = \${$self->{TYPE}};
433 my $number = \${$self->{NUMBER}};
435 my $field_offsets = $$type->field_offsets;
437 return $$field_offsets[$$number];
442 my $type = \${$self->{TYPE}};
443 my $number = \${$self->{NUMBER}};
445 my $field_sizes = $$type->field_sizes;
447 return $$field_sizes[$$number];
452 my $type = \${$self->{TYPE}};
453 my $number = \${$self->{NUMBER}};
455 my $field_type_names = $$type->field_type_names;
457 return $$field_type_names[$$number];