Commit | Line | Data |
2304df62 |
1 | #!./miniperl |
75f92628 |
2 | |
3 | =head1 NAME |
4 | |
5 | xsubpp - compiler to convert Perl XS code into C code |
6 | |
7 | =head1 SYNOPSIS |
8 | |
8fc38fda |
9 | B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-typemap typemap>]... file.xs |
75f92628 |
10 | |
11 | =head1 DESCRIPTION |
12 | |
13 | I<xsubpp> will compile XS code into C code by embedding the constructs |
14 | necessary to let C functions manipulate Perl values and creates the glue |
15 | necessary to let Perl access those functions. The compiler uses typemaps to |
16 | determine how to map C function parameters and variables to Perl values. |
17 | |
18 | The compiler will search for typemap files called I<typemap>. It will use |
19 | the following search path to find default typemaps, with the rightmost |
20 | typemap taking precedence. |
21 | |
22 | ../../../typemap:../../typemap:../typemap:typemap |
23 | |
24 | =head1 OPTIONS |
25 | |
26 | =over 5 |
27 | |
28 | =item B<-C++> |
29 | |
30 | Adds ``extern "C"'' to the C code. |
31 | |
32 | |
33 | =item B<-except> |
34 | |
35 | Adds exception handling stubs to the C code. |
36 | |
37 | =item B<-typemap typemap> |
38 | |
39 | Indicates that a user-supplied typemap should take precedence over the |
40 | default typemaps. This option may be used multiple times, with the last |
41 | typemap having the highest precedence. |
42 | |
8e07c86e |
43 | =item B<-v> |
44 | |
45 | Prints the I<xsubpp> version number to standard output, then exits. |
46 | |
8fc38fda |
47 | =item B<-prototypes> |
382b8d97 |
48 | |
8fc38fda |
49 | By default I<xsubpp> will not automatically generate prototype code for |
50 | all xsubs. This flag will enable prototypes. |
51 | |
52 | =item B<-noversioncheck> |
53 | |
54 | Disables the run time test that determines if the object file (derived |
55 | from the C<.xs> file) and the C<.pm> files have the same version |
56 | number. |
382b8d97 |
57 | |
75f92628 |
58 | =back |
59 | |
60 | =head1 ENVIRONMENT |
61 | |
62 | No environment variables are used. |
63 | |
64 | =head1 AUTHOR |
65 | |
66 | Larry Wall |
67 | |
f06db76b |
68 | =head1 MODIFICATION HISTORY |
69 | |
8e07c86e |
70 | See the file F<changes.pod>. |
e50aee73 |
71 | |
75f92628 |
72 | =head1 SEE ALSO |
73 | |
a5dbbe28 |
74 | perl(1), perlxs(1), perlxstut(1), perlxs(1) |
75f92628 |
75 | |
76 | =cut |
93a17b20 |
77 | |
f06db76b |
78 | # Global Constants |
db3b9414 |
79 | $XSUBPP_version = "1.938"; |
382b8d97 |
80 | require 5.002; |
4230ab3f |
81 | use vars '$cplusplus'; |
382b8d97 |
82 | |
8fc38fda |
83 | sub Q ; |
84 | |
c07a80fd |
85 | $FH = 'File0000' ; |
8fc38fda |
86 | |
87 | $usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-s pattern] [-typemap typemap]... file.xs\n"; |
f06db76b |
88 | |
382b8d97 |
89 | $proto_re = "[" . quotemeta('\$%&*@;') . "]" ; |
93a17b20 |
90 | |
8e07c86e |
91 | $except = ""; |
8fc38fda |
92 | $WantPrototypes = -1 ; |
93 | $WantVersionChk = 1 ; |
94 | $ProtoUsed = 0 ; |
8e07c86e |
95 | SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) { |
93a17b20 |
96 | $flag = shift @ARGV; |
e50aee73 |
97 | $flag =~ s/^-// ; |
8990e307 |
98 | $spat = shift, next SWITCH if $flag eq 's'; |
99 | $cplusplus = 1, next SWITCH if $flag eq 'C++'; |
382b8d97 |
100 | $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes'; |
101 | $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes'; |
8fc38fda |
102 | $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck'; |
103 | $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck'; |
8e07c86e |
104 | $except = " TRY", next SWITCH if $flag eq 'except'; |
8990e307 |
105 | push(@tm,shift), next SWITCH if $flag eq 'typemap'; |
8e07c86e |
106 | (print "xsubpp version $XSUBPP_version\n"), exit |
107 | if $flag eq 'v'; |
93a17b20 |
108 | die $usage; |
109 | } |
8fc38fda |
110 | if ($WantPrototypes == -1) |
111 | { $WantPrototypes = 0} |
112 | else |
113 | { $ProtoUsed = 1 } |
114 | |
115 | |
8990e307 |
116 | @ARGV == 1 or die $usage; |
c2960299 |
117 | ($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)# |
118 | or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)# |
8990e307 |
119 | or ($dir, $filename) = ('.', $ARGV[0]); |
120 | chdir($dir); |
8fc38fda |
121 | # Check for VMS; Config.pm may not be installed yet, but this routine |
122 | # is built into VMS perl |
123 | if (defined(&VMS::Filespec::vmsify)) { $Is_VMS = 1; $pwd = $ENV{DEFAULT}; } |
124 | else { $Is_VMS = 0; chomp($pwd = `pwd`); } |
125 | |
126 | ++ $IncludedFiles{$ARGV[0]} ; |
93a17b20 |
127 | |
4230ab3f |
128 | my(@XSStack) = ({type => 'none'}); # Stack of conditionals and INCLUDEs |
129 | my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA"); |
130 | |
f06db76b |
131 | sub TrimWhitespace |
132 | { |
133 | $_[0] =~ s/^\s+|\s+$//go ; |
134 | } |
135 | |
136 | sub TidyType |
137 | { |
138 | local ($_) = @_ ; |
139 | |
140 | # rationalise any '*' by joining them into bunches and removing whitespace |
141 | s#\s*(\*+)\s*#$1#g; |
e50aee73 |
142 | s#(\*+)# $1 #g ; |
f06db76b |
143 | |
144 | # change multiple whitespace into a single space |
145 | s/\s+/ /g ; |
146 | |
147 | # trim leading & trailing whitespace |
148 | TrimWhitespace($_) ; |
149 | |
150 | $_ ; |
151 | } |
152 | |
93a17b20 |
153 | $typemap = shift @ARGV; |
8990e307 |
154 | foreach $typemap (@tm) { |
155 | die "Can't find $typemap in $pwd\n" unless -r $typemap; |
93a17b20 |
156 | } |
748a9306 |
157 | unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap |
158 | ../../lib/ExtUtils/typemap ../../../typemap ../../typemap |
159 | ../typemap typemap); |
8990e307 |
160 | foreach $typemap (@tm) { |
f06db76b |
161 | next unless -e $typemap ; |
162 | # skip directories, binary files etc. |
163 | warn("Warning: ignoring non-text typemap file '$typemap'\n"), next |
164 | unless -T $typemap ; |
165 | open(TYPEMAP, $typemap) |
166 | or warn ("Warning: could not open typemap file '$typemap': $!\n"), next; |
e50aee73 |
167 | $mode = 'Typemap'; |
c2960299 |
168 | $junk = "" ; |
8990e307 |
169 | $current = \$junk; |
170 | while (<TYPEMAP>) { |
e50aee73 |
171 | next if /^\s*#/; |
8e07c86e |
172 | if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; } |
173 | if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; } |
174 | if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; } |
e50aee73 |
175 | if ($mode eq 'Typemap') { |
176 | chomp; |
f06db76b |
177 | my $line = $_ ; |
178 | TrimWhitespace($_) ; |
179 | # skip blank lines and comment lines |
180 | next if /^$/ or /^#/ ; |
382b8d97 |
181 | my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or |
182 | warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next; |
183 | $type = TidyType($type) ; |
184 | $type_kind{$type} = $kind ; |
185 | # prototype defaults to '$' |
93d3b392 |
186 | $proto = "\$" unless $proto ; |
382b8d97 |
187 | warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n") |
188 | unless ValidProtoString($proto) ; |
189 | $proto_letter{$type} = C_string($proto) ; |
8e07c86e |
190 | } |
191 | elsif (/^\s/) { |
192 | $$current .= $_; |
463ee0b2 |
193 | } |
e50aee73 |
194 | elsif ($mode eq 'Input') { |
8e07c86e |
195 | s/\s+$//; |
196 | $input_expr{$_} = ''; |
197 | $current = \$input_expr{$_}; |
93a17b20 |
198 | } |
8990e307 |
199 | else { |
8e07c86e |
200 | s/\s+$//; |
201 | $output_expr{$_} = ''; |
202 | $current = \$output_expr{$_}; |
93a17b20 |
203 | } |
8990e307 |
204 | } |
205 | close(TYPEMAP); |
206 | } |
93a17b20 |
207 | |
8990e307 |
208 | foreach $key (keys %input_expr) { |
209 | $input_expr{$key} =~ s/\n+$//; |
210 | } |
93a17b20 |
211 | |
8e07c86e |
212 | $END = "!End!\n\n"; # "impossible" keyword (multiple newline) |
213 | |
214 | # Match an XS keyword |
382b8d97 |
215 | $BLOCK_re= '\s*(' . join('|', qw( |
216 | REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT |
8fc38fda |
217 | CLEANUP ALIAS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE |
db3b9414 |
218 | SCOPE |
382b8d97 |
219 | )) . "|$END)\\s*:"; |
8e07c86e |
220 | |
221 | # Input: ($_, @line) == unparsed input. |
222 | # Output: ($_, @line) == (rest of line, following lines). |
223 | # Return: the matched keyword if found, otherwise 0 |
224 | sub check_keyword { |
225 | $_ = shift(@line) while !/\S/ && @line; |
226 | s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2; |
227 | } |
228 | |
229 | |
230 | sub print_section { |
231 | $_ = shift(@line) while !/\S/ && @line; |
232 | for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) { |
233 | print "$_\n"; |
234 | } |
235 | } |
236 | |
8fc38fda |
237 | sub process_keyword($) |
238 | { |
239 | my($pattern) = @_ ; |
240 | my $kwd ; |
241 | |
242 | &{"${kwd}_handler"}() |
243 | while $kwd = check_keyword($pattern) ; |
244 | } |
245 | |
8e07c86e |
246 | sub CASE_handler { |
247 | blurt ("Error: `CASE:' after unconditional `CASE:'") |
248 | if $condnum && $cond eq ''; |
249 | $cond = $_; |
250 | TrimWhitespace($cond); |
251 | print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n"); |
252 | $_ = '' ; |
253 | } |
254 | |
255 | sub INPUT_handler { |
256 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
257 | last if /^\s*NOT_IMPLEMENTED_YET/; |
258 | next unless /\S/; # skip blank lines |
259 | |
260 | TrimWhitespace($_) ; |
261 | my $line = $_ ; |
262 | |
263 | # remove trailing semicolon if no initialisation |
264 | s/\s*;$//g unless /=/ ; |
265 | |
266 | # check for optional initialisation code |
267 | my $var_init = '' ; |
268 | $var_init = $1 if s/\s*(=.*)$//s ; |
269 | $var_init =~ s/"/\\"/g; |
270 | |
271 | s/\s+/ /g; |
272 | my ($var_type, $var_addr, $var_name) = /^(.*?[^& ]) *(\&?) *\b(\w+)$/s |
273 | or blurt("Error: invalid argument declaration '$line'"), next; |
274 | |
275 | # Check for duplicate definitions |
276 | blurt ("Error: duplicate definition of argument '$var_name' ignored"), next |
277 | if $arg_list{$var_name} ++ ; |
278 | |
279 | $thisdone |= $var_name eq "THIS"; |
280 | $retvaldone |= $var_name eq "RETVAL"; |
281 | $var_types{$var_name} = $var_type; |
282 | print "\t" . &map_type($var_type); |
283 | $var_num = $args_match{$var_name}; |
382b8d97 |
284 | |
8fc38fda |
285 | $proto_arg[$var_num] = ProtoString($var_type) |
286 | if $var_num ; |
8e07c86e |
287 | if ($var_addr) { |
288 | $var_addr{$var_name} = 1; |
289 | $func_args =~ s/\b($var_name)\b/&$1/; |
290 | } |
291 | if ($var_init =~ /^=\s*NO_INIT\s*;?\s*$/) { |
292 | print "\t$var_name;\n"; |
293 | } elsif ($var_init =~ /\S/) { |
294 | &output_init($var_type, $var_num, "$var_name $var_init"); |
295 | } elsif ($var_num) { |
296 | # generate initialization code |
297 | &generate_init($var_type, $var_num, $var_name); |
298 | } else { |
299 | print ";\n"; |
300 | } |
301 | } |
302 | } |
303 | |
304 | sub OUTPUT_handler { |
305 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
306 | next unless /\S/; |
307 | my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ; |
308 | blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next |
309 | if $outargs{$outarg} ++ ; |
310 | if (!$gotRETVAL and $outarg eq 'RETVAL') { |
311 | # deal with RETVAL last |
312 | $RETVAL_code = $outcode ; |
313 | $gotRETVAL = 1 ; |
314 | next ; |
315 | } |
316 | blurt ("Error: OUTPUT $outarg not an argument"), next |
317 | unless defined($args_match{$outarg}); |
318 | blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next |
319 | unless defined $var_types{$outarg} ; |
320 | if ($outcode) { |
321 | print "\t$outcode\n"; |
322 | } else { |
323 | $var_num = $args_match{$outarg}; |
324 | &generate_output($var_types{$outarg}, $var_num, $outarg); |
325 | } |
326 | } |
327 | } |
328 | |
8fc38fda |
329 | sub CLEANUP_handler() { print_section() } |
330 | sub PREINIT_handler() { print_section() } |
331 | sub INIT_handler() { print_section() } |
332 | |
8e07c86e |
333 | sub GetAliases |
334 | { |
335 | my ($line) = @_ ; |
336 | my ($orig) = $line ; |
337 | my ($alias) ; |
338 | my ($value) ; |
339 | |
340 | # Parse alias definitions |
341 | # format is |
342 | # alias = value alias = value ... |
343 | |
344 | while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) { |
345 | $alias = $1 ; |
346 | $orig_alias = $alias ; |
347 | $value = $2 ; |
348 | |
349 | # check for optional package definition in the alias |
350 | $alias = $Packprefix . $alias if $alias !~ /::/ ; |
351 | |
352 | # check for duplicate alias name & duplicate value |
353 | Warn("Warning: Ignoring duplicate alias '$orig_alias'") |
4230ab3f |
354 | if defined $XsubAliases{$alias} ; |
8e07c86e |
355 | |
4230ab3f |
356 | Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values") |
357 | if $XsubAliasValues{$value} ; |
8e07c86e |
358 | |
4230ab3f |
359 | $XsubAliases = 1; |
360 | $XsubAliases{$alias} = $value ; |
361 | $XsubAliasValues{$value} = $orig_alias ; |
8e07c86e |
362 | } |
363 | |
364 | blurt("Error: Cannot parse ALIAS definitions from '$orig'") |
365 | if $line ; |
366 | } |
367 | |
382b8d97 |
368 | sub ALIAS_handler () |
8e07c86e |
369 | { |
370 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
371 | next unless /\S/; |
372 | TrimWhitespace($_) ; |
373 | GetAliases($_) if $_ ; |
374 | } |
375 | } |
376 | |
382b8d97 |
377 | sub REQUIRE_handler () |
8e07c86e |
378 | { |
379 | # the rest of the current line should contain a version number |
380 | my ($Ver) = $_ ; |
381 | |
382 | TrimWhitespace($Ver) ; |
383 | |
384 | death ("Error: REQUIRE expects a version number") |
385 | unless $Ver ; |
386 | |
387 | # check that the version number is of the form n.n |
388 | death ("Error: REQUIRE: expected a number, got '$Ver'") |
389 | unless $Ver =~ /^\d+(\.\d*)?/ ; |
390 | |
391 | death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.") |
392 | unless $XSUBPP_version >= $Ver ; |
393 | } |
394 | |
8fc38fda |
395 | sub VERSIONCHECK_handler () |
396 | { |
397 | # the rest of the current line should contain either ENABLE or |
398 | # DISABLE |
399 | |
400 | TrimWhitespace($_) ; |
401 | |
402 | # check for ENABLE/DISABLE |
403 | death ("Error: VERSIONCHECK: ENABLE/DISABLE") |
404 | unless /^(ENABLE|DISABLE)/i ; |
405 | |
406 | $WantVersionChk = 1 if $1 eq 'ENABLE' ; |
407 | $WantVersionChk = 0 if $1 eq 'DISABLE' ; |
408 | |
409 | } |
410 | |
382b8d97 |
411 | sub PROTOTYPE_handler () |
412 | { |
7d41bd0a |
413 | my $specified ; |
414 | |
c07a80fd |
415 | death("Error: Only 1 PROTOTYPE definition allowed per xsub") |
416 | if $proto_in_this_xsub ++ ; |
417 | |
382b8d97 |
418 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
419 | next unless /\S/; |
7d41bd0a |
420 | $specified = 1 ; |
382b8d97 |
421 | TrimWhitespace($_) ; |
422 | if ($_ eq 'DISABLE') { |
423 | $ProtoThisXSUB = 0 |
424 | } |
425 | elsif ($_ eq 'ENABLE') { |
426 | $ProtoThisXSUB = 1 |
427 | } |
428 | else { |
429 | # remove any whitespace |
430 | s/\s+//g ; |
431 | death("Error: Invalid prototype '$_'") |
432 | unless ValidProtoString($_) ; |
433 | $ProtoThisXSUB = C_string($_) ; |
434 | } |
435 | } |
c07a80fd |
436 | |
7d41bd0a |
437 | # If no prototype specified, then assume empty prototype "" |
438 | $ProtoThisXSUB = 2 unless $specified ; |
439 | |
8fc38fda |
440 | $ProtoUsed = 1 ; |
c07a80fd |
441 | |
382b8d97 |
442 | } |
443 | |
db3b9414 |
444 | sub SCOPE_handler () |
445 | { |
446 | death("Error: Only 1 SCOPE declaration allowed per xsub") |
447 | if $scope_in_this_xsub ++ ; |
448 | |
449 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
450 | next unless /\S/; |
451 | TrimWhitespace($_) ; |
452 | if ($_ =~ /^DISABLE/i) { |
453 | $ScopeThisXSUB = 0 |
454 | } |
455 | elsif ($_ =~ /^ENABLE/i) { |
456 | $ScopeThisXSUB = 1 |
457 | } |
458 | } |
459 | |
460 | } |
461 | |
382b8d97 |
462 | sub PROTOTYPES_handler () |
463 | { |
464 | # the rest of the current line should contain either ENABLE or |
465 | # DISABLE |
466 | |
467 | TrimWhitespace($_) ; |
468 | |
469 | # check for ENABLE/DISABLE |
470 | death ("Error: PROTOTYPES: ENABLE/DISABLE") |
471 | unless /^(ENABLE|DISABLE)/i ; |
472 | |
473 | $WantPrototypes = 1 if $1 eq 'ENABLE' ; |
474 | $WantPrototypes = 0 if $1 eq 'DISABLE' ; |
8fc38fda |
475 | $ProtoUsed = 1 ; |
382b8d97 |
476 | |
477 | } |
478 | |
8fc38fda |
479 | sub INCLUDE_handler () |
480 | { |
481 | # the rest of the current line should contain a valid filename |
482 | |
483 | TrimWhitespace($_) ; |
484 | |
8fc38fda |
485 | death("INCLUDE: filename missing") |
486 | unless $_ ; |
487 | |
488 | death("INCLUDE: output pipe is illegal") |
489 | if /^\s*\|/ ; |
490 | |
491 | # simple minded recursion detector |
492 | death("INCLUDE loop detected") |
493 | if $IncludedFiles{$_} ; |
494 | |
495 | ++ $IncludedFiles{$_} unless /\|\s*$/ ; |
496 | |
497 | # Save the current file context. |
4230ab3f |
498 | push(@XSStack, { |
499 | type => 'file', |
8fc38fda |
500 | LastLine => $lastline, |
501 | LastLineNo => $lastline_no, |
502 | Line => \@line, |
503 | LineNo => \@line_no, |
504 | Filename => $filename, |
c07a80fd |
505 | Handle => $FH, |
8fc38fda |
506 | }) ; |
507 | |
c07a80fd |
508 | ++ $FH ; |
8fc38fda |
509 | |
510 | # open the new file |
c07a80fd |
511 | open ($FH, "$_") or death("Cannot open '$_': $!") ; |
8fc38fda |
512 | |
513 | print Q<<"EOF" ; |
514 | # |
515 | #/* INCLUDE: Including '$_' from '$filename' */ |
516 | # |
517 | EOF |
518 | |
8fc38fda |
519 | $filename = $_ ; |
520 | |
c07a80fd |
521 | # Prime the pump by reading the first |
522 | # non-blank line |
523 | |
524 | # skip leading blank lines |
525 | while (<$FH>) { |
526 | last unless /^\s*$/ ; |
527 | } |
528 | |
529 | $lastline = $_ ; |
8fc38fda |
530 | $lastline_no = $. ; |
531 | |
532 | } |
533 | |
534 | sub PopFile() |
535 | { |
4230ab3f |
536 | return 0 unless $XSStack[-1]{type} eq 'file' ; |
537 | |
538 | my $data = pop @XSStack ; |
8fc38fda |
539 | my $ThisFile = $filename ; |
540 | my $isPipe = ($filename =~ /\|\s*$/) ; |
541 | |
542 | -- $IncludedFiles{$filename} |
543 | unless $isPipe ; |
544 | |
c07a80fd |
545 | close $FH ; |
8fc38fda |
546 | |
c07a80fd |
547 | $FH = $data->{Handle} ; |
8fc38fda |
548 | $filename = $data->{Filename} ; |
549 | $lastline = $data->{LastLine} ; |
550 | $lastline_no = $data->{LastLineNo} ; |
551 | @line = @{ $data->{Line} } ; |
552 | @line_no = @{ $data->{LineNo} } ; |
4230ab3f |
553 | |
8fc38fda |
554 | if ($isPipe and $? ) { |
555 | -- $lastline_no ; |
556 | print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ; |
557 | exit 1 ; |
558 | } |
559 | |
560 | print Q<<"EOF" ; |
561 | # |
562 | #/* INCLUDE: Returning to '$filename' from '$ThisFile' */ |
563 | # |
564 | EOF |
565 | |
566 | return 1 ; |
567 | } |
568 | |
382b8d97 |
569 | sub ValidProtoString ($) |
570 | { |
571 | my($string) = @_ ; |
572 | |
573 | if ( $string =~ /^$proto_re+$/ ) { |
574 | return $string ; |
575 | } |
576 | |
577 | return 0 ; |
578 | } |
579 | |
580 | sub C_string ($) |
581 | { |
582 | my($string) = @_ ; |
583 | |
584 | $string =~ s[\\][\\\\]g ; |
585 | $string ; |
586 | } |
587 | |
588 | sub ProtoString ($) |
589 | { |
590 | my ($type) = @_ ; |
591 | |
93d3b392 |
592 | $proto_letter{$type} or "\$" ; |
382b8d97 |
593 | } |
594 | |
8e07c86e |
595 | sub check_cpp { |
596 | my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line); |
597 | if (@cpp) { |
598 | my ($cpp, $cpplevel); |
599 | for $cpp (@cpp) { |
600 | if ($cpp =~ /^\#\s*if/) { |
601 | $cpplevel++; |
602 | } elsif (!$cpplevel) { |
603 | Warn("Warning: #else/elif/endif without #if in this function"); |
4230ab3f |
604 | print STDERR " (precede it with a blank line if the matching #if is outside the function)\n" |
605 | if $XSStack[-1]{type} eq 'if'; |
8e07c86e |
606 | return; |
607 | } elsif ($cpp =~ /^\#\s*endif/) { |
608 | $cpplevel--; |
609 | } |
610 | } |
611 | Warn("Warning: #if without #endif in this function") if $cpplevel; |
612 | } |
613 | } |
614 | |
615 | |
8990e307 |
616 | sub Q { |
e50aee73 |
617 | my($text) = @_; |
4633a7c4 |
618 | $text =~ s/^#//gm; |
2304df62 |
619 | $text =~ s/\[\[/{/g; |
620 | $text =~ s/\]\]/}/g; |
8990e307 |
621 | $text; |
93a17b20 |
622 | } |
623 | |
c07a80fd |
624 | open($FH, $filename) or die "cannot open $filename: $!\n"; |
c2960299 |
625 | |
f06db76b |
626 | # Identify the version of xsubpp used |
f06db76b |
627 | print <<EOM ; |
e50aee73 |
628 | /* |
629 | * This file was generated automatically by xsubpp version $XSUBPP_version from the |
93d3b392 |
630 | * contents of $filename. Do not edit this file, edit $filename instead. |
e50aee73 |
631 | * |
632 | * ANY CHANGES MADE HERE WILL BE LOST! |
f06db76b |
633 | * |
634 | */ |
e50aee73 |
635 | |
f06db76b |
636 | EOM |
637 | |
638 | |
c07a80fd |
639 | while (<$FH>) { |
e50aee73 |
640 | last if ($Module, $Package, $Prefix) = |
641 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/; |
a0d0e21e |
642 | print $_; |
93a17b20 |
643 | } |
e50aee73 |
644 | &Exit unless defined $_; |
645 | |
8fc38fda |
646 | $lastline = $_; |
647 | $lastline_no = $.; |
93a17b20 |
648 | |
e50aee73 |
649 | |
c07a80fd |
650 | # Read next xsub into @line from ($lastline, <$FH>). |
2304df62 |
651 | sub fetch_para { |
652 | # parse paragraph |
4230ab3f |
653 | death ("Error: Unterminated `#if/#ifdef/#ifndef'") |
654 | if !defined $lastline && $XSStack[-1]{type} eq 'if'; |
2304df62 |
655 | @line = (); |
c2960299 |
656 | @line_no = () ; |
4230ab3f |
657 | return PopFile() if !defined $lastline; |
e50aee73 |
658 | |
659 | if ($lastline =~ |
660 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) { |
661 | $Module = $1; |
8e07c86e |
662 | $Package = defined($2) ? $2 : ''; # keep -w happy |
663 | $Prefix = defined($3) ? $3 : ''; # keep -w happy |
e50aee73 |
664 | ($Module_cname = $Module) =~ s/\W/_/g; |
8e07c86e |
665 | ($Packid = $Package) =~ tr/:/_/; |
e50aee73 |
666 | $Packprefix = $Package; |
8e07c86e |
667 | $Packprefix .= "::" if $Packprefix ne ""; |
2304df62 |
668 | $lastline = ""; |
e50aee73 |
669 | } |
670 | |
671 | for(;;) { |
672 | if ($lastline !~ /^\s*#/ || |
4230ab3f |
673 | # CPP directives: |
674 | # ANSI: if ifdef ifndef elif else endif define undef |
675 | # line error pragma |
676 | # gcc: warning include_next |
677 | # obj-c: import |
678 | # others: ident (gcc notes that some cpps have this one) |
679 | $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) { |
e50aee73 |
680 | last if $lastline =~ /^\S/ && @line && $line[-1] eq ""; |
681 | push(@line, $lastline); |
682 | push(@line_no, $lastline_no) ; |
93a17b20 |
683 | } |
e50aee73 |
684 | |
685 | # Read next line and continuation lines |
c07a80fd |
686 | last unless defined($lastline = <$FH>); |
e50aee73 |
687 | $lastline_no = $.; |
688 | my $tmp_line; |
689 | $lastline .= $tmp_line |
c07a80fd |
690 | while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>)); |
e50aee73 |
691 | |
8e07c86e |
692 | chomp $lastline; |
e50aee73 |
693 | $lastline =~ s/^\s+$//; |
2304df62 |
694 | } |
e50aee73 |
695 | pop(@line), pop(@line_no) while @line && $line[-1] eq ""; |
e50aee73 |
696 | 1; |
2304df62 |
697 | } |
93a17b20 |
698 | |
c2960299 |
699 | PARAGRAPH: |
8e07c86e |
700 | while (fetch_para()) { |
e50aee73 |
701 | # Print initial preprocessor statements and blank lines |
4230ab3f |
702 | while (@line && $line[0] !~ /^[^\#]/) { |
703 | my $line = shift(@line); |
704 | print $line, "\n"; |
705 | next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/; |
706 | my $statement = $+; |
707 | if ($statement eq 'if') { |
708 | $XSS_work_idx = @XSStack; |
709 | push(@XSStack, {type => 'if'}); |
710 | } else { |
711 | death ("Error: `$statement' with no matching `if'") |
712 | if $XSStack[-1]{type} ne 'if'; |
713 | if ($XSStack[-1]{varname}) { |
714 | push(@InitFileCode, "#endif\n"); |
715 | push(@BootCode, "#endif"); |
716 | } |
717 | |
718 | my(@fns) = keys %{$XSStack[-1]{functions}}; |
719 | if ($statement ne 'endif') { |
720 | # Hide the functions defined in other #if branches, and reset. |
721 | @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns; |
722 | @{$XSStack[-1]}{qw(varname functions)} = ('', {}); |
723 | } else { |
724 | my($tmp) = pop(@XSStack); |
725 | 0 while (--$XSS_work_idx |
726 | && $XSStack[$XSS_work_idx]{type} ne 'if'); |
727 | # Keep all new defined functions |
728 | push(@fns, keys %{$tmp->{other_functions}}); |
729 | @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns; |
730 | } |
731 | } |
732 | } |
e50aee73 |
733 | |
734 | next PARAGRAPH unless @line; |
735 | |
4230ab3f |
736 | if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) { |
737 | # We are inside an #if, but have not yet #defined its xsubpp variable. |
738 | print "#define $cpp_next_tmp 1\n\n"; |
739 | push(@InitFileCode, "#if $cpp_next_tmp\n"); |
740 | push(@BootCode, "#if $cpp_next_tmp"); |
741 | $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++; |
742 | } |
743 | |
e50aee73 |
744 | death ("Code is not inside a function") |
745 | if $line[0] =~ /^\s/; |
746 | |
2304df62 |
747 | # initialize info arrays |
748 | undef(%args_match); |
749 | undef(%var_types); |
750 | undef(%var_addr); |
751 | undef(%defaults); |
752 | undef($class); |
753 | undef($static); |
754 | undef($elipsis); |
f06db76b |
755 | undef($wantRETVAL) ; |
756 | undef(%arg_list) ; |
382b8d97 |
757 | undef(@proto_arg) ; |
c07a80fd |
758 | undef($proto_in_this_xsub) ; |
db3b9414 |
759 | undef($scope_in_this_xsub) ; |
382b8d97 |
760 | $ProtoThisXSUB = $WantPrototypes ; |
db3b9414 |
761 | $ScopeThisXSUB = 0; |
2304df62 |
762 | |
8e07c86e |
763 | $_ = shift(@line); |
8fc38fda |
764 | while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) { |
765 | &{"${kwd}_handler"}() ; |
8e07c86e |
766 | next PARAGRAPH unless @line ; |
767 | $_ = shift(@line); |
768 | } |
c2960299 |
769 | |
8e07c86e |
770 | if (check_keyword("BOOT")) { |
771 | &check_cpp; |
772 | push (@BootCode, $_, @line, "") ; |
c2960299 |
773 | next PARAGRAPH ; |
a0d0e21e |
774 | } |
c2960299 |
775 | |
8e07c86e |
776 | |
777 | # extract return type, function name and arguments |
778 | my($ret_type) = TidyType($_); |
779 | |
c2960299 |
780 | # a function definition needs at least 2 lines |
781 | blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH |
782 | unless @line ; |
783 | |
8e07c86e |
784 | $static = 1 if $ret_type =~ s/^static\s+//; |
785 | |
2304df62 |
786 | $func_header = shift(@line); |
c2960299 |
787 | blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH |
8e07c86e |
788 | unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*$/s; |
c2960299 |
789 | |
8e07c86e |
790 | ($class, $func_name, $orig_args) = ($1, $2, $3) ; |
2304df62 |
791 | ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/; |
4230ab3f |
792 | $Full_func_name = "${Packid}_$func_name"; |
c2960299 |
793 | |
794 | # Check for duplicate function definition |
4230ab3f |
795 | for $tmp (@XSStack) { |
796 | next unless defined $tmp->{functions}{$Full_func_name}; |
797 | Warn("Warning: duplicate function definition '$func_name' detected"); |
798 | last; |
8e07c86e |
799 | } |
4230ab3f |
800 | $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ; |
801 | %XsubAliases = %XsubAliasValues = (); |
c2960299 |
802 | |
2304df62 |
803 | @args = split(/\s*,\s*/, $orig_args); |
a0d0e21e |
804 | if (defined($class)) { |
8fc38fda |
805 | my $arg0 = ((defined($static) or $func_name =~ /^new/) ? "CLASS" : "THIS"); |
8e07c86e |
806 | unshift(@args, $arg0); |
807 | ($orig_args = "$arg0, $orig_args") =~ s/^$arg0, $/$arg0/; |
2304df62 |
808 | } |
809 | $orig_args =~ s/"/\\"/g; |
810 | $min_args = $num_args = @args; |
811 | foreach $i (0..$num_args-1) { |
812 | if ($args[$i] =~ s/\.\.\.//) { |
813 | $elipsis = 1; |
814 | $min_args--; |
c2960299 |
815 | if ($args[$i] eq '' && $i == $num_args - 1) { |
2304df62 |
816 | pop(@args); |
817 | last; |
818 | } |
819 | } |
8e07c86e |
820 | if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) { |
2304df62 |
821 | $min_args--; |
822 | $args[$i] = $1; |
823 | $defaults{$args[$i]} = $2; |
824 | $defaults{$args[$i]} =~ s/"/\\"/g; |
825 | } |
93d3b392 |
826 | $proto_arg[$i+1] = "\$" ; |
2304df62 |
827 | } |
a0d0e21e |
828 | if (defined($class)) { |
2304df62 |
829 | $func_args = join(", ", @args[1..$#args]); |
830 | } else { |
831 | $func_args = join(", ", @args); |
832 | } |
833 | @args_match{@args} = 1..@args; |
834 | |
8e07c86e |
835 | $PPCODE = grep(/^\s*PPCODE\s*:/, @line); |
93d3b392 |
836 | $CODE = grep(/^\s*CODE\s*:/, @line); |
8e07c86e |
837 | $ALIAS = grep(/^\s*ALIAS\s*:/, @line); |
838 | |
2304df62 |
839 | # print function header |
a0d0e21e |
840 | print Q<<"EOF"; |
841 | #XS(XS_${Packid}_$func_name) |
2304df62 |
842 | #[[ |
a0d0e21e |
843 | # dXSARGS; |
93a17b20 |
844 | EOF |
8e07c86e |
845 | print Q<<"EOF" if $ALIAS ; |
846 | # dXSI32; |
847 | EOF |
2304df62 |
848 | if ($elipsis) { |
8e07c86e |
849 | $cond = ($min_args ? qq(items < $min_args) : 0); |
2304df62 |
850 | } |
851 | elsif ($min_args == $num_args) { |
852 | $cond = qq(items != $min_args); |
853 | } |
854 | else { |
855 | $cond = qq(items < $min_args || items > $num_args); |
856 | } |
8990e307 |
857 | |
2304df62 |
858 | print Q<<"EOF" if $except; |
859 | # char errbuf[1024]; |
860 | # *errbuf = '\0'; |
861 | EOF |
862 | |
8e07c86e |
863 | if ($ALIAS) |
864 | { print Q<<"EOF" if $cond } |
865 | # if ($cond) |
866 | # croak("Usage: %s($orig_args)", GvNAME(CvGV(cv))); |
867 | EOF |
868 | else |
869 | { print Q<<"EOF" if $cond } |
870 | # if ($cond) |
8990e307 |
871 | # croak("Usage: $pname($orig_args)"); |
93a17b20 |
872 | EOF |
873 | |
a0d0e21e |
874 | print Q<<"EOF" if $PPCODE; |
875 | # SP -= items; |
876 | EOF |
877 | |
2304df62 |
878 | # Now do a block of some sort. |
93a17b20 |
879 | |
2304df62 |
880 | $condnum = 0; |
8e07c86e |
881 | $cond = ''; # last CASE: condidional |
882 | push(@line, "$END:"); |
883 | push(@line_no, $line_no[-1]); |
884 | $_ = ''; |
885 | &check_cpp; |
2304df62 |
886 | while (@line) { |
8e07c86e |
887 | &CASE_handler if check_keyword("CASE"); |
888 | print Q<<"EOF"; |
889 | # $except [[ |
93a17b20 |
890 | EOF |
891 | |
892 | # do initialization of input variables |
893 | $thisdone = 0; |
894 | $retvaldone = 0; |
463ee0b2 |
895 | $deferred = ""; |
c2960299 |
896 | %arg_list = () ; |
897 | $gotRETVAL = 0; |
f06db76b |
898 | |
8fc38fda |
899 | INPUT_handler() ; |
db3b9414 |
900 | process_keyword("INPUT|PREINIT|ALIAS|PROTOTYPE|SCOPE") ; |
8fc38fda |
901 | |
db3b9414 |
902 | print Q<<"EOF" if $ScopeThisXSUB; |
903 | # ENTER; |
904 | # [[ |
905 | EOF |
906 | |
a0d0e21e |
907 | if (!$thisdone && defined($class)) { |
8fc38fda |
908 | if (defined($static) or $func_name =~ /^new/) { |
a0d0e21e |
909 | print "\tchar *"; |
910 | $var_types{"CLASS"} = "char *"; |
911 | &generate_init("char *", 1, "CLASS"); |
912 | } |
913 | else { |
93a17b20 |
914 | print "\t$class *"; |
915 | $var_types{"THIS"} = "$class *"; |
916 | &generate_init("$class *", 1, "THIS"); |
a0d0e21e |
917 | } |
93a17b20 |
918 | } |
919 | |
920 | # do code |
921 | if (/^\s*NOT_IMPLEMENTED_YET/) { |
4633a7c4 |
922 | print "\n\tcroak(\"$pname: not implemented yet\");\n"; |
923 | $_ = '' ; |
93a17b20 |
924 | } else { |
925 | if ($ret_type ne "void") { |
926 | print "\t" . &map_type($ret_type) . "\tRETVAL;\n" |
927 | if !$retvaldone; |
928 | $args_match{"RETVAL"} = 0; |
929 | $var_types{"RETVAL"} = $ret_type; |
930 | } |
db3b9414 |
931 | |
8e07c86e |
932 | print $deferred; |
db3b9414 |
933 | |
934 | process_keyword("INIT|ALIAS|PROTOTYPE") ; |
8e07c86e |
935 | |
936 | if (check_keyword("PPCODE")) { |
8fc38fda |
937 | print_section(); |
8e07c86e |
938 | death ("PPCODE must be last thing") if @line; |
db3b9414 |
939 | print "\tLEAVE;\n" if $ScopeThisXSUB; |
a0d0e21e |
940 | print "\tPUTBACK;\n\treturn;\n"; |
8e07c86e |
941 | } elsif (check_keyword("CODE")) { |
8fc38fda |
942 | print_section() ; |
943 | } elsif (defined($class) and $func_name eq "DESTROY") { |
a0d0e21e |
944 | print "\n\t"; |
8e07c86e |
945 | print "delete THIS;\n"; |
93a17b20 |
946 | } else { |
947 | print "\n\t"; |
948 | if ($ret_type ne "void") { |
463ee0b2 |
949 | print "RETVAL = "; |
e50aee73 |
950 | $wantRETVAL = 1; |
93a17b20 |
951 | } |
952 | if (defined($static)) { |
a0d0e21e |
953 | if ($func_name =~ /^new/) { |
8fc38fda |
954 | $func_name = "$class"; |
8e07c86e |
955 | } else { |
956 | print "${class}::"; |
a0d0e21e |
957 | } |
93a17b20 |
958 | } elsif (defined($class)) { |
8fc38fda |
959 | if ($func_name =~ /^new/) { |
960 | $func_name .= " $class"; |
961 | } else { |
93a17b20 |
962 | print "THIS->"; |
8fc38fda |
963 | } |
93a17b20 |
964 | } |
e50aee73 |
965 | $func_name =~ s/^($spat)// |
966 | if defined($spat); |
93a17b20 |
967 | print "$func_name($func_args);\n"; |
93a17b20 |
968 | } |
969 | } |
970 | |
971 | # do output variables |
8e07c86e |
972 | $gotRETVAL = 0; |
973 | undef $RETVAL_code ; |
974 | undef %outargs ; |
8fc38fda |
975 | process_keyword("OUTPUT|ALIAS|PROTOTYPE"); |
f06db76b |
976 | |
977 | # all OUTPUT done, so now push the return value on the stack |
8e07c86e |
978 | if ($gotRETVAL && $RETVAL_code) { |
979 | print "\t$RETVAL_code\n"; |
980 | } elsif ($gotRETVAL || $wantRETVAL) { |
981 | &generate_output($ret_type, 0, 'RETVAL'); |
982 | } |
f06db76b |
983 | |
93a17b20 |
984 | # do cleanup |
8fc38fda |
985 | process_keyword("CLEANUP|ALIAS|PROTOTYPE") ; |
8e07c86e |
986 | |
db3b9414 |
987 | print Q<<"EOF" if $ScopeThisXSUB; |
988 | # ]] |
989 | EOF |
990 | print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE; |
991 | # LEAVE; |
992 | EOF |
993 | |
93a17b20 |
994 | # print function trailer |
8e07c86e |
995 | print Q<<EOF; |
2304df62 |
996 | # ]] |
8e07c86e |
997 | EOF |
998 | print Q<<EOF if $except; |
8990e307 |
999 | # BEGHANDLERS |
1000 | # CATCHALL |
1001 | # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason); |
1002 | # ENDHANDLERS |
93a17b20 |
1003 | EOF |
8e07c86e |
1004 | if (check_keyword("CASE")) { |
1005 | blurt ("Error: No `CASE:' at top of function") |
1006 | unless $condnum; |
1007 | $_ = "CASE: $_"; # Restore CASE: label |
1008 | next; |
8990e307 |
1009 | } |
8e07c86e |
1010 | last if $_ eq "$END:"; |
1011 | death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function"); |
2304df62 |
1012 | } |
a0d0e21e |
1013 | |
2304df62 |
1014 | print Q<<EOF if $except; |
1015 | # if (errbuf[0]) |
1016 | # croak(errbuf); |
1017 | EOF |
a0d0e21e |
1018 | |
93d3b392 |
1019 | if ($ret_type ne "void" or $CODE) { |
1020 | print Q<<EOF unless $PPCODE; |
a0d0e21e |
1021 | # XSRETURN(1); |
1022 | EOF |
93d3b392 |
1023 | } else { |
1024 | print Q<<EOF unless $PPCODE; |
1025 | # XSRETURN_EMPTY; |
1026 | EOF |
1027 | } |
a0d0e21e |
1028 | |
2304df62 |
1029 | print Q<<EOF; |
2304df62 |
1030 | #]] |
8990e307 |
1031 | # |
93a17b20 |
1032 | EOF |
382b8d97 |
1033 | |
4230ab3f |
1034 | my $newXS = "newXS" ; |
1035 | my $proto = "" ; |
1036 | |
382b8d97 |
1037 | # Build the prototype string for the xsub |
1038 | if ($ProtoThisXSUB) { |
4230ab3f |
1039 | $newXS = "newXSproto"; |
1040 | |
1041 | if ($ProtoThisXSUB == 2) { |
1042 | # User has specified empty prototype |
1043 | $proto = ', ""' ; |
1044 | } |
7d41bd0a |
1045 | elsif ($ProtoThisXSUB != 1) { |
1046 | # User has specified a prototype |
4230ab3f |
1047 | $proto = ', "' . $ProtoThisXSUB . '"'; |
382b8d97 |
1048 | } |
1049 | else { |
1050 | my $s = ';'; |
1051 | if ($min_args < $num_args) { |
1052 | $s = ''; |
1053 | $proto_arg[$min_args] .= ";" ; |
1054 | } |
4230ab3f |
1055 | push @proto_arg, "$s\@" |
382b8d97 |
1056 | if $elipsis ; |
1057 | |
4230ab3f |
1058 | $proto = ', "' . join ("", @proto_arg) . '"'; |
382b8d97 |
1059 | } |
1060 | } |
1061 | |
4230ab3f |
1062 | if (%XsubAliases) { |
1063 | $XsubAliases{$pname} = 0 |
1064 | unless defined $XsubAliases{$pname} ; |
1065 | while ( ($name, $value) = each %XsubAliases) { |
1066 | push(@InitFileCode, Q<<"EOF"); |
1067 | # cv = newXS(\"$name\", XS_$Full_func_name, file); |
1068 | # XSANY.any_i32 = $value ; |
1069 | EOF |
1070 | push(@InitFileCode, Q<<"EOF") if $proto; |
1071 | # sv_setpv((SV*)cv$proto) ; |
1072 | EOF |
1073 | } |
1074 | } |
1075 | else { |
1076 | push(@InitFileCode, |
1077 | " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n"); |
1078 | } |
93a17b20 |
1079 | } |
1080 | |
1081 | # print initialization routine |
8990e307 |
1082 | print Q<<"EOF"; |
4633a7c4 |
1083 | ##ifdef __cplusplus |
1084 | #extern "C" |
1085 | ##endif |
a0d0e21e |
1086 | #XS(boot_$Module_cname) |
2304df62 |
1087 | #[[ |
a0d0e21e |
1088 | # dXSARGS; |
8990e307 |
1089 | # char* file = __FILE__; |
1090 | # |
93a17b20 |
1091 | EOF |
1092 | |
8fc38fda |
1093 | print Q<<"EOF" if $WantVersionChk ; |
1094 | # XS_VERSION_BOOTCHECK ; |
1095 | # |
1096 | EOF |
1097 | |
4230ab3f |
1098 | print Q<<"EOF" if defined $XsubAliases ; |
8e07c86e |
1099 | # { |
1100 | # CV * cv ; |
1101 | # |
1102 | EOF |
1103 | |
4230ab3f |
1104 | print @InitFileCode; |
a0d0e21e |
1105 | |
4230ab3f |
1106 | print Q<<"EOF" if defined $XsubAliases ; |
8e07c86e |
1107 | # } |
1108 | EOF |
1109 | |
a0d0e21e |
1110 | if (@BootCode) |
1111 | { |
8e07c86e |
1112 | print "\n /* Initialisation Section */\n" ; |
a0d0e21e |
1113 | print grep (s/$/\n/, @BootCode) ; |
8e07c86e |
1114 | print "\n /* End of Initialisation Section */\n\n" ; |
93a17b20 |
1115 | } |
a0d0e21e |
1116 | |
e50aee73 |
1117 | print Q<<"EOF";; |
1118 | # ST(0) = &sv_yes; |
1119 | # XSRETURN(1); |
1120 | #]] |
1121 | EOF |
1122 | |
8fc38fda |
1123 | warn("Please specify prototyping behavior for $filename (see perlxs manual)\n") |
1124 | unless $ProtoUsed ; |
e50aee73 |
1125 | &Exit; |
1126 | |
93a17b20 |
1127 | |
1128 | sub output_init { |
2304df62 |
1129 | local($type, $num, $init) = @_; |
a0d0e21e |
1130 | local($arg) = "ST(" . ($num - 1) . ")"; |
93a17b20 |
1131 | |
2304df62 |
1132 | eval qq/print " $init\\\n"/; |
93a17b20 |
1133 | } |
1134 | |
c2960299 |
1135 | sub Warn |
1136 | { |
1137 | # work out the line number |
1138 | my $line_no = $line_no[@line_no - @line -1] ; |
1139 | |
1140 | print STDERR "@_ in $filename, line $line_no\n" ; |
1141 | } |
1142 | |
1143 | sub blurt |
1144 | { |
1145 | Warn @_ ; |
1146 | $errors ++ |
1147 | } |
1148 | |
1149 | sub death |
1150 | { |
1151 | Warn @_ ; |
1152 | exit 1 ; |
1153 | } |
8990e307 |
1154 | |
93a17b20 |
1155 | sub generate_init { |
2304df62 |
1156 | local($type, $num, $var) = @_; |
a0d0e21e |
1157 | local($arg) = "ST(" . ($num - 1) . ")"; |
2304df62 |
1158 | local($argoff) = $num - 1; |
1159 | local($ntype); |
1160 | local($tk); |
93a17b20 |
1161 | |
f06db76b |
1162 | $type = TidyType($type) ; |
c2960299 |
1163 | blurt("Error: '$type' not in typemap"), return |
1164 | unless defined($type_kind{$type}); |
1165 | |
2304df62 |
1166 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
8e07c86e |
1167 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1168 | $tk = $type_kind{$type}; |
1169 | $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/; |
8e07c86e |
1170 | $type =~ tr/:/_/; |
c2960299 |
1171 | blurt("Error: No INPUT definition for type '$type' found"), return |
1172 | unless defined $input_expr{$tk} ; |
2304df62 |
1173 | $expr = $input_expr{$tk}; |
1174 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1175 | blurt("Error: '$subtype' not in typemap"), return |
1176 | unless defined($type_kind{$subtype}); |
1177 | blurt("Error: No INPUT definition for type '$subtype' found"), return |
1178 | unless defined $input_expr{$type_kind{$subtype}} ; |
2304df62 |
1179 | $subexpr = $input_expr{$type_kind{$subtype}}; |
1180 | $subexpr =~ s/ntype/subtype/g; |
1181 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1182 | $subexpr =~ s/\n\t/\n\t\t/g; |
93d3b392 |
1183 | $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g; |
a0d0e21e |
1184 | $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/; |
2304df62 |
1185 | $expr =~ s/DO_ARRAY_ELEM/$subexpr/; |
1186 | } |
db3b9414 |
1187 | if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments |
1188 | $ScopeThisXSUB = 1; |
1189 | } |
2304df62 |
1190 | if (defined($defaults{$var})) { |
1191 | $expr =~ s/(\t+)/$1 /g; |
1192 | $expr =~ s/ /\t/g; |
1193 | eval qq/print "\\t$var;\\n"/; |
1194 | $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/; |
db3b9414 |
1195 | } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) { |
2304df62 |
1196 | eval qq/print "\\t$var;\\n"/; |
1197 | $deferred .= eval qq/"\\n$expr;\\n"/; |
1198 | } else { |
1199 | eval qq/print "$expr;\\n"/; |
1200 | } |
93a17b20 |
1201 | } |
1202 | |
1203 | sub generate_output { |
2304df62 |
1204 | local($type, $num, $var) = @_; |
a0d0e21e |
1205 | local($arg) = "ST(" . ($num - ($num != 0)) . ")"; |
2304df62 |
1206 | local($argoff) = $num - 1; |
1207 | local($ntype); |
93a17b20 |
1208 | |
f06db76b |
1209 | $type = TidyType($type) ; |
2304df62 |
1210 | if ($type =~ /^array\(([^,]*),(.*)\)/) { |
1211 | print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1)), XFree((char *)$var);\n"; |
1212 | } else { |
f06db76b |
1213 | blurt("Error: '$type' not in typemap"), return |
2304df62 |
1214 | unless defined($type_kind{$type}); |
c2960299 |
1215 | blurt("Error: No OUTPUT definition for type '$type' found"), return |
1216 | unless defined $output_expr{$type_kind{$type}} ; |
2304df62 |
1217 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
1218 | $ntype =~ s/\(\)//g; |
8e07c86e |
1219 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1220 | $expr = $output_expr{$type_kind{$type}}; |
1221 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1222 | blurt("Error: '$subtype' not in typemap"), return |
1223 | unless defined($type_kind{$subtype}); |
1224 | blurt("Error: No OUTPUT definition for type '$subtype' found"), return |
1225 | unless defined $output_expr{$type_kind{$subtype}} ; |
2304df62 |
1226 | $subexpr = $output_expr{$type_kind{$subtype}}; |
1227 | $subexpr =~ s/ntype/subtype/g; |
1228 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1229 | $subexpr =~ s/\$var/${var}[ix_$var]/g; |
1230 | $subexpr =~ s/\n\t/\n\t\t/g; |
1231 | $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/; |
a0d0e21e |
1232 | eval "print qq\a$expr\a"; |
2304df62 |
1233 | } |
a0d0e21e |
1234 | elsif ($var eq 'RETVAL') { |
93d3b392 |
1235 | if ($expr =~ /^\t\$arg\s*=\s*\$var\s*;/) { |
1236 | eval "print qq\a$expr\a"; |
1237 | print "\tif (SvREFCNT(ST(0))) sv_2mortal(ST(0));\n"; |
1238 | } |
1239 | elsif ($expr =~ /^\t\$arg = /) { |
a0d0e21e |
1240 | eval "print qq\a$expr\a"; |
2304df62 |
1241 | print "\tsv_2mortal(ST(0));\n"; |
93a17b20 |
1242 | } |
2304df62 |
1243 | else { |
8990e307 |
1244 | print "\tST(0) = sv_newmortal();\n"; |
a0d0e21e |
1245 | eval "print qq\a$expr\a"; |
463ee0b2 |
1246 | } |
2304df62 |
1247 | } |
a0d0e21e |
1248 | elsif ($arg =~ /^ST\(\d+\)$/) { |
1249 | eval "print qq\a$expr\a"; |
1250 | } |
2304df62 |
1251 | } |
93a17b20 |
1252 | } |
1253 | |
1254 | sub map_type { |
e50aee73 |
1255 | my($type) = @_; |
93a17b20 |
1256 | |
8e07c86e |
1257 | $type =~ tr/:/_/; |
1258 | $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s; |
1259 | $type; |
93a17b20 |
1260 | } |
8990e307 |
1261 | |
e50aee73 |
1262 | |
1263 | sub Exit { |
748a9306 |
1264 | # If this is VMS, the exit status has meaning to the shell, so we |
8fc38fda |
1265 | # use a predictable value (SS$_Normal or SS$_Abort) rather than an |
1266 | # arbitrary number. |
1267 | exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ; |
e50aee73 |
1268 | } |