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