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