Memoize tests
[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
b26a54d0 9B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-nooptimize>] [B<-typemap typemap>] ... file.xs
75f92628 10
11=head1 DESCRIPTION
12
b26a54d0 13This compiler is typically run by the makefiles created by L<ExtUtils::MakeMaker>.
14
75f92628 15I<xsubpp> will compile XS code into C code by embedding the constructs
16necessary to let C functions manipulate Perl values and creates the glue
17necessary to let Perl access those functions. The compiler uses typemaps to
18determine how to map C function parameters and variables to Perl values.
19
20The compiler will search for typemap files called I<typemap>. It will use
21the following search path to find default typemaps, with the rightmost
22typemap taking precedence.
23
24 ../../../typemap:../../typemap:../typemap:typemap
25
26=head1 OPTIONS
27
b26a54d0 28Note that the C<XSOPT> MakeMaker option may be used to add these options to
29any makefiles generated by MakeMaker.
30
75f92628 31=over 5
32
33=item B<-C++>
34
35Adds ``extern "C"'' to the C code.
36
75f92628 37=item B<-except>
38
39Adds exception handling stubs to the C code.
40
41=item B<-typemap typemap>
42
43Indicates that a user-supplied typemap should take precedence over the
44default typemaps. This option may be used multiple times, with the last
45typemap having the highest precedence.
46
8e07c86e 47=item B<-v>
48
49Prints the I<xsubpp> version number to standard output, then exits.
50
8fc38fda 51=item B<-prototypes>
382b8d97 52
8fc38fda 53By default I<xsubpp> will not automatically generate prototype code for
54all xsubs. This flag will enable prototypes.
55
56=item B<-noversioncheck>
57
58Disables the run time test that determines if the object file (derived
59from the C<.xs> file) and the C<.pm> files have the same version
60number.
382b8d97 61
6f1abe2b 62=item B<-nolinenumbers>
63
64Prevents the inclusion of `#line' directives in the output.
65
b26a54d0 66=item B<-nooptimize>
67
68Disables certain optimizations. The only optimization that is currently
69affected is the use of I<target>s by the output C code (see L<perlguts>).
70This may significantly slow down the generated code, but this is the way
71B<xsubpp> of 5.005 and earlier operated.
72
11416672 73=item B<-noinout>
74
75Disable recognition of C<IN>, C<OUT_LIST> and C<INOUT_LIST> declarations.
76
77=item B<-noargtypes>
78
79Disable recognition of ANSI-like descriptions of function signature.
80
c5be433b 81=back
75f92628 82
83=head1 ENVIRONMENT
84
85No environment variables are used.
86
87=head1 AUTHOR
88
89Larry Wall
90
f06db76b 91=head1 MODIFICATION HISTORY
92
8e07c86e 93See the file F<changes.pod>.
e50aee73 94
75f92628 95=head1 SEE ALSO
96
55a00e51 97perl(1), perlxs(1), perlxstut(1)
75f92628 98
99=cut
93a17b20 100
382b8d97 101require 5.002;
774d564b 102use Cwd;
4230ab3f 103use vars '$cplusplus';
7ad6fb0b 104use vars '%v';
382b8d97 105
01f988be 106use Config;
107
aa689395 108sub Q ;
109
774d564b 110# Global Constants
774d564b 111
7817ba4d 112$XSUBPP_version = "1.9508";
aa689395 113
114my ($Is_VMS, $SymSet);
115if ($^O eq 'VMS') {
116 $Is_VMS = 1;
117 # Establish set of global symbols with max length 28, since xsubpp
118 # will later add the 'XS_' prefix.
119 require ExtUtils::XSSymSet;
120 $SymSet = new ExtUtils::XSSymSet 28;
121}
8fc38fda 122
c07a80fd 123$FH = 'File0000' ;
8fc38fda 124
11416672 125$usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-nooptimize] [-noinout] [-noargtypes] [-s pattern] [-typemap typemap]... file.xs\n";
f06db76b 126
382b8d97 127$proto_re = "[" . quotemeta('\$%&*@;') . "]" ;
93a17b20 128
8e07c86e 129$except = "";
8fc38fda 130$WantPrototypes = -1 ;
131$WantVersionChk = 1 ;
132$ProtoUsed = 0 ;
6f1abe2b 133$WantLineNumbers = 1 ;
b26a54d0 134$WantOptimize = 1 ;
11416672 135
136my $process_inout = 1;
137my $process_argtypes = 1;
138
8e07c86e 139SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) {
93a17b20 140 $flag = shift @ARGV;
e50aee73 141 $flag =~ s/^-// ;
ff68c719 142 $spat = quotemeta shift, next SWITCH if $flag eq 's';
8990e307 143 $cplusplus = 1, next SWITCH if $flag eq 'C++';
382b8d97 144 $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes';
145 $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes';
8fc38fda 146 $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck';
147 $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck';
c5be433b 148 # XXX left this in for compat
acfe0abc 149 next SWITCH if $flag eq 'object_capi';
8e07c86e 150 $except = " TRY", next SWITCH if $flag eq 'except';
8990e307 151 push(@tm,shift), next SWITCH if $flag eq 'typemap';
6f1abe2b 152 $WantLineNumbers = 0, next SWITCH if $flag eq 'nolinenumbers';
153 $WantLineNumbers = 1, next SWITCH if $flag eq 'linenumbers';
b26a54d0 154 $WantOptimize = 0, next SWITCH if $flag eq 'nooptimize';
155 $WantOptimize = 1, next SWITCH if $flag eq 'optimize';
11416672 156 $process_inout = 0, next SWITCH if $flag eq 'noinout';
157 $process_inout = 1, next SWITCH if $flag eq 'inout';
158 $process_argtypes = 0, next SWITCH if $flag eq 'noargtypes';
159 $process_argtypes = 1, next SWITCH if $flag eq 'argtypes';
b26a54d0 160 (print "xsubpp version $XSUBPP_version\n"), exit
8e07c86e 161 if $flag eq 'v';
93a17b20 162 die $usage;
163}
8fc38fda 164if ($WantPrototypes == -1)
165 { $WantPrototypes = 0}
166else
167 { $ProtoUsed = 1 }
168
169
8990e307 170@ARGV == 1 or die $usage;
c2960299 171($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)#
57497940 172 or ($dir, $filename) = $ARGV[0] =~ m#(.*)\\(.*)#
c2960299 173 or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)#
8990e307 174 or ($dir, $filename) = ('.', $ARGV[0]);
175chdir($dir);
774d564b 176$pwd = cwd();
8fc38fda 177
178++ $IncludedFiles{$ARGV[0]} ;
93a17b20 179
4230ab3f 180my(@XSStack) = ({type => 'none'}); # Stack of conditionals and INCLUDEs
181my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA");
aa689395 182
4230ab3f 183
f06db76b 184sub TrimWhitespace
185{
186 $_[0] =~ s/^\s+|\s+$//go ;
187}
188
189sub TidyType
190{
191 local ($_) = @_ ;
192
193 # rationalise any '*' by joining them into bunches and removing whitespace
194 s#\s*(\*+)\s*#$1#g;
e50aee73 195 s#(\*+)# $1 #g ;
f06db76b 196
197 # change multiple whitespace into a single space
198 s/\s+/ /g ;
199
200 # trim leading & trailing whitespace
201 TrimWhitespace($_) ;
202
203 $_ ;
204}
205
93a17b20 206$typemap = shift @ARGV;
8990e307 207foreach $typemap (@tm) {
208 die "Can't find $typemap in $pwd\n" unless -r $typemap;
93a17b20 209}
748a9306 210unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap
211 ../../lib/ExtUtils/typemap ../../../typemap ../../typemap
212 ../typemap typemap);
8990e307 213foreach $typemap (@tm) {
f06db76b 214 next unless -e $typemap ;
215 # skip directories, binary files etc.
216 warn("Warning: ignoring non-text typemap file '$typemap'\n"), next
217 unless -T $typemap ;
218 open(TYPEMAP, $typemap)
219 or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
e50aee73 220 $mode = 'Typemap';
c2960299 221 $junk = "" ;
8990e307 222 $current = \$junk;
223 while (<TYPEMAP>) {
e50aee73 224 next if /^\s*#/;
e1f0c0aa 225 my $line_no = $. + 1;
8e07c86e 226 if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; }
227 if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; }
228 if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; }
e50aee73 229 if ($mode eq 'Typemap') {
230 chomp;
f06db76b 231 my $line = $_ ;
232 TrimWhitespace($_) ;
233 # skip blank lines and comment lines
234 next if /^$/ or /^#/ ;
382b8d97 235 my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or
236 warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next;
237 $type = TidyType($type) ;
238 $type_kind{$type} = $kind ;
239 # prototype defaults to '$'
93d3b392 240 $proto = "\$" unless $proto ;
382b8d97 241 warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n")
242 unless ValidProtoString($proto) ;
243 $proto_letter{$type} = C_string($proto) ;
8e07c86e 244 }
245 elsif (/^\s/) {
246 $$current .= $_;
463ee0b2 247 }
e50aee73 248 elsif ($mode eq 'Input') {
8e07c86e 249 s/\s+$//;
250 $input_expr{$_} = '';
251 $current = \$input_expr{$_};
93a17b20 252 }
8990e307 253 else {
8e07c86e 254 s/\s+$//;
255 $output_expr{$_} = '';
256 $current = \$output_expr{$_};
93a17b20 257 }
8990e307 258 }
259 close(TYPEMAP);
260}
93a17b20 261
8990e307 262foreach $key (keys %input_expr) {
263 $input_expr{$key} =~ s/\n+$//;
264}
93a17b20 265
14455d6c 266$bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced
b26a54d0 267$cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast
14455d6c 268$size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn)
b26a54d0 269
270foreach $key (keys %output_expr) {
271 use re 'eval';
272
273 my ($t, $with_size, $arg, $sarg) =
274 ($output_expr{$key} =~
438cc608 275 m[^ \s+ sv_set ( [iunp] ) v (n)? # Type, is_setpvn
b26a54d0 276 \s* \( \s* $cast \$arg \s* ,
14455d6c 277 \s* ( (??{ $bal }) ) # Set from
278 ( (??{ $size }) )? # Possible sizeof set-from
b26a54d0 279 \) \s* ; \s* $
280 ]x);
281 $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
282}
283
8e07c86e 284$END = "!End!\n\n"; # "impossible" keyword (multiple newline)
285
286# Match an XS keyword
382b8d97 287$BLOCK_re= '\s*(' . join('|', qw(
288 REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT
be3174d2 289 CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE
63385af5 290 SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL
382b8d97 291 )) . "|$END)\\s*:";
8e07c86e 292
293# Input: ($_, @line) == unparsed input.
294# Output: ($_, @line) == (rest of line, following lines).
295# Return: the matched keyword if found, otherwise 0
296sub check_keyword {
297 $_ = shift(@line) while !/\S/ && @line;
298 s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2;
299}
300
efbca139 301my ($C_group_rex, $C_arg);
f8b8e0a4 302# Group in C (no support for comments or literals)
303$C_group_rex = qr/ [({\[]
14455d6c 304 (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
f8b8e0a4 305 [)}\]] /x ;
306# Chunk in C without comma at toplevel (no comments):
307$C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
14455d6c 308 | (??{ $C_group_rex })
f8b8e0a4 309 | " (?: (?> [^\\"]+ )
310 | \\.
311 )* " # String literal
312 | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
313 )* /xs;
8e07c86e 314
6f1abe2b 315if ($WantLineNumbers) {
316 {
317 package xsubpp::counter;
318 sub TIEHANDLE {
319 my ($class, $cfile) = @_;
320 my $buf = "";
321 $SECTION_END_MARKER = "#line --- \"$cfile\"";
322 $line_no = 1;
323 bless \$buf;
324 }
325
326 sub PRINT {
327 my $self = shift;
328 for (@_) {
329 $$self .= $_;
330 while ($$self =~ s/^([^\n]*\n)//) {
331 my $line = $1;
332 ++ $line_no;
333 $line =~ s|^\#line\s+---(?=\s)|#line $line_no|;
334 print STDOUT $line;
335 }
336 }
337 }
338
339 sub PRINTF {
340 my $self = shift;
341 my $fmt = shift;
342 $self->PRINT(sprintf($fmt, @_));
343 }
344
345 sub DESTROY {
346 # Not necessary if we're careful to end with a "\n"
347 my $self = shift;
348 print STDOUT $$self;
349 }
350 }
351
352 my $cfile = $filename;
353 $cfile =~ s/\.xs$/.c/i or $cfile .= ".c";
354 tie(*PSEUDO_STDOUT, 'xsubpp::counter', $cfile);
355 select PSEUDO_STDOUT;
356}
357
8e07c86e 358sub print_section {
6f1abe2b 359 # the "do" is required for right semantics
360 do { $_ = shift(@line) } while !/\S/ && @line;
361
362 print("#line ", $line_no[@line_no - @line -1], " \"$filename\"\n")
d3308daf 363 if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/;
8e07c86e 364 for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
365 print "$_\n";
366 }
6f1abe2b 367 print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
8e07c86e 368}
369
cfc02341 370sub merge_section {
371 my $in = '';
372
373 while (!/\S/ && @line) {
374 $_ = shift(@line);
375 }
376
377 for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
378 $in .= "$_\n";
379 }
380 chomp $in;
381 return $in;
382}
383
8fc38fda 384sub process_keyword($)
385{
386 my($pattern) = @_ ;
387 my $kwd ;
388
389 &{"${kwd}_handler"}()
390 while $kwd = check_keyword($pattern) ;
391}
392
8e07c86e 393sub CASE_handler {
394 blurt ("Error: `CASE:' after unconditional `CASE:'")
395 if $condnum && $cond eq '';
396 $cond = $_;
397 TrimWhitespace($cond);
398 print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n");
399 $_ = '' ;
400}
401
402sub INPUT_handler {
403 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
404 last if /^\s*NOT_IMPLEMENTED_YET/;
405 next unless /\S/; # skip blank lines
406
407 TrimWhitespace($_) ;
408 my $line = $_ ;
409
410 # remove trailing semicolon if no initialisation
7ad6fb0b 411 s/\s*;$//g unless /[=;+].*\S/ ;
8e07c86e 412
413 # check for optional initialisation code
414 my $var_init = '' ;
7ad6fb0b 415 $var_init = $1 if s/\s*([=;+].*)$//s ;
8e07c86e 416 $var_init =~ s/"/\\"/g;
417
418 s/\s+/ /g;
0f568861 419 my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s
8e07c86e 420 or blurt("Error: invalid argument declaration '$line'"), next;
421
422 # Check for duplicate definitions
423 blurt ("Error: duplicate definition of argument '$var_name' ignored"), next
f8b8e0a4 424 if $arg_list{$var_name}++
425 or defined $arg_types{$var_name} and not $processing_arg_with_types;
8e07c86e 426
427 $thisdone |= $var_name eq "THIS";
428 $retvaldone |= $var_name eq "RETVAL";
429 $var_types{$var_name} = $var_type;
ddf6bed1 430 # XXXX This check is a safeguard against the unfinished conversion of
431 # generate_init(). When generate_init() is fixed,
432 # one can use 2-args map_type() unconditionally.
433 if ($var_type =~ / \( \s* \* \s* \) /x) {
434 # Function pointers are not yet supported with &output_init!
435 print "\t" . &map_type($var_type, $var_name);
436 $name_printed = 1;
437 } else {
438 print "\t" . &map_type($var_type);
439 $name_printed = 0;
440 }
8e07c86e 441 $var_num = $args_match{$var_name};
382b8d97 442
8fc38fda 443 $proto_arg[$var_num] = ProtoString($var_type)
444 if $var_num ;
0f568861 445 $func_args =~ s/\b($var_name)\b/&$1/ if $var_addr;
f8b8e0a4 446 if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/
0f568861 447 or $in_out{$var_name} and $in_out{$var_name} =~ /^OUT/
f8b8e0a4 448 and $var_init !~ /\S/) {
ddf6bed1 449 if ($name_printed) {
450 print ";\n";
451 } else {
9bea678f 452 print "\t$var_name;\n";
ddf6bed1 453 }
8e07c86e 454 } elsif ($var_init =~ /\S/) {
ddf6bed1 455 &output_init($var_type, $var_num, $var_name, $var_init, $name_printed);
8e07c86e 456 } elsif ($var_num) {
457 # generate initialization code
ddf6bed1 458 &generate_init($var_type, $var_num, $var_name, $name_printed);
8e07c86e 459 } else {
460 print ";\n";
461 }
462 }
463}
464
465sub OUTPUT_handler {
466 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
467 next unless /\S/;
ef50df4b 468 if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
469 $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0);
470 next;
471 }
8e07c86e 472 my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ;
473 blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next
474 if $outargs{$outarg} ++ ;
475 if (!$gotRETVAL and $outarg eq 'RETVAL') {
476 # deal with RETVAL last
477 $RETVAL_code = $outcode ;
478 $gotRETVAL = 1 ;
479 next ;
480 }
481 blurt ("Error: OUTPUT $outarg not an argument"), next
482 unless defined($args_match{$outarg});
483 blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next
484 unless defined $var_types{$outarg} ;
f78230ad 485 $var_num = $args_match{$outarg};
8e07c86e 486 if ($outcode) {
487 print "\t$outcode\n";
f78230ad 488 print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic;
8e07c86e 489 } else {
ef50df4b 490 &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic);
8e07c86e 491 }
0f568861 492 delete $in_out{$outarg} # No need to auto-OUTPUT
493 if exists $in_out{$outarg} and $in_out{$outarg} =~ /OUT$/;
8e07c86e 494 }
495}
496
cfc02341 497sub C_ARGS_handler() {
498 my $in = merge_section();
499
500 TrimWhitespace($in);
501 $func_args = $in;
502}
503
504sub INTERFACE_MACRO_handler() {
505 my $in = merge_section();
506
507 TrimWhitespace($in);
508 if ($in =~ /\s/) { # two
509 ($interface_macro, $interface_macro_set) = split ' ', $in;
510 } else {
511 $interface_macro = $in;
512 $interface_macro_set = 'UNKNOWN_CVT'; # catch later
513 }
514 $interface = 1; # local
515 $Interfaces = 1; # global
516}
517
518sub INTERFACE_handler() {
519 my $in = merge_section();
520
521 TrimWhitespace($in);
522
523 foreach (split /[\s,]+/, $in) {
524 $Interfaces{$_} = $_;
525 }
526 print Q<<"EOF";
527# XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr);
528EOF
529 $interface = 1; # local
530 $Interfaces = 1; # global
531}
532
8fc38fda 533sub CLEANUP_handler() { print_section() }
534sub PREINIT_handler() { print_section() }
63385af5 535sub POSTCALL_handler() { print_section() }
8fc38fda 536sub INIT_handler() { print_section() }
537
8e07c86e 538sub GetAliases
539{
540 my ($line) = @_ ;
541 my ($orig) = $line ;
542 my ($alias) ;
543 my ($value) ;
544
545 # Parse alias definitions
546 # format is
547 # alias = value alias = value ...
548
549 while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) {
550 $alias = $1 ;
551 $orig_alias = $alias ;
552 $value = $2 ;
553
554 # check for optional package definition in the alias
555 $alias = $Packprefix . $alias if $alias !~ /::/ ;
556
557 # check for duplicate alias name & duplicate value
558 Warn("Warning: Ignoring duplicate alias '$orig_alias'")
4230ab3f 559 if defined $XsubAliases{$alias} ;
8e07c86e 560
4230ab3f 561 Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values")
562 if $XsubAliasValues{$value} ;
8e07c86e 563
4230ab3f 564 $XsubAliases = 1;
565 $XsubAliases{$alias} = $value ;
566 $XsubAliasValues{$value} = $orig_alias ;
8e07c86e 567 }
568
569 blurt("Error: Cannot parse ALIAS definitions from '$orig'")
570 if $line ;
571}
572
be3174d2 573sub ATTRS_handler ()
574{
575 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
576 next unless /\S/;
577 TrimWhitespace($_) ;
578 push @Attributes, $_;
579 }
580}
581
382b8d97 582sub ALIAS_handler ()
8e07c86e 583{
584 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
585 next unless /\S/;
586 TrimWhitespace($_) ;
587 GetAliases($_) if $_ ;
588 }
589}
590
382b8d97 591sub REQUIRE_handler ()
8e07c86e 592{
593 # the rest of the current line should contain a version number
594 my ($Ver) = $_ ;
595
596 TrimWhitespace($Ver) ;
597
598 death ("Error: REQUIRE expects a version number")
599 unless $Ver ;
600
601 # check that the version number is of the form n.n
602 death ("Error: REQUIRE: expected a number, got '$Ver'")
603 unless $Ver =~ /^\d+(\.\d*)?/ ;
604
605 death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.")
606 unless $XSUBPP_version >= $Ver ;
607}
608
8fc38fda 609sub VERSIONCHECK_handler ()
610{
611 # the rest of the current line should contain either ENABLE or
612 # DISABLE
613
614 TrimWhitespace($_) ;
615
616 # check for ENABLE/DISABLE
617 death ("Error: VERSIONCHECK: ENABLE/DISABLE")
618 unless /^(ENABLE|DISABLE)/i ;
619
620 $WantVersionChk = 1 if $1 eq 'ENABLE' ;
621 $WantVersionChk = 0 if $1 eq 'DISABLE' ;
622
623}
624
382b8d97 625sub PROTOTYPE_handler ()
626{
7d41bd0a 627 my $specified ;
628
c07a80fd 629 death("Error: Only 1 PROTOTYPE definition allowed per xsub")
630 if $proto_in_this_xsub ++ ;
631
382b8d97 632 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
633 next unless /\S/;
7d41bd0a 634 $specified = 1 ;
382b8d97 635 TrimWhitespace($_) ;
636 if ($_ eq 'DISABLE') {
637 $ProtoThisXSUB = 0
638 }
639 elsif ($_ eq 'ENABLE') {
640 $ProtoThisXSUB = 1
641 }
642 else {
643 # remove any whitespace
644 s/\s+//g ;
645 death("Error: Invalid prototype '$_'")
646 unless ValidProtoString($_) ;
647 $ProtoThisXSUB = C_string($_) ;
648 }
649 }
c07a80fd 650
7d41bd0a 651 # If no prototype specified, then assume empty prototype ""
652 $ProtoThisXSUB = 2 unless $specified ;
653
8fc38fda 654 $ProtoUsed = 1 ;
c07a80fd 655
382b8d97 656}
657
db3b9414 658sub SCOPE_handler ()
659{
660 death("Error: Only 1 SCOPE declaration allowed per xsub")
661 if $scope_in_this_xsub ++ ;
662
663 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
664 next unless /\S/;
665 TrimWhitespace($_) ;
666 if ($_ =~ /^DISABLE/i) {
667 $ScopeThisXSUB = 0
668 }
669 elsif ($_ =~ /^ENABLE/i) {
670 $ScopeThisXSUB = 1
671 }
672 }
673
674}
675
382b8d97 676sub PROTOTYPES_handler ()
677{
678 # the rest of the current line should contain either ENABLE or
679 # DISABLE
680
681 TrimWhitespace($_) ;
682
683 # check for ENABLE/DISABLE
684 death ("Error: PROTOTYPES: ENABLE/DISABLE")
685 unless /^(ENABLE|DISABLE)/i ;
686
687 $WantPrototypes = 1 if $1 eq 'ENABLE' ;
688 $WantPrototypes = 0 if $1 eq 'DISABLE' ;
8fc38fda 689 $ProtoUsed = 1 ;
382b8d97 690
691}
692
8fc38fda 693sub INCLUDE_handler ()
694{
695 # the rest of the current line should contain a valid filename
696
697 TrimWhitespace($_) ;
698
8fc38fda 699 death("INCLUDE: filename missing")
700 unless $_ ;
701
702 death("INCLUDE: output pipe is illegal")
703 if /^\s*\|/ ;
704
705 # simple minded recursion detector
706 death("INCLUDE loop detected")
707 if $IncludedFiles{$_} ;
708
709 ++ $IncludedFiles{$_} unless /\|\s*$/ ;
710
711 # Save the current file context.
4230ab3f 712 push(@XSStack, {
713 type => 'file',
8fc38fda 714 LastLine => $lastline,
715 LastLineNo => $lastline_no,
716 Line => \@line,
717 LineNo => \@line_no,
718 Filename => $filename,
c07a80fd 719 Handle => $FH,
8fc38fda 720 }) ;
721
c07a80fd 722 ++ $FH ;
8fc38fda 723
724 # open the new file
c07a80fd 725 open ($FH, "$_") or death("Cannot open '$_': $!") ;
8fc38fda 726
727 print Q<<"EOF" ;
728#
729#/* INCLUDE: Including '$_' from '$filename' */
730#
731EOF
732
8fc38fda 733 $filename = $_ ;
734
c07a80fd 735 # Prime the pump by reading the first
736 # non-blank line
737
738 # skip leading blank lines
739 while (<$FH>) {
740 last unless /^\s*$/ ;
741 }
742
743 $lastline = $_ ;
8fc38fda 744 $lastline_no = $. ;
745
746}
747
748sub PopFile()
749{
4230ab3f 750 return 0 unless $XSStack[-1]{type} eq 'file' ;
751
752 my $data = pop @XSStack ;
8fc38fda 753 my $ThisFile = $filename ;
754 my $isPipe = ($filename =~ /\|\s*$/) ;
755
756 -- $IncludedFiles{$filename}
757 unless $isPipe ;
758
c07a80fd 759 close $FH ;
8fc38fda 760
c07a80fd 761 $FH = $data->{Handle} ;
8fc38fda 762 $filename = $data->{Filename} ;
763 $lastline = $data->{LastLine} ;
764 $lastline_no = $data->{LastLineNo} ;
765 @line = @{ $data->{Line} } ;
766 @line_no = @{ $data->{LineNo} } ;
4230ab3f 767
8fc38fda 768 if ($isPipe and $? ) {
769 -- $lastline_no ;
770 print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ;
771 exit 1 ;
772 }
773
774 print Q<<"EOF" ;
775#
776#/* INCLUDE: Returning to '$filename' from '$ThisFile' */
777#
778EOF
779
780 return 1 ;
781}
782
382b8d97 783sub ValidProtoString ($)
784{
785 my($string) = @_ ;
786
787 if ( $string =~ /^$proto_re+$/ ) {
788 return $string ;
789 }
790
791 return 0 ;
792}
793
794sub C_string ($)
795{
796 my($string) = @_ ;
797
798 $string =~ s[\\][\\\\]g ;
799 $string ;
800}
801
802sub ProtoString ($)
803{
804 my ($type) = @_ ;
805
93d3b392 806 $proto_letter{$type} or "\$" ;
382b8d97 807}
808
8e07c86e 809sub check_cpp {
810 my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line);
811 if (@cpp) {
812 my ($cpp, $cpplevel);
813 for $cpp (@cpp) {
814 if ($cpp =~ /^\#\s*if/) {
815 $cpplevel++;
816 } elsif (!$cpplevel) {
817 Warn("Warning: #else/elif/endif without #if in this function");
4230ab3f 818 print STDERR " (precede it with a blank line if the matching #if is outside the function)\n"
819 if $XSStack[-1]{type} eq 'if';
8e07c86e 820 return;
821 } elsif ($cpp =~ /^\#\s*endif/) {
822 $cpplevel--;
823 }
824 }
825 Warn("Warning: #if without #endif in this function") if $cpplevel;
826 }
827}
828
829
8990e307 830sub Q {
e50aee73 831 my($text) = @_;
4633a7c4 832 $text =~ s/^#//gm;
2304df62 833 $text =~ s/\[\[/{/g;
834 $text =~ s/\]\]/}/g;
8990e307 835 $text;
93a17b20 836}
837
c07a80fd 838open($FH, $filename) or die "cannot open $filename: $!\n";
c2960299 839
f06db76b 840# Identify the version of xsubpp used
f06db76b 841print <<EOM ;
e50aee73 842/*
843 * This file was generated automatically by xsubpp version $XSUBPP_version from the
93d3b392 844 * contents of $filename. Do not edit this file, edit $filename instead.
e50aee73 845 *
846 * ANY CHANGES MADE HERE WILL BE LOST!
f06db76b 847 *
848 */
e50aee73 849
f06db76b 850EOM
6f1abe2b 851
852
853print("#line 1 \"$filename\"\n")
854 if $WantLineNumbers;
f06db76b 855
e03d20b3 856firstmodule:
c07a80fd 857while (<$FH>) {
e03d20b3 858 if (/^=/) {
7817ba4d 859 my $podstartline = $.;
e03d20b3 860 do {
7817ba4d 861 if (/^=cut\s*$/) {
862 print("/* Skipped embedded POD. */\n");
863 printf("#line %d \"$filename\"\n", $. + 1)
864 if $WantLineNumbers;
865 next firstmodule
866 }
867
e03d20b3 868 } while (<$FH>);
7817ba4d 869 # At this point $. is at end of file so die won't state the start
870 # of the problem, and as we haven't yet read any lines &death won't
871 # show the correct line in the message either.
872 die ("Error: Unterminated pod in $filename, line $podstartline\n")
873 unless $lastline;
e03d20b3 874 }
e50aee73 875 last if ($Module, $Package, $Prefix) =
876 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
01f988be 877
a0d0e21e 878 print $_;
93a17b20 879}
e50aee73 880&Exit unless defined $_;
881
cfc02341 882print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
883
8fc38fda 884$lastline = $_;
885$lastline_no = $.;
93a17b20 886
c07a80fd 887# Read next xsub into @line from ($lastline, <$FH>).
2304df62 888sub fetch_para {
889 # parse paragraph
4230ab3f 890 death ("Error: Unterminated `#if/#ifdef/#ifndef'")
891 if !defined $lastline && $XSStack[-1]{type} eq 'if';
2304df62 892 @line = ();
c2960299 893 @line_no = () ;
4230ab3f 894 return PopFile() if !defined $lastline;
e50aee73 895
896 if ($lastline =~
897 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
898 $Module = $1;
8e07c86e 899 $Package = defined($2) ? $2 : ''; # keep -w happy
900 $Prefix = defined($3) ? $3 : ''; # keep -w happy
ff68c719 901 $Prefix = quotemeta $Prefix ;
e50aee73 902 ($Module_cname = $Module) =~ s/\W/_/g;
8e07c86e 903 ($Packid = $Package) =~ tr/:/_/;
e50aee73 904 $Packprefix = $Package;
8e07c86e 905 $Packprefix .= "::" if $Packprefix ne "";
2304df62 906 $lastline = "";
e50aee73 907 }
908
909 for(;;) {
e03d20b3 910 # Skip embedded PODs
413e5597 911 while ($lastline =~ /^=/) {
e03d20b3 912 while ($lastline = <$FH>) {
913 last if ($lastline =~ /^=cut\s*$/);
914 }
915 death ("Error: Unterminated pod") unless $lastline;
916 $lastline = <$FH>;
917 chomp $lastline;
918 $lastline =~ s/^\s+$//;
919 }
e50aee73 920 if ($lastline !~ /^\s*#/ ||
4230ab3f 921 # CPP directives:
922 # ANSI: if ifdef ifndef elif else endif define undef
923 # line error pragma
924 # gcc: warning include_next
925 # obj-c: import
926 # others: ident (gcc notes that some cpps have this one)
927 $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
e50aee73 928 last if $lastline =~ /^\S/ && @line && $line[-1] eq "";
929 push(@line, $lastline);
930 push(@line_no, $lastline_no) ;
93a17b20 931 }
e50aee73 932
933 # Read next line and continuation lines
c07a80fd 934 last unless defined($lastline = <$FH>);
e50aee73 935 $lastline_no = $.;
936 my $tmp_line;
937 $lastline .= $tmp_line
c07a80fd 938 while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>));
f8b8e0a4 939
8e07c86e 940 chomp $lastline;
e50aee73 941 $lastline =~ s/^\s+$//;
2304df62 942 }
e50aee73 943 pop(@line), pop(@line_no) while @line && $line[-1] eq "";
e50aee73 944 1;
2304df62 945}
93a17b20 946
c2960299 947PARAGRAPH:
8e07c86e 948while (fetch_para()) {
e50aee73 949 # Print initial preprocessor statements and blank lines
4230ab3f 950 while (@line && $line[0] !~ /^[^\#]/) {
951 my $line = shift(@line);
952 print $line, "\n";
953 next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
954 my $statement = $+;
955 if ($statement eq 'if') {
956 $XSS_work_idx = @XSStack;
957 push(@XSStack, {type => 'if'});
958 } else {
959 death ("Error: `$statement' with no matching `if'")
960 if $XSStack[-1]{type} ne 'if';
961 if ($XSStack[-1]{varname}) {
962 push(@InitFileCode, "#endif\n");
963 push(@BootCode, "#endif");
964 }
965
966 my(@fns) = keys %{$XSStack[-1]{functions}};
967 if ($statement ne 'endif') {
968 # Hide the functions defined in other #if branches, and reset.
969 @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns;
970 @{$XSStack[-1]}{qw(varname functions)} = ('', {});
971 } else {
972 my($tmp) = pop(@XSStack);
973 0 while (--$XSS_work_idx
974 && $XSStack[$XSS_work_idx]{type} ne 'if');
975 # Keep all new defined functions
976 push(@fns, keys %{$tmp->{other_functions}});
977 @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
978 }
979 }
980 }
e50aee73 981
982 next PARAGRAPH unless @line;
983
4230ab3f 984 if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) {
985 # We are inside an #if, but have not yet #defined its xsubpp variable.
986 print "#define $cpp_next_tmp 1\n\n";
987 push(@InitFileCode, "#if $cpp_next_tmp\n");
988 push(@BootCode, "#if $cpp_next_tmp");
989 $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++;
990 }
991
55497cff 992 death ("Code is not inside a function"
993 ." (maybe last function was ended by a blank line "
6d087280 994 ." followed by a statement on column one?)")
e50aee73 995 if $line[0] =~ /^\s/;
996
2304df62 997 # initialize info arrays
998 undef(%args_match);
999 undef(%var_types);
2304df62 1000 undef(%defaults);
1001 undef($class);
1002 undef($static);
1003 undef($elipsis);
f06db76b 1004 undef($wantRETVAL) ;
f8b8e0a4 1005 undef($RETVAL_no_return) ;
f06db76b 1006 undef(%arg_list) ;
382b8d97 1007 undef(@proto_arg) ;
f8b8e0a4 1008 undef(@arg_with_types) ;
1009 undef($processing_arg_with_types) ;
1010 undef(%arg_types) ;
0f568861 1011 undef(@outlist) ;
f8b8e0a4 1012 undef(%in_out) ;
c07a80fd 1013 undef($proto_in_this_xsub) ;
db3b9414 1014 undef($scope_in_this_xsub) ;
cfc02341 1015 undef($interface);
f8b8e0a4 1016 undef($prepush_done);
cfc02341 1017 $interface_macro = 'XSINTERFACE_FUNC' ;
1018 $interface_macro_set = 'XSINTERFACE_FUNC_SET' ;
382b8d97 1019 $ProtoThisXSUB = $WantPrototypes ;
db3b9414 1020 $ScopeThisXSUB = 0;
f8b8e0a4 1021 $xsreturn = 0;
2304df62 1022
8e07c86e 1023 $_ = shift(@line);
8fc38fda 1024 while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) {
1025 &{"${kwd}_handler"}() ;
8e07c86e 1026 next PARAGRAPH unless @line ;
1027 $_ = shift(@line);
1028 }
c2960299 1029
8e07c86e 1030 if (check_keyword("BOOT")) {
1031 &check_cpp;
6f1abe2b 1032 push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"")
1033 if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/;
1034 push (@BootCode, @line, "") ;
c2960299 1035 next PARAGRAPH ;
a0d0e21e 1036 }
c2960299 1037
8e07c86e 1038
1039 # extract return type, function name and arguments
cfc02341 1040 ($ret_type) = TidyType($_);
f8b8e0a4 1041 $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//;
8e07c86e 1042
11416672 1043 # Allow one-line ANSI-like declaration
1044 unshift @line, $2
1045 if $process_argtypes
1046 and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s;
1047
c2960299 1048 # a function definition needs at least 2 lines
1049 blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH
1050 unless @line ;
1051
8e07c86e 1052 $static = 1 if $ret_type =~ s/^static\s+//;
1053
2304df62 1054 $func_header = shift(@line);
c2960299 1055 blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH
f8b8e0a4 1056 unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
c2960299 1057
8e07c86e 1058 ($class, $func_name, $orig_args) = ($1, $2, $3) ;
f480b56a 1059 $class = "$4 $class" if $4;
2304df62 1060 ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/;
ff68c719 1061 ($clean_func_name = $func_name) =~ s/^$Prefix//;
1062 $Full_func_name = "${Packid}_$clean_func_name";
ff0cee69 1063 if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); }
c2960299 1064
1065 # Check for duplicate function definition
4230ab3f 1066 for $tmp (@XSStack) {
1067 next unless defined $tmp->{functions}{$Full_func_name};
ff68c719 1068 Warn("Warning: duplicate function definition '$clean_func_name' detected");
4230ab3f 1069 last;
8e07c86e 1070 }
4230ab3f 1071 $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ;
be3174d2 1072 %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = ();
ef50df4b 1073 $DoSetMagic = 1;
c2960299 1074
f8b8e0a4 1075 $orig_args =~ s/\\\s*/ /g; # process line continuations
1076
cb79badd 1077 my %only_outlist;
f8b8e0a4 1078 if ($process_argtypes and $orig_args =~ /\S/) {
1079 my $args = "$orig_args ,";
14455d6c 1080 if ($args =~ /^( (??{ $C_arg }) , )* $ /x) {
1081 @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg);
f8b8e0a4 1082 for ( @args ) {
1083 s/^\s+//;
1084 s/\s+$//;
1085 my $arg = $_;
1086 my $default;
1087 ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x;
1088 my ($pre, $name) = ($arg =~ /(.*?) \s* \b(\w+) \s* $ /x);
1089 next unless length $pre;
1090 my $out_type;
1091 my $inout_var;
0f568861 1092 if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//) {
f8b8e0a4 1093 my $type = $1;
63385af5 1094 $out_type = $type if $type ne 'IN';
0f568861 1095 $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//;
f8b8e0a4 1096 }
1097 if (/\W/) { # Has a type
1098 push @arg_with_types, $arg;
1099 # warn "pushing '$arg'\n";
1100 $arg_types{$name} = $arg;
1101 $_ = "$name$default";
1102 }
cb79badd 1103 $only_outlist{$_} = 1 if $out_type eq "OUTLIST";
0f568861 1104 push @outlist, $name if $out_type =~ /OUTLIST$/;
f8b8e0a4 1105 $in_out{$name} = $out_type if $out_type;
1106 }
1107 } else {
1108 @args = split(/\s*,\s*/, $orig_args);
1109 Warn("Warning: cannot parse argument list '$orig_args', fallback to split");
1110 }
1111 } else {
1112 @args = split(/\s*,\s*/, $orig_args);
1113 for (@args) {
0f568861 1114 if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\s+//) {
f8b8e0a4 1115 my $out_type = $1;
63385af5 1116 next if $out_type eq 'IN';
cb79badd 1117 $only_outlist{$_} = 1 if $out_type eq "OUTLIST";
0f568861 1118 push @outlist, $name if $out_type =~ /OUTLIST$/;
f8b8e0a4 1119 $in_out{$_} = $out_type;
1120 }
1121 }
1122 }
a0d0e21e 1123 if (defined($class)) {
683d4eee 1124 my $arg0 = ((defined($static) or $func_name eq 'new')
1125 ? "CLASS" : "THIS");
8e07c86e 1126 unshift(@args, $arg0);
f8b8e0a4 1127 ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/;
2304df62 1128 }
f8b8e0a4 1129 my $extra_args = 0;
1130 @args_num = ();
1131 $num_args = 0;
1132 my $report_args = '';
1133 foreach $i (0 .. $#args) {
2304df62 1134 if ($args[$i] =~ s/\.\.\.//) {
1135 $elipsis = 1;
f8b8e0a4 1136 if ($args[$i] eq '' && $i == $#args) {
1137 $report_args .= ", ...";
2304df62 1138 pop(@args);
1139 last;
1140 }
1141 }
cb79badd 1142 if ($only_outlist{$args[$i]}) {
f8b8e0a4 1143 push @args_num, undef;
1144 } else {
1145 push @args_num, ++$num_args;
1146 $report_args .= ", $args[$i]";
1147 }
8e07c86e 1148 if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) {
f8b8e0a4 1149 $extra_args++;
2304df62 1150 $args[$i] = $1;
1151 $defaults{$args[$i]} = $2;
1152 $defaults{$args[$i]} =~ s/"/\\"/g;
1153 }
93d3b392 1154 $proto_arg[$i+1] = "\$" ;
2304df62 1155 }
f8b8e0a4 1156 $min_args = $num_args - $extra_args;
1157 $report_args =~ s/"/\\"/g;
1158 $report_args =~ s/^,\s+//;
1159 my @func_args = @args;
1160 shift @func_args if defined($class);
1161
1162 for (@func_args) {
1163 s/^/&/ if $in_out{$_};
2304df62 1164 }
f8b8e0a4 1165 $func_args = join(", ", @func_args);
1166 @args_match{@args} = @args_num;
2304df62 1167
8e07c86e 1168 $PPCODE = grep(/^\s*PPCODE\s*:/, @line);
93d3b392 1169 $CODE = grep(/^\s*CODE\s*:/, @line);
6c5fb52b 1170 # Detect CODE: blocks which use ST(n)= or XST_m*(n,v)
1171 # to set explicit return values.
1172 $EXPLICIT_RETURN = ($CODE &&
1173 ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x ));
8e07c86e 1174 $ALIAS = grep(/^\s*ALIAS\s*:/, @line);
cfc02341 1175 $INTERFACE = grep(/^\s*INTERFACE\s*:/, @line);
8e07c86e 1176
f8b8e0a4 1177 $xsreturn = 1 if $EXPLICIT_RETURN;
1178
2304df62 1179 # print function header
a0d0e21e 1180 print Q<<"EOF";
05ceb97a 1181#XS(XS_${Full_func_name}); /* prototype to pass -Wmissing-prototypes */
ff68c719 1182#XS(XS_${Full_func_name})
2304df62 1183#[[
a0d0e21e 1184# dXSARGS;
93a17b20 1185EOF
8e07c86e 1186 print Q<<"EOF" if $ALIAS ;
1187# dXSI32;
1188EOF
cfc02341 1189 print Q<<"EOF" if $INTERFACE ;
1190# dXSFUNCTION($ret_type);
1191EOF
2304df62 1192 if ($elipsis) {
8e07c86e 1193 $cond = ($min_args ? qq(items < $min_args) : 0);
2304df62 1194 }
1195 elsif ($min_args == $num_args) {
1196 $cond = qq(items != $min_args);
1197 }
1198 else {
1199 $cond = qq(items < $min_args || items > $num_args);
1200 }
8990e307 1201
2304df62 1202 print Q<<"EOF" if $except;
1203# char errbuf[1024];
1204# *errbuf = '\0';
1205EOF
1206
8e07c86e 1207 if ($ALIAS)
1208 { print Q<<"EOF" if $cond }
1209# if ($cond)
f8b8e0a4 1210# Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv)));
8e07c86e 1211EOF
1212 else
1213 { print Q<<"EOF" if $cond }
1214# if ($cond)
f8b8e0a4 1215# Perl_croak(aTHX_ "Usage: $pname($report_args)");
93a17b20 1216EOF
1217
349b520e 1218 #gcc -Wall: if an xsub has no arguments and PPCODE is used
1219 #it is likely none of ST, XSRETURN or XSprePUSH macros are used
1220 #hence `ax' (setup by dXSARGS) is unused
1221 #XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS
1222 #but such a move could break third-party extensions
1223 print Q<<"EOF" if $PPCODE and $num_args == 0;
1224# PERL_UNUSED_VAR(ax); /* -Wall */
1225EOF
1226
a0d0e21e 1227 print Q<<"EOF" if $PPCODE;
1228# SP -= items;
1229EOF
1230
2304df62 1231 # Now do a block of some sort.
93a17b20 1232
2304df62 1233 $condnum = 0;
8e07c86e 1234 $cond = ''; # last CASE: condidional
1235 push(@line, "$END:");
1236 push(@line_no, $line_no[-1]);
1237 $_ = '';
1238 &check_cpp;
2304df62 1239 while (@line) {
8e07c86e 1240 &CASE_handler if check_keyword("CASE");
1241 print Q<<"EOF";
1242# $except [[
93a17b20 1243EOF
1244
1245 # do initialization of input variables
1246 $thisdone = 0;
1247 $retvaldone = 0;
463ee0b2 1248 $deferred = "";
c2960299 1249 %arg_list = () ;
1250 $gotRETVAL = 0;
f06db76b 1251
8fc38fda 1252 INPUT_handler() ;
be3174d2 1253 process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE") ;
8fc38fda 1254
db3b9414 1255 print Q<<"EOF" if $ScopeThisXSUB;
1256# ENTER;
1257# [[
1258EOF
1259
a0d0e21e 1260 if (!$thisdone && defined($class)) {
683d4eee 1261 if (defined($static) or $func_name eq 'new') {
a0d0e21e 1262 print "\tchar *";
1263 $var_types{"CLASS"} = "char *";
1264 &generate_init("char *", 1, "CLASS");
1265 }
1266 else {
93a17b20 1267 print "\t$class *";
1268 $var_types{"THIS"} = "$class *";
1269 &generate_init("$class *", 1, "THIS");
a0d0e21e 1270 }
93a17b20 1271 }
1272
1273 # do code
1274 if (/^\s*NOT_IMPLEMENTED_YET/) {
cea2e8a9 1275 print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n";
4633a7c4 1276 $_ = '' ;
93a17b20 1277 } else {
1278 if ($ret_type ne "void") {
ddf6bed1 1279 print "\t" . &map_type($ret_type, 'RETVAL') . ";\n"
93a17b20 1280 if !$retvaldone;
1281 $args_match{"RETVAL"} = 0;
1282 $var_types{"RETVAL"} = $ret_type;
b26a54d0 1283 print "\tdXSTARG;\n"
1284 if $WantOptimize and $targetable{$type_kind{$ret_type}};
93a17b20 1285 }
db3b9414 1286
f8b8e0a4 1287 if (@arg_with_types) {
1288 unshift @line, @arg_with_types, $_;
1289 $_ = "";
1290 $processing_arg_with_types = 1;
1291 INPUT_handler() ;
1292 }
8e07c86e 1293 print $deferred;
db3b9414 1294
be3174d2 1295 process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS") ;
8e07c86e 1296
1297 if (check_keyword("PPCODE")) {
8fc38fda 1298 print_section();
8e07c86e 1299 death ("PPCODE must be last thing") if @line;
db3b9414 1300 print "\tLEAVE;\n" if $ScopeThisXSUB;
a0d0e21e 1301 print "\tPUTBACK;\n\treturn;\n";
8e07c86e 1302 } elsif (check_keyword("CODE")) {
8fc38fda 1303 print_section() ;
1304 } elsif (defined($class) and $func_name eq "DESTROY") {
a0d0e21e 1305 print "\n\t";
8e07c86e 1306 print "delete THIS;\n";
93a17b20 1307 } else {
1308 print "\n\t";
1309 if ($ret_type ne "void") {
463ee0b2 1310 print "RETVAL = ";
e50aee73 1311 $wantRETVAL = 1;
93a17b20 1312 }
1313 if (defined($static)) {
683d4eee 1314 if ($func_name eq 'new') {
8fc38fda 1315 $func_name = "$class";
8e07c86e 1316 } else {
1317 print "${class}::";
a0d0e21e 1318 }
93a17b20 1319 } elsif (defined($class)) {
683d4eee 1320 if ($func_name eq 'new') {
8fc38fda 1321 $func_name .= " $class";
1322 } else {
93a17b20 1323 print "THIS->";
8fc38fda 1324 }
93a17b20 1325 }
e50aee73 1326 $func_name =~ s/^($spat)//
1327 if defined($spat);
cfc02341 1328 $func_name = 'XSFUNCTION' if $interface;
93a17b20 1329 print "$func_name($func_args);\n";
93a17b20 1330 }
1331 }
1332
1333 # do output variables
f8b8e0a4 1334 $gotRETVAL = 0; # 1 if RETVAL seen in OUTPUT section;
1335 undef $RETVAL_code ; # code to set RETVAL (from OUTPUT section);
1336 # $wantRETVAL set if 'RETVAL =' autogenerated
1337 ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return;
8e07c86e 1338 undef %outargs ;
be3174d2 1339 process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE");
f06db76b 1340
0f568861 1341 &generate_output($var_types{$_}, $args_match{$_}, $_, $DoSetMagic)
1342 for grep $in_out{$_} =~ /OUT$/, keys %in_out;
1343
f06db76b 1344 # all OUTPUT done, so now push the return value on the stack
8e07c86e 1345 if ($gotRETVAL && $RETVAL_code) {
1346 print "\t$RETVAL_code\n";
1347 } elsif ($gotRETVAL || $wantRETVAL) {
b26a54d0 1348 my $t = $WantOptimize && $targetable{$type_kind{$ret_type}};
1349 my $var = 'RETVAL';
1350 my $type = $ret_type;
1351
1352 # 0: type, 1: with_size, 2: how, 3: how_size
1353 if ($t and not $t->[1] and $t->[0] eq 'p') {
1354 # PUSHp corresponds to setpvn. Treate setpv directly
1355 my $what = eval qq("$t->[2]");
1356 warn $@ if $@;
1357
1358 print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
f8b8e0a4 1359 $prepush_done = 1;
b26a54d0 1360 }
1361 elsif ($t) {
1362 my $what = eval qq("$t->[2]");
1363 warn $@ if $@;
1364
1365 my $size = $t->[3];
1366 $size = '' unless defined $size;
1367 $size = eval qq("$size");
1368 warn $@ if $@;
1369 print "\tXSprePUSH; PUSH$t->[0]($what$size);\n";
f8b8e0a4 1370 $prepush_done = 1;
b26a54d0 1371 }
1372 else {
1373 # RETVAL almost never needs SvSETMAGIC()
1374 &generate_output($ret_type, 0, 'RETVAL', 0);
1375 }
8e07c86e 1376 }
f06db76b 1377
f8b8e0a4 1378 $xsreturn = 1 if $ret_type ne "void";
1379 my $num = $xsreturn;
0f568861 1380 my $c = @outlist;
f8b8e0a4 1381 print "\tXSprePUSH;" if $c and not $prepush_done;
1382 print "\tEXTEND(SP,$c);\n" if $c;
1383 $xsreturn += $c;
0f568861 1384 generate_output($var_types{$_}, $num++, $_, 0, 1) for @outlist;
f8b8e0a4 1385
93a17b20 1386 # do cleanup
be3174d2 1387 process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE") ;
8e07c86e 1388
db3b9414 1389 print Q<<"EOF" if $ScopeThisXSUB;
1390# ]]
1391EOF
1392 print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE;
1393# LEAVE;
1394EOF
1395
93a17b20 1396 # print function trailer
8e07c86e 1397 print Q<<EOF;
2304df62 1398# ]]
8e07c86e 1399EOF
1400 print Q<<EOF if $except;
8990e307 1401# BEGHANDLERS
1402# CATCHALL
1403# sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
1404# ENDHANDLERS
93a17b20 1405EOF
8e07c86e 1406 if (check_keyword("CASE")) {
1407 blurt ("Error: No `CASE:' at top of function")
1408 unless $condnum;
1409 $_ = "CASE: $_"; # Restore CASE: label
1410 next;
8990e307 1411 }
8e07c86e 1412 last if $_ eq "$END:";
1413 death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function");
2304df62 1414 }
a0d0e21e 1415
2304df62 1416 print Q<<EOF if $except;
1417# if (errbuf[0])
cea2e8a9 1418# Perl_croak(aTHX_ errbuf);
2304df62 1419EOF
a0d0e21e 1420
f8b8e0a4 1421 if ($xsreturn) {
93d3b392 1422 print Q<<EOF unless $PPCODE;
f8b8e0a4 1423# XSRETURN($xsreturn);
a0d0e21e 1424EOF
93d3b392 1425 } else {
1426 print Q<<EOF unless $PPCODE;
1427# XSRETURN_EMPTY;
1428EOF
1429 }
a0d0e21e 1430
2304df62 1431 print Q<<EOF;
2304df62 1432#]]
8990e307 1433#
93a17b20 1434EOF
382b8d97 1435
4230ab3f 1436 my $newXS = "newXS" ;
1437 my $proto = "" ;
1438
382b8d97 1439 # Build the prototype string for the xsub
1440 if ($ProtoThisXSUB) {
4230ab3f 1441 $newXS = "newXSproto";
1442
6f1abe2b 1443 if ($ProtoThisXSUB eq 2) {
4230ab3f 1444 # User has specified empty prototype
1445 $proto = ', ""' ;
1446 }
6f1abe2b 1447 elsif ($ProtoThisXSUB ne 1) {
7d41bd0a 1448 # User has specified a prototype
4230ab3f 1449 $proto = ', "' . $ProtoThisXSUB . '"';
382b8d97 1450 }
1451 else {
1452 my $s = ';';
1453 if ($min_args < $num_args) {
1454 $s = '';
1455 $proto_arg[$min_args] .= ";" ;
1456 }
4230ab3f 1457 push @proto_arg, "$s\@"
382b8d97 1458 if $elipsis ;
1459
4230ab3f 1460 $proto = ', "' . join ("", @proto_arg) . '"';
382b8d97 1461 }
1462 }
1463
4230ab3f 1464 if (%XsubAliases) {
1465 $XsubAliases{$pname} = 0
1466 unless defined $XsubAliases{$pname} ;
1467 while ( ($name, $value) = each %XsubAliases) {
1468 push(@InitFileCode, Q<<"EOF");
1469# cv = newXS(\"$name\", XS_$Full_func_name, file);
1470# XSANY.any_i32 = $value ;
1471EOF
1472 push(@InitFileCode, Q<<"EOF") if $proto;
1473# sv_setpv((SV*)cv$proto) ;
1474EOF
1475 }
cfc02341 1476 }
be3174d2 1477 elsif (@Attributes) {
1478 push(@InitFileCode, Q<<"EOF");
1479# cv = newXS(\"$pname\", XS_$Full_func_name, file);
1480# apply_attrs_string("$Package", cv, "@Attributes", 0);
1481EOF
1482 }
cfc02341 1483 elsif ($interface) {
1484 while ( ($name, $value) = each %Interfaces) {
1485 $name = "$Package\::$name" unless $name =~ /::/;
1486 push(@InitFileCode, Q<<"EOF");
1487# cv = newXS(\"$name\", XS_$Full_func_name, file);
1488# $interface_macro_set(cv,$value) ;
1489EOF
1490 push(@InitFileCode, Q<<"EOF") if $proto;
1491# sv_setpv((SV*)cv$proto) ;
1492EOF
1493 }
4230ab3f 1494 }
1495 else {
1496 push(@InitFileCode,
1497 " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n");
1498 }
93a17b20 1499}
1500
1501# print initialization routine
7ee8c957 1502
e3b8966e 1503print Q<<"EOF";
e3b8966e 1504##ifdef __cplusplus
1505#extern "C"
1506##endif
7ee8c957 1507EOF
1508
8990e307 1509print Q<<"EOF";
05ceb97a 1510#XS(boot_$Module_cname); /* prototype to pass -Wmissing-prototypes */
a0d0e21e 1511#XS(boot_$Module_cname)
7ee8c957 1512EOF
1513
7ee8c957 1514print Q<<"EOF";
2304df62 1515#[[
a0d0e21e 1516# dXSARGS;
c6c619a9 1517EOF
1518
1519#-Wall: if there is no $Full_func_name there are no xsubs in this .xs
1520#so `file' is unused
1521print Q<<"EOF" if $Full_func_name;
8990e307 1522# char* file = __FILE__;
93a17b20 1523EOF
1524
c6c619a9 1525print Q "#\n";
1526
8fc38fda 1527print Q<<"EOF" if $WantVersionChk ;
1528# XS_VERSION_BOOTCHECK ;
1529#
1530EOF
1531
cfc02341 1532print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
8e07c86e 1533# {
1534# CV * cv ;
1535#
1536EOF
1537
4230ab3f 1538print @InitFileCode;
a0d0e21e 1539
cfc02341 1540print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
8e07c86e 1541# }
1542EOF
1543
a0d0e21e 1544if (@BootCode)
1545{
6f1abe2b 1546 print "\n /* Initialisation Section */\n\n" ;
1547 @line = @BootCode;
1548 print_section();
8e07c86e 1549 print "\n /* End of Initialisation Section */\n\n" ;
93a17b20 1550}
a0d0e21e 1551
e50aee73 1552print Q<<"EOF";;
3280af22 1553# XSRETURN_YES;
e50aee73 1554#]]
e3b8966e 1555#
1556EOF
1557
8fc38fda 1558warn("Please specify prototyping behavior for $filename (see perlxs manual)\n")
1559 unless $ProtoUsed ;
e50aee73 1560&Exit;
1561
93a17b20 1562sub output_init {
ddf6bed1 1563 local($type, $num, $var, $init, $name_printed) = @_;
a0d0e21e 1564 local($arg) = "ST(" . ($num - 1) . ")";
93a17b20 1565
7ad6fb0b 1566 if( $init =~ /^=/ ) {
ddf6bed1 1567 if ($name_printed) {
1568 eval qq/print " $init\\n"/;
1569 } else {
1570 eval qq/print "\\t$var $init\\n"/;
1571 }
7ad6fb0b 1572 warn $@ if $@;
1573 } else {
1574 if( $init =~ s/^\+// && $num ) {
ddf6bed1 1575 &generate_init($type, $num, $var, $name_printed);
1576 } elsif ($name_printed) {
1577 print ";\n";
1578 $init =~ s/^;//;
7ad6fb0b 1579 } else {
1580 eval qq/print "\\t$var;\\n"/;
1581 warn $@ if $@;
1582 $init =~ s/^;//;
1583 }
1584 $deferred .= eval qq/"\\n\\t$init\\n"/;
1585 warn $@ if $@;
1586 }
93a17b20 1587}
1588
c2960299 1589sub Warn
1590{
1591 # work out the line number
1592 my $line_no = $line_no[@line_no - @line -1] ;
1593
1594 print STDERR "@_ in $filename, line $line_no\n" ;
1595}
1596
1597sub blurt
1598{
1599 Warn @_ ;
1600 $errors ++
1601}
1602
1603sub death
1604{
1605 Warn @_ ;
1606 exit 1 ;
1607}
8990e307 1608
93a17b20 1609sub generate_init {
2304df62 1610 local($type, $num, $var) = @_;
a0d0e21e 1611 local($arg) = "ST(" . ($num - 1) . ")";
2304df62 1612 local($argoff) = $num - 1;
1613 local($ntype);
1614 local($tk);
93a17b20 1615
f06db76b 1616 $type = TidyType($type) ;
c2960299 1617 blurt("Error: '$type' not in typemap"), return
1618 unless defined($type_kind{$type});
1619
2304df62 1620 ($ntype = $type) =~ s/\s*\*/Ptr/g;
8e07c86e 1621 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
2304df62 1622 $tk = $type_kind{$type};
1623 $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
8e07c86e 1624 $type =~ tr/:/_/;
615ca1f4 1625 blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
c2960299 1626 unless defined $input_expr{$tk} ;
2304df62 1627 $expr = $input_expr{$tk};
1628 if ($expr =~ /DO_ARRAY_ELEM/) {
c2960299 1629 blurt("Error: '$subtype' not in typemap"), return
1630 unless defined($type_kind{$subtype});
615ca1f4 1631 blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
c2960299 1632 unless defined $input_expr{$type_kind{$subtype}} ;
2304df62 1633 $subexpr = $input_expr{$type_kind{$subtype}};
f8c11764 1634 $subexpr =~ s/\$type/\$subtype/g;
2304df62 1635 $subexpr =~ s/ntype/subtype/g;
1636 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1637 $subexpr =~ s/\n\t/\n\t\t/g;
93d3b392 1638 $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
a0d0e21e 1639 $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/;
2304df62 1640 $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
1641 }
db3b9414 1642 if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
1643 $ScopeThisXSUB = 1;
1644 }
2304df62 1645 if (defined($defaults{$var})) {
1646 $expr =~ s/(\t+)/$1 /g;
1647 $expr =~ s/ /\t/g;
ddf6bed1 1648 if ($name_printed) {
1649 print ";\n";
1650 } else {
1651 eval qq/print "\\t$var;\\n"/;
1652 warn $@ if $@;
1653 }
a104f515 1654 if ($defaults{$var} eq 'NO_INIT') {
4628e4f8 1655 $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
1656 } else {
1657 $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
1658 }
7ad6fb0b 1659 warn $@ if $@;
db3b9414 1660 } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) {
ddf6bed1 1661 if ($name_printed) {
1662 print ";\n";
1663 } else {
1664 eval qq/print "\\t$var;\\n"/;
1665 warn $@ if $@;
1666 }
2304df62 1667 $deferred .= eval qq/"\\n$expr;\\n"/;
7ad6fb0b 1668 warn $@ if $@;
2304df62 1669 } else {
ddf6bed1 1670 die "panic: do not know how to handle this branch for function pointers"
1671 if $name_printed;
2304df62 1672 eval qq/print "$expr;\\n"/;
7ad6fb0b 1673 warn $@ if $@;
2304df62 1674 }
93a17b20 1675}
1676
1677sub generate_output {
f8b8e0a4 1678 local($type, $num, $var, $do_setmagic, $do_push) = @_;
a0d0e21e 1679 local($arg) = "ST(" . ($num - ($num != 0)) . ")";
2304df62 1680 local($argoff) = $num - 1;
1681 local($ntype);
93a17b20 1682
f06db76b 1683 $type = TidyType($type) ;
2304df62 1684 if ($type =~ /^array\(([^,]*),(.*)\)/) {
f8c11764 1685 print "\t$arg = sv_newmortal();\n";
4bd3d083 1686 print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n";
ef50df4b 1687 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
2304df62 1688 } else {
f06db76b 1689 blurt("Error: '$type' not in typemap"), return
2304df62 1690 unless defined($type_kind{$type});
615ca1f4 1691 blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
c2960299 1692 unless defined $output_expr{$type_kind{$type}} ;
2304df62 1693 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1694 $ntype =~ s/\(\)//g;
8e07c86e 1695 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
2304df62 1696 $expr = $output_expr{$type_kind{$type}};
1697 if ($expr =~ /DO_ARRAY_ELEM/) {
c2960299 1698 blurt("Error: '$subtype' not in typemap"), return
1699 unless defined($type_kind{$subtype});
615ca1f4 1700 blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
c2960299 1701 unless defined $output_expr{$type_kind{$subtype}} ;
2304df62 1702 $subexpr = $output_expr{$type_kind{$subtype}};
1703 $subexpr =~ s/ntype/subtype/g;
1704 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1705 $subexpr =~ s/\$var/${var}[ix_$var]/g;
1706 $subexpr =~ s/\n\t/\n\t\t/g;
1707 $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
a0d0e21e 1708 eval "print qq\a$expr\a";
7ad6fb0b 1709 warn $@ if $@;
ef50df4b 1710 print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic;
2304df62 1711 }
a0d0e21e 1712 elsif ($var eq 'RETVAL') {
a2baab1c 1713 if ($expr =~ /^\t\$arg = new/) {
1714 # We expect that $arg has refcnt 1, so we need to
1715 # mortalize it.
a0d0e21e 1716 eval "print qq\a$expr\a";
7ad6fb0b 1717 warn $@ if $@;
f8b8e0a4 1718 print "\tsv_2mortal(ST($num));\n";
1719 print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic;
93a17b20 1720 }
a2baab1c 1721 elsif ($expr =~ /^\s*\$arg\s*=/) {
1722 # We expect that $arg has refcnt >=1, so we need
d689ffdd 1723 # to mortalize it!
a2baab1c 1724 eval "print qq\a$expr\a";
7ad6fb0b 1725 warn $@ if $@;
d689ffdd 1726 print "\tsv_2mortal(ST(0));\n";
ef50df4b 1727 print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic;
a2baab1c 1728 }
2304df62 1729 else {
a2baab1c 1730 # Just hope that the entry would safely write it
1731 # over an already mortalized value. By
1732 # coincidence, something like $arg = &sv_undef
1733 # works too.
8990e307 1734 print "\tST(0) = sv_newmortal();\n";
a0d0e21e 1735 eval "print qq\a$expr\a";
7ad6fb0b 1736 warn $@ if $@;
ef50df4b 1737 # new mortals don't have set magic
463ee0b2 1738 }
2304df62 1739 }
f8b8e0a4 1740 elsif ($do_push) {
1741 print "\tPUSHs(sv_newmortal());\n";
1742 $arg = "ST($num)";
1743 eval "print qq\a$expr\a";
1744 warn $@ if $@;
1745 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1746 }
a0d0e21e 1747 elsif ($arg =~ /^ST\(\d+\)$/) {
1748 eval "print qq\a$expr\a";
7ad6fb0b 1749 warn $@ if $@;
ef50df4b 1750 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
a0d0e21e 1751 }
2304df62 1752 }
93a17b20 1753}
1754
1755sub map_type {
ddf6bed1 1756 my($type, $varname) = @_;
93a17b20 1757
8e07c86e 1758 $type =~ tr/:/_/;
1759 $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s;
ddf6bed1 1760 if ($varname) {
1761 if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) {
1762 (substr $type, pos $type, 0) = " $varname ";
1763 } else {
1764 $type .= "\t$varname";
1765 }
1766 }
8e07c86e 1767 $type;
93a17b20 1768}
8990e307 1769
e50aee73 1770
1771sub Exit {
ff0cee69 1772# If this is VMS, the exit status has meaning to the shell, so we
1773# use a predictable value (SS$_Normal or SS$_Abort) rather than an
1774# arbitrary number.
1775# exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ;
1776 exit ($errors ? 1 : 0);
e50aee73 1777}