[patch] ext/b/t/lint.t fails on win32
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Constant / ProxySubs.pm
CommitLineData
6d7fb585 1package ExtUtils::Constant::ProxySubs;
2
3use strict;
64bb7586 4use vars qw($VERSION @ISA %type_to_struct %type_from_struct %type_to_sv
6800c0cf 5 %type_to_C_value %type_is_a_problem %type_num_args
6 %type_temporary);
6d7fb585 7use Carp;
8require ExtUtils::Constant::XS;
9use ExtUtils::Constant::Utils qw(C_stringify);
10use ExtUtils::Constant::XS qw(%XS_TypeSet);
11
12$VERSION = '0.01';
13@ISA = 'ExtUtils::Constant::XS';
14
15%type_to_struct =
16 (
17 IV => '{const char *name; I32 namelen; IV value;}',
64bb7586 18 NV => '{const char *name; I32 namelen; NV value;}',
19 UV => '{const char *name; I32 namelen; UV value;}',
49657794 20 PV => '{const char *name; I32 namelen; const char *value;}',
6f226cd7 21 PVN => '{const char *name; I32 namelen; const char *value; STRLEN len;}',
64bb7586 22 YES => '{const char *name; I32 namelen;}',
23 NO => '{const char *name; I32 namelen;}',
6f226cd7 24 UNDEF => '{const char *name; I32 namelen;}',
6d7fb585 25 '' => '{const char *name; I32 namelen;} ',
26 );
27
64bb7586 28%type_from_struct =
29 (
30 IV => sub { $_[0] . '->value' },
31 NV => sub { $_[0] . '->value' },
32 UV => sub { $_[0] . '->value' },
49657794 33 PV => sub { $_[0] . '->value' },
6f226cd7 34 PVN => sub { $_[0] . '->value', $_[0] . '->len' },
64bb7586 35 YES => sub {},
36 NO => sub {},
6f226cd7 37 UNDEF => sub {},
64bb7586 38 '' => sub {},
39 );
40
6d7fb585 41%type_to_sv =
42 (
64bb7586 43 IV => sub { "newSViv($_[0])" },
44 NV => sub { "newSVnv($_[0])" },
45 UV => sub { "newSVuv($_[0])" },
49657794 46 PV => sub { "newSVpv($_[0], 0)" },
6f226cd7 47 PVN => sub { "newSVpvn($_[0], $_[1])" },
64bb7586 48 YES => sub { '&PL_sv_yes' },
49 NO => sub { '&PL_sv_no' },
0fcb9a02 50 UNDEF => sub { '&PL_sv_undef' },
6d7fb585 51 '' => sub { '&PL_sv_yes' },
2ebbb0c3 52 SV => sub {"SvREFCNT_inc($_[0])"},
6d7fb585 53 );
54
55%type_to_C_value =
56 (
64bb7586 57 YES => sub {},
58 NO => sub {},
0fcb9a02 59 UNDEF => sub {},
6d7fb585 60 '' => sub {},
61 );
62
64bb7586 63sub type_to_C_value {
64 my ($self, $type) = @_;
65 return $type_to_C_value{$type} || sub {return map {ref $_ ? @$_ : $_} @_};
66}
67
49657794 68# TODO - figure out if there is a clean way for the type_to_sv code to
69# attempt s/sv_2mortal// and if it succeeds tell type_to_sv not to add
70# SvREFCNT_inc
6d7fb585 71%type_is_a_problem =
72 (
2ebbb0c3 73 # The documentation says *mortal SV*, but we now need a non-mortal copy.
6d7fb585 74 SV => 1,
75 );
76
49657794 77%type_temporary =
78 (
6f226cd7 79 SV => ['SV *'],
80 PV => ['const char *'],
81 PVN => ['const char *', 'STRLEN'],
49657794 82 );
6f226cd7 83$type_temporary{$_} = [$_] foreach qw(IV UV NV);
6800c0cf 84
6d7fb585 85while (my ($type, $value) = each %XS_TypeSet) {
64bb7586 86 $type_num_args{$type}
87 = defined $value ? ref $value ? scalar @$value : 1 : 0;
6d7fb585 88}
89$type_num_args{''} = 0;
90
91sub partition_names {
92 my ($self, $default_type, @items) = @_;
93 my (%found, @notfound, @trouble);
94
95 while (my $item = shift @items) {
96 my $default = delete $item->{default};
97 if ($default) {
98 # If we find a default value, convert it into a regular item and
99 # append it to the queue of items to process
100 my $default_item = {%$item};
101 $default_item->{invert_macro} = 1;
102 $default_item->{pre} = delete $item->{def_pre};
103 $default_item->{post} = delete $item->{def_post};
104 $default_item->{type} = shift @$default;
105 $default_item->{value} = $default;
106 push @items, $default_item;
107 } else {
108 # It can be "not found" unless it's the default (invert the macro)
109 # or the "macro" is an empty string (ie no macro)
110 push @notfound, $item unless $item->{invert_macro}
0fcb9a02 111 or !$self->macro_to_ifdef($self->macro_from_item($item));
6d7fb585 112 }
113
64bb7586 114 if ($item->{pre} or $item->{post} or $item->{not_constant}
115 or $type_is_a_problem{$item->{type}}) {
6d7fb585 116 push @trouble, $item;
117 } else {
118 push @{$found{$item->{type}}}, $item;
119 }
120 }
121 # use Data::Dumper; print Dumper \%found;
122 (\%found, \@notfound, \@trouble);
123}
124
125sub boottime_iterator {
126 my ($self, $type, $iterator, $hash, $subname) = @_;
64bb7586 127 my $extractor = $type_from_struct{$type};
128 die "Can't find extractor code for type $type"
129 unless defined $extractor;
6d7fb585 130 my $generator = $type_to_sv{$type};
131 die "Can't find generator code for type $type"
132 unless defined $generator;
133
134 my $athx = $self->C_constant_prefix_param();
135
64bb7586 136 return sprintf <<"EOBOOT", &$generator(&$extractor($iterator));
6d7fb585 137 while ($iterator->name) {
138 $subname($athx $hash, $iterator->name,
139 $iterator->namelen, %s);
140 ++$iterator;
141 }
142EOBOOT
143}
144
64bb7586 145sub name_len_value_macro {
146 my ($self, $item) = @_;
147 my $name = $item->{name};
148 my $value = $item->{value};
149 $value = $item->{name} unless defined $value;
150
151 my $namelen = length $name;
152 if ($name =~ tr/\0-\377// != $namelen) {
153 # the hash API signals UTF-8 by passing the length negated.
154 utf8::encode($name);
155 $namelen = -length $name;
156 }
157 $name = C_stringify($name);
158
0fcb9a02 159 my $macro = $self->macro_from_item($item);
64bb7586 160 ($name, $namelen, $value, $macro);
161}
162
6d7fb585 163sub WriteConstants {
164 my $self = shift;
165 my $ARGS = shift;
166
167 my ($c_fh, $xs_fh, $c_subname, $xs_subname, $default_type, $package)
168 = @{$ARGS}{qw(c_fh xs_fh c_subname xs_subname default_type package)};
169
170 $xs_subname ||= 'constant';
171
172 croak("Package name '$package' contains % characters") if $package =~ /%/;
173
174 # All the types we see
175 my $what = {};
176 # A hash to lookup items with.
177 my $items = {};
178
179 my @items = $self->normalise_items ({disable_utf8_duplication => 1},
180 $default_type, $what, $items, @_);
181
182 # Partition the values by type. Also include any defaults in here
183 # Everything that doesn't have a default needs alternative code for
184 # "I'm missing"
185 # And everything that has pre or post code ends up in a private block
186 my ($found, $notfound, $trouble)
187 = $self->partition_names($default_type, @items);
188
6d7fb585 189 my $pthx = $self->C_constant_prefix_param_defintion();
190 my $athx = $self->C_constant_prefix_param();
191 my $symbol_table = C_stringify($package) . '::';
192
64bb7586 193 print $c_fh $self->header(), <<"EOADD";
6d7fb585 194void ${c_subname}_add_symbol($pthx HV *hash, const char *name, I32 namelen, SV *value) {
0998eade 195 SV **sv = hv_fetch(hash, name, namelen, TRUE);
196 if (!sv) {
197 Perl_croak($athx "Couldn't add key '%s' to %%%s::", name, "$package");
198 }
199 if (SvOK(*sv) || SvTYPE(*sv) == SVt_PVGV) {
200 /* Someone has been here before us - have to make a real sub. */
201 newCONSTSUB(hash, name, value);
202 } else {
203 SvUPGRADE(*sv, SVt_RV);
204 SvRV_set(*sv, value);
205 SvROK_on(*sv);
abe8a887 206 SvREADONLY_on(value);
6d7fb585 207 }
208}
209
210static HV *${c_subname}_missing = NULL;
211
212EOADD
213
214 print $xs_fh <<"EOBOOT";
215BOOT:
216 {
217#ifdef dTHX
218 dTHX;
219#endif
220 HV *symbol_table = get_hv("$symbol_table", TRUE);
221EOBOOT
222
223 my %iterator;
224
225 $found->{''}
226 = [map {{%$_, type=>'', invert_macro => 1}} @$notfound];
227
228 foreach my $type (sort keys %$found) {
229 my $struct = $type_to_struct{$type};
64bb7586 230 my $type_to_value = $self->type_to_C_value($type);
6d7fb585 231 my $number_of_args = $type_num_args{$type};
232 die "Can't find structure definition for type $type"
233 unless defined $struct;
234
235 my $struct_type = $type ? lc($type) . '_s' : 'notfound_s';
236 print $c_fh "struct $struct_type $struct;\n";
237
238 my $array_name = 'values_for_' . ($type ? lc $type : 'notfound');
239 print $xs_fh <<"EOBOOT";
240
241 static const struct $struct_type $array_name\[] =
242 {
243EOBOOT
244
245
246 foreach my $item (@{$found->{$type}}) {
64bb7586 247 my ($name, $namelen, $value, $macro)
248 = $self->name_len_value_macro($item);
6d7fb585 249
6d7fb585 250 my $ifdef = $self->macro_to_ifdef($macro);
251 if (!$ifdef && $item->{invert_macro}) {
252 carp("Attempting to supply a default for '$name' which has no conditional macro");
253 next;
254 }
255 print $xs_fh $ifdef;
256 if ($item->{invert_macro}) {
257 print $xs_fh
258 " /* This is the default value: */\n" if $type;
259 print $xs_fh "#else\n";
260 }
261 print $xs_fh " { ", join (', ', "\"$name\"", $namelen,
262 &$type_to_value($value)), " },\n",
263 $self->macro_to_endif($macro);
264 }
265
266
267 # Terminate the list with a NULL
268 print $xs_fh " { NULL, 0", (", 0" x $number_of_args), " } };\n";
269
270 $iterator{$type} = "value_for_" . ($type ? lc $type : 'notfound');
271
272 print $xs_fh <<"EOBOOT";
273 const struct $struct_type *$iterator{$type} = $array_name;
274
275EOBOOT
276 }
277
278 delete $found->{''};
279 foreach my $type (sort keys %$found) {
280 print $xs_fh $self->boottime_iterator($type, $iterator{$type},
281 'symbol_table',
282 "${c_subname}_add_symbol");
283 }
284 print $xs_fh <<"EOBOOT";
285
286 ${c_subname}_missing = newHV();
287 while (value_for_notfound->name) {
288 if (!hv_store(${c_subname}_missing, value_for_notfound->name,
0998eade 289 value_for_notfound->namelen, &PL_sv_yes, 0))
64bb7586 290 Perl_croak($athx "Couldn't add key '%s' to missing_hash",
6d7fb585 291 value_for_notfound->name);
292 ++value_for_notfound;
293 }
6d7fb585 294EOBOOT
295
64bb7586 296 foreach my $item (@$trouble) {
297 my ($name, $namelen, $value, $macro)
298 = $self->name_len_value_macro($item);
299 my $ifdef = $self->macro_to_ifdef($macro);
300 my $type = $item->{type};
301 my $type_to_value = $self->type_to_C_value($type);
302
303 print $xs_fh $ifdef;
304 if ($item->{invert_macro}) {
305 print $xs_fh
306 " /* This is the default value: */\n" if $type;
307 print $xs_fh "#else\n";
308 }
309 my $generator = $type_to_sv{$type};
310 die "Can't find generator code for type $type"
311 unless defined $generator;
312
6f226cd7 313 print $xs_fh " {\n";
6800c0cf 314 # We need to use a temporary value because some really troublesome
315 # items use C pre processor directives in their values, and in turn
316 # these don't fit nicely in the macro-ised generator functions
6f226cd7 317 my $counter = 0;
318 printf $xs_fh " %s temp%d;\n", $_, $counter++
319 foreach @{$type_temporary{$type}};
320
321 print $xs_fh " $item->{pre}\n" if $item->{pre};
322
323 # And because the code in pre might be both declarations and
324 # statements, we can't declare and assign to the temporaries in one.
325 $counter = 0;
326 printf $xs_fh " temp%d = %s;\n", $counter++, $_
327 foreach &$type_to_value($value);
328
329 my @tempvarnames = map {sprintf 'temp%d', $_} 0 .. $counter - 1;
330 printf $xs_fh <<"EOBOOT", $name, &$generator(@tempvarnames);
2ebbb0c3 331 ${c_subname}_add_symbol($athx symbol_table, "%s",
332 $namelen, %s);
64bb7586 333EOBOOT
2ebbb0c3 334 print $xs_fh " $item->{post}\n" if $item->{post};
335 print $xs_fh " }\n";
64bb7586 336
337 print $xs_fh $self->macro_to_endif($macro);
338 }
339
340 print $xs_fh <<EOCONSTANT
341 }
6d7fb585 342
343void
344$xs_subname(sv)
345 PREINIT:
346 STRLEN len;
347 INPUT:
348 SV * sv;
349 const char * s = SvPV(sv, len);
350 PPCODE:
351 if (hv_exists(${c_subname}_missing, s, SvUTF8(sv) ? -len : len)) {
352 sv = newSVpvf("Your vendor has not defined $package macro %" SVf
353 ", used", sv);
354 } else {
355 sv = newSVpvf("%" SVf " is not a valid $package macro", sv);
356 }
357 PUSHs(sv_2mortal(sv));
358EOCONSTANT
359}
360
3611;