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}};
69 my $kind = \${$self->{KIND}};
70 my $dirty = \${$self->{DIRTY}};
74 if(defined($_)) { $$kind = $_; $$dirty = 1; }
76 if (!defined($$kind)) {
85 my $_name = \${$self->{_NAME}};
86 my $dirty = \${$self->{DIRTY}};
90 if(defined($_)) { $$_name = $_; $$dirty = 1; }
97 my $name = \${$self->{NAME}};
98 my $dirty = \${$self->{DIRTY}};
102 if(defined($_)) { $$name = $_; $$dirty = 1; }
107 my $kind = \${$self->{KIND}};
108 my $_name = \${$self->{_NAME}};
110 return "$$kind $$_name";
116 my $pack = \${$self->{PACK}};
117 my $dirty = \${$self->{DIRTY}};
121 if(defined($_)) { $$pack = $_; $$dirty = 1; }
129 my $align = \${$self->{ALIGN}};
139 my $count = $self->field_count;
142 for (my $n = 0; $n < $count; $n++) {
143 my $field = 'c_type_field'->new($self, $n);
144 push @fields, $field;
149 sub field_base_sizes {
151 my $field_base_sizes = \${$self->{FIELD_BASE_SIZES}};
155 return $$field_base_sizes;
160 my $field_aligns = \${$self->{FIELD_ALIGNS}};
164 return $$field_aligns;
169 my $field_type_names = \${$self->{FIELD_TYPE_NAMES}};
171 my @field_type_names = @{$$field_type_names};
172 my $count = scalar(@field_type_names);
179 my $field_names = \${$self->{FIELD_NAMES}};
180 my $dirty = \${$self->{DIRTY}};
184 if(defined($_)) { $$field_names = $_; $$dirty = 1; }
186 return $$field_names;
191 my $field_offsets = \${$self->{FIELD_OFFSETS}};
195 return $$field_offsets;
200 my $field_sizes = \${$self->{FIELD_SIZES}};
204 return $$field_sizes;
207 sub field_type_names {
209 my $field_type_names = \${$self->{FIELD_TYPE_NAMES}};
210 my $dirty = \${$self->{DIRTY}};
214 if(defined($_)) { $$field_type_names = $_; $$dirty = 1; }
216 return $$field_type_names;
222 my $size = \${$self->{SIZE}};
232 my $dirty = \${$self->{DIRTY}};
236 my $find_align = \${$self->{FIND_ALIGN}};
237 my $find_kind = \${$self->{FIND_KIND}};
238 my $find_size = \${$self->{FIND_SIZE}};
240 my $align = \${$self->{ALIGN}};
241 my $kind = \${$self->{KIND}};
242 my $size = \${$self->{SIZE}};
243 my $field_aligns = \${$self->{FIELD_ALIGNS}};
244 my $field_base_sizes = \${$self->{FIELD_BASE_SIZES}};
245 my $field_offsets = \${$self->{FIELD_OFFSETS}};
246 my $field_sizes = \${$self->{FIELD_SIZES}};
248 my $pack = $self->pack;
249 $pack = 4 if !defined($pack);
251 my $max_field_align = 0;
257 foreach my $field ($self->fields) {
258 my $type_name = $field->type_name;
259 my $type_size = &$$find_size($type_name);
261 my $base_type_name = $type_name;
262 if ($base_type_name =~ s/^(.*?)\s*(?:\[\s*(.*?)\s*\]|:(\d+))?$/$1/) {
266 my $base_size = &$$find_size($base_type_name);
267 $$align = &$$find_align($base_type_name);
269 if (defined($$align)) {
270 $$align = $pack if $$align > $pack;
271 $max_field_align = $$align if $$align > $max_field_align;
273 if ($offset % $$align != 0) {
274 $offset = (int($offset / $$align) + 1) * $$align;
278 if ($$kind !~ /^(?:struct|union)$/) {
279 $$kind = &$$find_kind($type_name) || "";
282 if (!defined($type_size)) {
286 } elsif ($type_size >= 0) {
288 $offset += $pack * int(($offset_bits + 8 * $pack - 1 ) / (8 * $pack));
292 $$$field_aligns[$n] = $$align;
293 $$$field_base_sizes[$n] = $base_size;
294 $$$field_offsets[$n] = $offset;
295 $$$field_sizes[$n] = $type_size;
297 $offset += $type_size;
299 $$$field_aligns[$n] = $$align;
300 $$$field_base_sizes[$n] = $base_size;
301 $$$field_offsets[$n] = $offset;
302 $$$field_sizes[$n] = $type_size;
304 $offset_bits += -$type_size;
311 $$align = $max_field_align if $max_field_align < $pack;
314 $offset += $pack * int(($offset_bits + 8 * $pack - 1 ) / (8 * $pack));
319 if ($$kind =~ /^(?:struct|union)$/) {
320 if ($$size % $$align != 0) {
321 $$size = (int($$size / $$align) + 1) * $$align;
328 package c_type_field;
332 my $class = ref($proto) || $proto;
334 bless ($self, $class);
336 my $type = \${$self->{TYPE}};
337 my $number = \${$self->{NUMBER}};
347 my $type = \${$self->{TYPE}};
348 my $number = \${$self->{NUMBER}};
350 my $field_aligns = $$type->field_aligns;
352 return $$field_aligns[$$number];
357 my $type = \${$self->{TYPE}};
358 my $number = \${$self->{NUMBER}};
360 my $field_base_sizes = $$type->field_base_sizes;
362 return $$field_base_sizes[$$number];
367 my $type = \${$self->{TYPE}};
368 my $number = \${$self->{NUMBER}};
370 my $field_names = $$type->field_names;
372 return $$field_names[$$number];
377 my $type = \${$self->{TYPE}};
378 my $number = \${$self->{NUMBER}};
380 my $field_offsets = $$type->field_offsets;
382 return $$field_offsets[$$number];
387 my $type = \${$self->{TYPE}};
388 my $number = \${$self->{NUMBER}};
390 my $field_sizes = $$type->field_sizes;
392 return $$field_sizes[$$number];
397 my $type = \${$self->{TYPE}};
398 my $number = \${$self->{NUMBER}};
400 my $field_type_names = $$type->field_type_names;
402 return $$field_type_names[$$number];