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