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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 use output
qw($output);
27 my $class = ref($proto) || $proto;
29 bless ($self, $class);
34 ########################################################################
35 # set_find_align_callback
37 sub set_find_align_callback {
40 my $find_align = \${$self->{FIND_ALIGN}};
45 ########################################################################
46 # set_find_kind_callback
48 sub set_find_kind_callback {
51 my $find_kind = \${$self->{FIND_KIND}};
56 ########################################################################
57 # set_find_size_callback
59 sub set_find_size_callback {
62 my $find_size = \${$self->{FIND_SIZE}};
67 ########################################################################
68 # set_find_count_callback
70 sub set_find_count_callback {
73 my $find_count = \${$self->{FIND_COUNT}};
80 my $kind = \${$self->{KIND}};
81 my $dirty = \${$self->{DIRTY}};
85 if(defined($_)) { $$kind = $_; $$dirty = 1; }
87 if (!defined($$kind)) {
96 my $_name = \${$self->{_NAME}};
97 my $dirty = \${$self->{DIRTY}};
101 if(defined($_)) { $$_name = $_; $$dirty = 1; }
108 my $name = \${$self->{NAME}};
109 my $dirty = \${$self->{DIRTY}};
113 if(defined($_)) { $$name = $_; $$dirty = 1; }
118 my $kind = \${$self->{KIND}};
119 my $_name = \${$self->{_NAME}};
121 return "$$kind $$_name";
127 my $pack = \${$self->{PACK}};
128 my $dirty = \${$self->{DIRTY}};
132 if(defined($_)) { $$pack = $_; $$dirty = 1; }
140 my $align = \${$self->{ALIGN}};
150 my $count = $self->field_count;
153 for (my $n = 0; $n < $count; $n++) {
154 my $field = 'c_type_field'->new($self, $n);
155 push @fields, $field;
160 sub field_base_sizes {
162 my $field_base_sizes = \${$self->{FIELD_BASE_SIZES}};
166 return $$field_base_sizes;
171 my $field_aligns = \${$self->{FIELD_ALIGNS}};
175 return $$field_aligns;
180 my $field_type_names = \${$self->{FIELD_TYPE_NAMES}};
182 my @field_type_names = @{$$field_type_names};
183 my $count = scalar(@field_type_names);
190 my $field_names = \${$self->{FIELD_NAMES}};
191 my $dirty = \${$self->{DIRTY}};
195 if(defined($_)) { $$field_names = $_; $$dirty = 1; }
197 return $$field_names;
202 my $field_offsets = \${$self->{FIELD_OFFSETS}};
206 return $$field_offsets;
211 my $field_sizes = \${$self->{FIELD_SIZES}};
215 return $$field_sizes;
218 sub field_type_names {
220 my $field_type_names = \${$self->{FIELD_TYPE_NAMES}};
221 my $dirty = \${$self->{DIRTY}};
225 if(defined($_)) { $$field_type_names = $_; $$dirty = 1; }
227 return $$field_type_names;
233 my $size = \${$self->{SIZE}};
243 my $dirty = \${$self->{DIRTY}};
247 my $find_align = \${$self->{FIND_ALIGN}};
248 my $find_kind = \${$self->{FIND_KIND}};
249 my $find_size = \${$self->{FIND_SIZE}};
250 my $find_count = \${$self->{FIND_COUNT}};
252 my $align = \${$self->{ALIGN}};
253 my $kind = \${$self->{KIND}};
254 my $size = \${$self->{SIZE}};
255 my $field_aligns = \${$self->{FIELD_ALIGNS}};
256 my $field_base_sizes = \${$self->{FIELD_BASE_SIZES}};
257 my $field_offsets = \${$self->{FIELD_OFFSETS}};
258 my $field_sizes = \${$self->{FIELD_SIZES}};
260 my $pack = $self->pack;
261 $pack = 8 if !defined($pack);
263 my $max_field_align = 0;
266 my $bitfield_size = 0;
267 my $bitfield_bits = 0;
270 foreach my $field ($self->fields) {
271 my $type_name = $field->type_name;
275 if ($type_name =~ s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/)
281 if ($type_name =~ s/\s+DECLSPEC_ALIGN\((\d+)\)//)
285 my $base_size = &$$find_size($type_name);
286 my $type_size=$base_size;
289 $count=&$$find_count($count) if ($count !~ /^\d+$/);
296 $type_size *= int($count);
299 if ($bitfield_size != 0)
301 if (($type_name eq "" and defined $bits and $bits == 0) or
302 (defined $type_size and $bitfield_size != $type_size) or
304 $bitfield_bits + $bits > 8 * $bitfield_size)
306 # This marks the end of the previous bitfield
312 $bitfield_bits+=$bits;
318 $$align = &$$find_align($type_name);
319 $$align=$declspec_align if (defined $declspec_align);
323 $$align = $pack if $$align > $pack;
324 $max_field_align = $$align if $$align > $max_field_align;
326 if ($offset % $$align != 0) {
327 $offset = (int($offset / $$align) + 1) * $$align;
331 if ($$kind !~ /^(?:struct|union)$/)
333 $$kind = &$$find_kind($type_name) || "";
343 $$$field_aligns[$n] = $$align;
344 $$$field_base_sizes[$n] = $base_size;
345 $$$field_offsets[$n] = $offset;
346 $$$field_sizes[$n] = $type_size;
347 $offset += $type_size;
351 $bitfield_size=$type_size;
352 $bitfield_bits=$bits;
358 $$align = $max_field_align if $max_field_align < $pack;
361 if ($$kind =~ /^(?:struct|union)$/) {
362 if ($$size % $$align != 0) {
363 $$size = (int($$size / $$align) + 1) * $$align;
370 package c_type_field;
374 my $class = ref($proto) || $proto;
376 bless ($self, $class);
378 my $type = \${$self->{TYPE}};
379 my $number = \${$self->{NUMBER}};
389 my $type = \${$self->{TYPE}};
390 my $number = \${$self->{NUMBER}};
392 my $field_aligns = $$type->field_aligns;
394 return $$field_aligns[$$number];
399 my $type = \${$self->{TYPE}};
400 my $number = \${$self->{NUMBER}};
402 my $field_base_sizes = $$type->field_base_sizes;
404 return $$field_base_sizes[$$number];
409 my $type = \${$self->{TYPE}};
410 my $number = \${$self->{NUMBER}};
412 my $field_names = $$type->field_names;
414 return $$field_names[$$number];
419 my $type = \${$self->{TYPE}};
420 my $number = \${$self->{NUMBER}};
422 my $field_offsets = $$type->field_offsets;
424 return $$field_offsets[$$number];
429 my $type = \${$self->{TYPE}};
430 my $number = \${$self->{NUMBER}};
432 my $field_sizes = $$type->field_sizes;
434 return $$field_sizes[$$number];
439 my $type = \${$self->{TYPE}};
440 my $number = \${$self->{NUMBER}};
442 my $field_type_names = $$type->field_type_names;
444 return $$field_type_names[$$number];