5 xsubpp - compiler to convert Perl XS code into C code
9 B<xsubpp> [B<-v>] [B<-C++>] [B<-csuffix csuffix>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-nooptimize>] [B<-typemap typemap>] ... file.xs
13 This compiler is typically run by the makefiles created by L<ExtUtils::MakeMaker>.
15 I<xsubpp> will compile XS code into C code by embedding the constructs
16 necessary to let C functions manipulate Perl values and creates the glue
17 necessary to let Perl access those functions. The compiler uses typemaps to
18 determine how to map C function parameters and variables to Perl values.
20 The compiler will search for typemap files called I<typemap>. It will use
21 the following search path to find default typemaps, with the rightmost
22 typemap taking precedence.
24 ../../../typemap:../../typemap:../typemap:typemap
28 Note that the C<XSOPT> MakeMaker option may be used to add these options to
29 any makefiles generated by MakeMaker.
35 Adds ``extern "C"'' to the C code.
37 =item B<-csuffix csuffix>
39 Set the suffix used for the generated C or C++ code. Defaults to '.c'
40 (even with B<-C++>), but some platforms might want to have e.g. '.cpp'.
41 Don't forget the '.' from the front.
45 Retains '::' in type names so that C++ hierachical types can be mapped.
49 Adds exception handling stubs to the C code.
51 =item B<-typemap typemap>
53 Indicates that a user-supplied typemap should take precedence over the
54 default typemaps. This option may be used multiple times, with the last
55 typemap having the highest precedence.
59 Prints the I<xsubpp> version number to standard output, then exits.
63 By default I<xsubpp> will not automatically generate prototype code for
64 all xsubs. This flag will enable prototypes.
66 =item B<-noversioncheck>
68 Disables the run time test that determines if the object file (derived
69 from the C<.xs> file) and the C<.pm> files have the same version
72 =item B<-nolinenumbers>
74 Prevents the inclusion of `#line' directives in the output.
78 Disables certain optimizations. The only optimization that is currently
79 affected is the use of I<target>s by the output C code (see L<perlguts>).
80 This may significantly slow down the generated code, but this is the way
81 B<xsubpp> of 5.005 and earlier operated.
85 Disable recognition of C<IN>, C<OUT_LIST> and C<INOUT_LIST> declarations.
89 Disable recognition of ANSI-like descriptions of function signature.
95 No environment variables are used.
101 =head1 MODIFICATION HISTORY
103 See the file F<changes.pod>.
107 perl(1), perlxs(1), perlxstut(1)
113 use vars qw($cplusplus $hiertype);
122 $XSUBPP_version = "1.9508";
124 my ($Is_VMS, $SymSet);
127 # Establish set of global symbols with max length 28, since xsubpp
128 # will later add the 'XS_' prefix.
129 require ExtUtils::XSSymSet;
130 $SymSet = new ExtUtils::XSSymSet 28;
135 $usage = "Usage: xsubpp [-v] [-C++] [-csuffix csuffix] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-nooptimize] [-noinout] [-noargtypes] [-s pattern] [-typemap typemap]... file.xs\n";
137 $proto_re = "[" . quotemeta('\$%&*@;[]') . "]" ;
140 $WantPrototypes = -1 ;
141 $WantVersionChk = 1 ;
143 $WantLineNumbers = 1 ;
146 $Fallback = 'PL_sv_undef';
148 my $process_inout = 1;
149 my $process_argtypes = 1;
152 SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) {
155 $spat = quotemeta shift, next SWITCH if $flag eq 's';
156 $cplusplus = 1, next SWITCH if $flag eq 'C++';
157 $csuffix = shift, next SWITCH if $flag eq 'csuffix';
158 $hiertype = 1, next SWITCH if $flag eq 'hiertype';
159 $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes';
160 $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes';
161 $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck';
162 $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck';
163 # XXX left this in for compat
164 next SWITCH if $flag eq 'object_capi';
165 $except = " TRY", next SWITCH if $flag eq 'except';
166 push(@tm,shift), next SWITCH if $flag eq 'typemap';
167 $WantLineNumbers = 0, next SWITCH if $flag eq 'nolinenumbers';
168 $WantLineNumbers = 1, next SWITCH if $flag eq 'linenumbers';
169 $WantOptimize = 0, next SWITCH if $flag eq 'nooptimize';
170 $WantOptimize = 1, next SWITCH if $flag eq 'optimize';
171 $process_inout = 0, next SWITCH if $flag eq 'noinout';
172 $process_inout = 1, next SWITCH if $flag eq 'inout';
173 $process_argtypes = 0, next SWITCH if $flag eq 'noargtypes';
174 $process_argtypes = 1, next SWITCH if $flag eq 'argtypes';
175 (print "xsubpp version $XSUBPP_version\n"), exit
179 if ($WantPrototypes == -1)
180 { $WantPrototypes = 0}
185 @ARGV == 1 or die $usage;
186 ($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)#
187 or ($dir, $filename) = $ARGV[0] =~ m#(.*)\\(.*)#
188 or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)#
189 or ($dir, $filename) = ('.', $ARGV[0]);
193 ++ $IncludedFiles{$ARGV[0]} ;
195 my(@XSStack) = ({type => 'none'}); # Stack of conditionals and INCLUDEs
196 my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA");
201 $_[0] =~ s/^\s+|\s+$//go ;
208 # rationalise any '*' by joining them into bunches and removing whitespace
212 # change multiple whitespace into a single space
215 # trim leading & trailing whitespace
221 $typemap = shift @ARGV;
222 foreach $typemap (@tm) {
223 die "Can't find $typemap in $pwd\n" unless -r $typemap;
225 unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap
226 ../../lib/ExtUtils/typemap ../../../typemap ../../typemap
228 foreach $typemap (@tm) {
229 next unless -f $typemap ;
230 # skip directories, binary files etc.
231 warn("Warning: ignoring non-text typemap file '$typemap'\n"), next
233 open(TYPEMAP, $typemap)
234 or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
240 my $line_no = $. + 1;
241 if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; }
242 if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; }
243 if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; }
244 if ($mode eq 'Typemap') {
248 # skip blank lines and comment lines
249 next if /^$/ or /^#/ ;
250 my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or
251 warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next;
252 $type = TidyType($type) ;
253 $type_kind{$type} = $kind ;
254 # prototype defaults to '$'
255 $proto = "\$" unless $proto ;
256 warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n")
257 unless ValidProtoString($proto) ;
258 $proto_letter{$type} = C_string($proto) ;
263 elsif ($mode eq 'Input') {
265 $input_expr{$_} = '';
266 $current = \$input_expr{$_};
270 $output_expr{$_} = '';
271 $current = \$output_expr{$_};
277 foreach $key (keys %input_expr) {
278 $input_expr{$key} =~ s/;*\s+\z//;
281 $bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced
282 $cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast
283 $size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn)
285 foreach $key (keys %output_expr) {
288 my ($t, $with_size, $arg, $sarg) =
289 ($output_expr{$key} =~
290 m[^ \s+ sv_set ( [iunp] ) v (n)? # Type, is_setpvn
291 \s* \( \s* $cast \$arg \s* ,
292 \s* ( (??{ $bal }) ) # Set from
293 ( (??{ $size }) )? # Possible sizeof set-from
296 $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
299 $END = "!End!\n\n"; # "impossible" keyword (multiple newline)
301 # Match an XS keyword
302 $BLOCK_re= '\s*(' . join('|', qw(
303 REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT
304 CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE
305 SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL OVERLOAD FALLBACK
308 # Input: ($_, @line) == unparsed input.
309 # Output: ($_, @line) == (rest of line, following lines).
310 # Return: the matched keyword if found, otherwise 0
312 $_ = shift(@line) while !/\S/ && @line;
313 s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2;
316 my ($C_group_rex, $C_arg);
317 # Group in C (no support for comments or literals)
318 $C_group_rex = qr/ [({\[]
319 (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
321 # Chunk in C without comma at toplevel (no comments):
322 $C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
323 | (??{ $C_group_rex })
324 | " (?: (?> [^\\"]+ )
326 )* " # String literal
327 | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
330 if ($WantLineNumbers) {
332 package xsubpp::counter;
334 my ($class, $cfile) = @_;
336 $SECTION_END_MARKER = "#line --- \"$cfile\"";
345 while ($$self =~ s/^([^\n]*\n)//) {
348 $line =~ s|^\#line\s+---(?=\s)|#line $line_no|;
357 $self->PRINT(sprintf($fmt, @_));
361 # Not necessary if we're careful to end with a "\n"
367 my $cfile = $filename;
368 $cfile =~ s/\.xs$/$csuffix/i or $cfile .= $csuffix;
369 tie(*PSEUDO_STDOUT, 'xsubpp::counter', $cfile);
370 select PSEUDO_STDOUT;
374 # the "do" is required for right semantics
375 do { $_ = shift(@line) } while !/\S/ && @line;
377 print("#line ", $line_no[@line_no - @line -1], " \"$filename\"\n")
378 if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/;
379 for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
382 print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
388 while (!/\S/ && @line) {
392 for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
399 sub process_keyword($)
404 &{"${kwd}_handler"}()
405 while $kwd = check_keyword($pattern) ;
409 blurt ("Error: `CASE:' after unconditional `CASE:'")
410 if $condnum && $cond eq '';
412 TrimWhitespace($cond);
413 print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n");
418 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
419 last if /^\s*NOT_IMPLEMENTED_YET/;
420 next unless /\S/; # skip blank lines
425 # remove trailing semicolon if no initialisation
426 s/\s*;$//g unless /[=;+].*\S/ ;
428 # Process the length(foo) declarations
429 if (s/^([^=]*)\blength\(\s*(\w+)\s*\)\s*$/$1 XSauto_length_of_$2=NO_INIT/x) {
430 print "\tSTRLEN\tSTRLEN_length_of_$2;\n";
431 $lengthof{$2} = $name;
432 # $islengthof{$name} = $1;
433 $deferred .= "\n\tXSauto_length_of_$2 = STRLEN_length_of_$2;";
436 # check for optional initialisation code
438 $var_init = $1 if s/\s*([=;+].*)$//s ;
439 $var_init =~ s/"/\\"/g;
442 my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s
443 or blurt("Error: invalid argument declaration '$line'"), next;
445 # Check for duplicate definitions
446 blurt ("Error: duplicate definition of argument '$var_name' ignored"), next
447 if $arg_list{$var_name}++
448 or defined $argtype_seen{$var_name} and not $processing_arg_with_types;
450 $thisdone |= $var_name eq "THIS";
451 $retvaldone |= $var_name eq "RETVAL";
452 $var_types{$var_name} = $var_type;
453 # XXXX This check is a safeguard against the unfinished conversion of
454 # generate_init(). When generate_init() is fixed,
455 # one can use 2-args map_type() unconditionally.
456 if ($var_type =~ / \( \s* \* \s* \) /x) {
457 # Function pointers are not yet supported with &output_init!
458 print "\t" . &map_type($var_type, $var_name);
461 print "\t" . &map_type($var_type);
464 $var_num = $args_match{$var_name};
466 $proto_arg[$var_num] = ProtoString($var_type)
468 $func_args =~ s/\b($var_name)\b/&$1/ if $var_addr;
469 if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/
470 or $in_out{$var_name} and $in_out{$var_name} =~ /^OUT/
471 and $var_init !~ /\S/) {
475 print "\t$var_name;\n";
477 } elsif ($var_init =~ /\S/) {
478 &output_init($var_type, $var_num, $var_name, $var_init, $name_printed);
480 # generate initialization code
481 &generate_init($var_type, $var_num, $var_name, $name_printed);
489 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
491 if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
492 $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0);
495 my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ;
496 blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next
497 if $outargs{$outarg} ++ ;
498 if (!$gotRETVAL and $outarg eq 'RETVAL') {
499 # deal with RETVAL last
500 $RETVAL_code = $outcode ;
504 blurt ("Error: OUTPUT $outarg not an argument"), next
505 unless defined($args_match{$outarg});
506 blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next
507 unless defined $var_types{$outarg} ;
508 $var_num = $args_match{$outarg};
510 print "\t$outcode\n";
511 print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic;
513 &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic);
515 delete $in_out{$outarg} # No need to auto-OUTPUT
516 if exists $in_out{$outarg} and $in_out{$outarg} =~ /OUT$/;
520 sub C_ARGS_handler() {
521 my $in = merge_section();
527 sub INTERFACE_MACRO_handler() {
528 my $in = merge_section();
531 if ($in =~ /\s/) { # two
532 ($interface_macro, $interface_macro_set) = split ' ', $in;
534 $interface_macro = $in;
535 $interface_macro_set = 'UNKNOWN_CVT'; # catch later
537 $interface = 1; # local
538 $Interfaces = 1; # global
541 sub INTERFACE_handler() {
542 my $in = merge_section();
546 foreach (split /[\s,]+/, $in) {
547 $Interfaces{$_} = $_;
550 # XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr);
552 $interface = 1; # local
553 $Interfaces = 1; # global
556 sub CLEANUP_handler() { print_section() }
557 sub PREINIT_handler() { print_section() }
558 sub POSTCALL_handler() { print_section() }
559 sub INIT_handler() { print_section() }
568 # Parse alias definitions
570 # alias = value alias = value ...
572 while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) {
574 $orig_alias = $alias ;
577 # check for optional package definition in the alias
578 $alias = $Packprefix . $alias if $alias !~ /::/ ;
580 # check for duplicate alias name & duplicate value
581 Warn("Warning: Ignoring duplicate alias '$orig_alias'")
582 if defined $XsubAliases{$alias} ;
584 Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values")
585 if $XsubAliasValues{$value} ;
588 $XsubAliases{$alias} = $value ;
589 $XsubAliasValues{$value} = $orig_alias ;
592 blurt("Error: Cannot parse ALIAS definitions from '$orig'")
598 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
601 push @Attributes, $_;
607 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
610 GetAliases($_) if $_ ;
614 sub OVERLOAD_handler()
616 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
619 while ( s/^\s*([\w:"\\)\+\-\*\/\%\<\>\.\&\|\^\!\~\{\}\=]+)\s*//) {
620 $Overload = 1 unless $Overload;
621 my $overload = "$Package\::(".$1 ;
623 " newXS(\"$overload\", XS_$Full_func_name, file$proto);\n");
629 sub FALLBACK_handler()
631 # the rest of the current line should contain either TRUE,
636 TRUE => "PL_sv_yes", 1 => "PL_sv_yes",
637 FALSE => "PL_sv_no", 0 => "PL_sv_no",
638 UNDEF => "PL_sv_undef",
641 # check for valid FALLBACK value
642 death ("Error: FALLBACK: TRUE/FALSE/UNDEF") unless exists $map{uc $_} ;
644 $Fallback = $map{uc $_} ;
647 sub REQUIRE_handler ()
649 # the rest of the current line should contain a version number
652 TrimWhitespace($Ver) ;
654 death ("Error: REQUIRE expects a version number")
657 # check that the version number is of the form n.n
658 death ("Error: REQUIRE: expected a number, got '$Ver'")
659 unless $Ver =~ /^\d+(\.\d*)?/ ;
661 death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.")
662 unless $XSUBPP_version >= $Ver ;
665 sub VERSIONCHECK_handler ()
667 # the rest of the current line should contain either ENABLE or
672 # check for ENABLE/DISABLE
673 death ("Error: VERSIONCHECK: ENABLE/DISABLE")
674 unless /^(ENABLE|DISABLE)/i ;
676 $WantVersionChk = 1 if $1 eq 'ENABLE' ;
677 $WantVersionChk = 0 if $1 eq 'DISABLE' ;
681 sub PROTOTYPE_handler ()
685 death("Error: Only 1 PROTOTYPE definition allowed per xsub")
686 if $proto_in_this_xsub ++ ;
688 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
692 if ($_ eq 'DISABLE') {
695 elsif ($_ eq 'ENABLE') {
699 # remove any whitespace
701 death("Error: Invalid prototype '$_'")
702 unless ValidProtoString($_) ;
703 $ProtoThisXSUB = C_string($_) ;
707 # If no prototype specified, then assume empty prototype ""
708 $ProtoThisXSUB = 2 unless $specified ;
716 death("Error: Only 1 SCOPE declaration allowed per xsub")
717 if $scope_in_this_xsub ++ ;
719 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
722 if ($_ =~ /^DISABLE/i) {
725 elsif ($_ =~ /^ENABLE/i) {
732 sub PROTOTYPES_handler ()
734 # the rest of the current line should contain either ENABLE or
739 # check for ENABLE/DISABLE
740 death ("Error: PROTOTYPES: ENABLE/DISABLE")
741 unless /^(ENABLE|DISABLE)/i ;
743 $WantPrototypes = 1 if $1 eq 'ENABLE' ;
744 $WantPrototypes = 0 if $1 eq 'DISABLE' ;
749 sub INCLUDE_handler ()
751 # the rest of the current line should contain a valid filename
755 death("INCLUDE: filename missing")
758 death("INCLUDE: output pipe is illegal")
761 # simple minded recursion detector
762 death("INCLUDE loop detected")
763 if $IncludedFiles{$_} ;
765 ++ $IncludedFiles{$_} unless /\|\s*$/ ;
767 # Save the current file context.
770 LastLine => $lastline,
771 LastLineNo => $lastline_no,
774 Filename => $filename,
781 open ($FH, "$_") or death("Cannot open '$_': $!") ;
785 #/* INCLUDE: Including '$_' from '$filename' */
791 # Prime the pump by reading the first
794 # skip leading blank lines
796 last unless /^\s*$/ ;
806 return 0 unless $XSStack[-1]{type} eq 'file' ;
808 my $data = pop @XSStack ;
809 my $ThisFile = $filename ;
810 my $isPipe = ($filename =~ /\|\s*$/) ;
812 -- $IncludedFiles{$filename}
817 $FH = $data->{Handle} ;
818 $filename = $data->{Filename} ;
819 $lastline = $data->{LastLine} ;
820 $lastline_no = $data->{LastLineNo} ;
821 @line = @{ $data->{Line} } ;
822 @line_no = @{ $data->{LineNo} } ;
824 if ($isPipe and $? ) {
826 print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ;
832 #/* INCLUDE: Returning to '$filename' from '$ThisFile' */
839 sub ValidProtoString ($)
843 if ( $string =~ /^$proto_re+$/ ) {
854 $string =~ s[\\][\\\\]g ;
862 $proto_letter{$type} or "\$" ;
866 my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line);
868 my ($cpp, $cpplevel);
870 if ($cpp =~ /^\#\s*if/) {
872 } elsif (!$cpplevel) {
873 Warn("Warning: #else/elif/endif without #if in this function");
874 print STDERR " (precede it with a blank line if the matching #if is outside the function)\n"
875 if $XSStack[-1]{type} eq 'if';
877 } elsif ($cpp =~ /^\#\s*endif/) {
881 Warn("Warning: #if without #endif in this function") if $cpplevel;
894 open($FH, $filename) or die "cannot open $filename: $!\n";
896 # Identify the version of xsubpp used
899 * This file was generated automatically by xsubpp version $XSUBPP_version from the
900 * contents of $filename. Do not edit this file, edit $filename instead.
902 * ANY CHANGES MADE HERE WILL BE LOST!
909 print("#line 1 \"$filename\"\n")
915 my $podstartline = $.;
918 # We can't just write out a /* */ comment, as our embedded
919 # POD might itself be in a comment. We can't put a /**/
920 # comment inside #if 0, as the C standard says that the source
921 # file is decomposed into preprocessing characters in the stage
922 # before preprocessing commands are executed.
923 # I don't want to leave the text as barewords, because the spec
924 # isn't clear whether macros are expanded before or after
925 # preprocessing commands are executed, and someone pathological
926 # may just have defined one of the 3 words as a macro that does
927 # something strange. Multiline strings are illegal in C, so
928 # the "" we write must be a string literal. And they aren't
929 # concatenated until 2 steps later, so we are safe.
930 print("#if 0\n \"Skipped embedded POD.\"\n#endif\n");
931 printf("#line %d \"$filename\"\n", $. + 1)
937 # At this point $. is at end of file so die won't state the start
938 # of the problem, and as we haven't yet read any lines &death won't
939 # show the correct line in the message either.
940 die ("Error: Unterminated pod in $filename, line $podstartline\n")
943 last if ($Module, $Package, $Prefix) =
944 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
948 &Exit unless defined $_;
950 print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
955 # Read next xsub into @line from ($lastline, <$FH>).
958 death ("Error: Unterminated `#if/#ifdef/#ifndef'")
959 if !defined $lastline && $XSStack[-1]{type} eq 'if';
962 return PopFile() if !defined $lastline;
965 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
967 $Package = defined($2) ? $2 : ''; # keep -w happy
968 $Prefix = defined($3) ? $3 : ''; # keep -w happy
969 $Prefix = quotemeta $Prefix ;
970 ($Module_cname = $Module) =~ s/\W/_/g;
971 ($Packid = $Package) =~ tr/:/_/;
972 $Packprefix = $Package;
973 $Packprefix .= "::" if $Packprefix ne "";
979 while ($lastline =~ /^=/) {
980 while ($lastline = <$FH>) {
981 last if ($lastline =~ /^=cut\s*$/);
983 death ("Error: Unterminated pod") unless $lastline;
986 $lastline =~ s/^\s+$//;
988 if ($lastline !~ /^\s*#/ ||
990 # ANSI: if ifdef ifndef elif else endif define undef
992 # gcc: warning include_next
994 # others: ident (gcc notes that some cpps have this one)
995 $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
996 last if $lastline =~ /^\S/ && @line && $line[-1] eq "";
997 push(@line, $lastline);
998 push(@line_no, $lastline_no) ;
1001 # Read next line and continuation lines
1002 last unless defined($lastline = <$FH>);
1005 $lastline .= $tmp_line
1006 while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>));
1009 $lastline =~ s/^\s+$//;
1011 pop(@line), pop(@line_no) while @line && $line[-1] eq "";
1016 while (fetch_para()) {
1017 # Print initial preprocessor statements and blank lines
1018 while (@line && $line[0] !~ /^[^\#]/) {
1019 my $line = shift(@line);
1021 next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
1023 if ($statement eq 'if') {
1024 $XSS_work_idx = @XSStack;
1025 push(@XSStack, {type => 'if'});
1027 death ("Error: `$statement' with no matching `if'")
1028 if $XSStack[-1]{type} ne 'if';
1029 if ($XSStack[-1]{varname}) {
1030 push(@InitFileCode, "#endif\n");
1031 push(@BootCode, "#endif");
1034 my(@fns) = keys %{$XSStack[-1]{functions}};
1035 if ($statement ne 'endif') {
1036 # Hide the functions defined in other #if branches, and reset.
1037 @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns;
1038 @{$XSStack[-1]}{qw(varname functions)} = ('', {});
1040 my($tmp) = pop(@XSStack);
1041 0 while (--$XSS_work_idx
1042 && $XSStack[$XSS_work_idx]{type} ne 'if');
1043 # Keep all new defined functions
1044 push(@fns, keys %{$tmp->{other_functions}});
1045 @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
1050 next PARAGRAPH unless @line;
1052 if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) {
1053 # We are inside an #if, but have not yet #defined its xsubpp variable.
1054 print "#define $cpp_next_tmp 1\n\n";
1055 push(@InitFileCode, "#if $cpp_next_tmp\n");
1056 push(@BootCode, "#if $cpp_next_tmp");
1057 $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++;
1060 death ("Code is not inside a function"
1061 ." (maybe last function was ended by a blank line "
1062 ." followed by a statement on column one?)")
1063 if $line[0] =~ /^\s/;
1065 # initialize info arrays
1073 undef($wantRETVAL) ;
1074 undef($RETVAL_no_return) ;
1077 undef(@fake_INPUT_pre) ; # For length(s) generated variables
1078 undef(@fake_INPUT) ;
1079 undef($processing_arg_with_types) ;
1080 undef(%argtype_seen) ;
1084 # undef(%islengthof) ;
1085 undef($proto_in_this_xsub) ;
1086 undef($scope_in_this_xsub) ;
1088 undef($prepush_done);
1089 $interface_macro = 'XSINTERFACE_FUNC' ;
1090 $interface_macro_set = 'XSINTERFACE_FUNC_SET' ;
1091 $ProtoThisXSUB = $WantPrototypes ;
1096 while ($kwd = check_keyword("REQUIRE|PROTOTYPES|FALLBACK|VERSIONCHECK|INCLUDE")) {
1097 &{"${kwd}_handler"}() ;
1098 next PARAGRAPH unless @line ;
1102 if (check_keyword("BOOT")) {
1104 push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"")
1105 if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/;
1106 push (@BootCode, @line, "") ;
1111 # extract return type, function name and arguments
1112 ($ret_type) = TidyType($_);
1113 $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//;
1115 # Allow one-line ANSI-like declaration
1117 if $process_argtypes
1118 and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s;
1120 # a function definition needs at least 2 lines
1121 blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH
1124 $externC = 1 if $ret_type =~ s/^extern "C"\s+//;
1125 $static = 1 if $ret_type =~ s/^static\s+//;
1127 $func_header = shift(@line);
1128 blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH
1129 unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
1131 ($class, $func_name, $orig_args) = ($1, $2, $3) ;
1132 $class = "$4 $class" if $4;
1133 ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/;
1134 ($clean_func_name = $func_name) =~ s/^$Prefix//;
1135 $Full_func_name = "${Packid}_$clean_func_name";
1136 if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); }
1138 # Check for duplicate function definition
1139 for $tmp (@XSStack) {
1140 next unless defined $tmp->{functions}{$Full_func_name};
1141 Warn("Warning: duplicate function definition '$clean_func_name' detected");
1144 $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ;
1145 %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = ();
1148 $orig_args =~ s/\\\s*/ /g; # process line continuations
1150 my %only_C_inlist; # Not in the signature of Perl function
1151 if ($process_argtypes and $orig_args =~ /\S/) {
1152 my $args = "$orig_args ,";
1153 if ($args =~ /^( (??{ $C_arg }) , )* $ /x) {
1154 @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg);
1158 my ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x;
1159 my ($pre, $name) = ($arg =~ /(.*?) \s*
1160 \b ( \w+ | length\( \s*\w+\s* \) )
1162 next unless length $pre;
1165 if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//) {
1167 $out_type = $type if $type ne 'IN';
1168 $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//;
1169 $pre =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//;
1172 if ($name =~ /^length\( \s* (\w+) \s* \)\z/x) {
1173 $name = "XSauto_length_of_$1";
1175 die "Default value on length() argument: `$_'"
1178 if (length $pre or $islength) { # Has a type
1180 push @fake_INPUT_pre, $arg;
1182 push @fake_INPUT, $arg;
1184 # warn "pushing '$arg'\n";
1185 $argtype_seen{$name}++;
1186 $_ = "$name$default"; # Assigns to @args
1188 $only_C_inlist{$_} = 1 if $out_type eq "OUTLIST" or $islength;
1189 push @outlist, $name if $out_type =~ /OUTLIST$/;
1190 $in_out{$name} = $out_type if $out_type;
1193 @args = split(/\s*,\s*/, $orig_args);
1194 Warn("Warning: cannot parse argument list '$orig_args', fallback to split");
1197 @args = split(/\s*,\s*/, $orig_args);
1199 if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\s+//) {
1201 next if $out_type eq 'IN';
1202 $only_C_inlist{$_} = 1 if $out_type eq "OUTLIST";
1203 push @outlist, $name if $out_type =~ /OUTLIST$/;
1204 $in_out{$_} = $out_type;
1208 if (defined($class)) {
1209 my $arg0 = ((defined($static) or $func_name eq 'new')
1210 ? "CLASS" : "THIS");
1211 unshift(@args, $arg0);
1212 ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/;
1217 my $report_args = '';
1218 foreach $i (0 .. $#args) {
1219 if ($args[$i] =~ s/\.\.\.//) {
1221 if ($args[$i] eq '' && $i == $#args) {
1222 $report_args .= ", ...";
1227 if ($only_C_inlist{$args[$i]}) {
1228 push @args_num, undef;
1230 push @args_num, ++$num_args;
1231 $report_args .= ", $args[$i]";
1233 if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) {
1236 $defaults{$args[$i]} = $2;
1237 $defaults{$args[$i]} =~ s/"/\\"/g;
1239 $proto_arg[$i+1] = "\$" ;
1241 $min_args = $num_args - $extra_args;
1242 $report_args =~ s/"/\\"/g;
1243 $report_args =~ s/^,\s+//;
1244 my @func_args = @args;
1245 shift @func_args if defined($class);
1248 s/^/&/ if $in_out{$_};
1250 $func_args = join(", ", @func_args);
1251 @args_match{@args} = @args_num;
1253 $PPCODE = grep(/^\s*PPCODE\s*:/, @line);
1254 $CODE = grep(/^\s*CODE\s*:/, @line);
1255 # Detect CODE: blocks which use ST(n)= or XST_m*(n,v)
1256 # to set explicit return values.
1257 $EXPLICIT_RETURN = ($CODE &&
1258 ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x ));
1259 $ALIAS = grep(/^\s*ALIAS\s*:/, @line);
1260 $INTERFACE = grep(/^\s*INTERFACE\s*:/, @line);
1262 $xsreturn = 1 if $EXPLICIT_RETURN;
1264 $externC = $externC ? qq[extern "C"] : "";
1266 # print function header
1269 #XS(XS_${Full_func_name}); /* prototype to pass -Wmissing-prototypes */
1270 #XS(XS_${Full_func_name})
1274 print Q<<"EOF" if $ALIAS ;
1277 print Q<<"EOF" if $INTERFACE ;
1278 # dXSFUNCTION($ret_type);
1281 $cond = ($min_args ? qq(items < $min_args) : 0);
1283 elsif ($min_args == $num_args) {
1284 $cond = qq(items != $min_args);
1287 $cond = qq(items < $min_args || items > $num_args);
1290 print Q<<"EOF" if $except;
1291 # char errbuf[1024];
1296 { print Q<<"EOF" if $cond }
1298 # Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv)));
1301 { print Q<<"EOF" if $cond }
1303 # Perl_croak(aTHX_ "Usage: $pname($report_args)");
1306 #gcc -Wall: if an xsub has no arguments and PPCODE is used
1307 #it is likely none of ST, XSRETURN or XSprePUSH macros are used
1308 #hence `ax' (setup by dXSARGS) is unused
1309 #XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS
1310 #but such a move could break third-party extensions
1311 print Q<<"EOF" if $PPCODE and $num_args == 0;
1312 # PERL_UNUSED_VAR(ax); /* -Wall */
1315 print Q<<"EOF" if $PPCODE;
1319 # Now do a block of some sort.
1322 $cond = ''; # last CASE: condidional
1323 push(@line, "$END:");
1324 push(@line_no, $line_no[-1]);
1328 &CASE_handler if check_keyword("CASE");
1333 # do initialization of input variables
1341 process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE|OVERLOAD") ;
1343 print Q<<"EOF" if $ScopeThisXSUB;
1348 if (!$thisdone && defined($class)) {
1349 if (defined($static) or $func_name eq 'new') {
1351 $var_types{"CLASS"} = "char *";
1352 &generate_init("char *", 1, "CLASS");
1356 $var_types{"THIS"} = "$class *";
1357 &generate_init("$class *", 1, "THIS");
1362 if (/^\s*NOT_IMPLEMENTED_YET/) {
1363 print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n";
1366 if ($ret_type ne "void") {
1367 print "\t" . &map_type($ret_type, 'RETVAL') . ";\n"
1369 $args_match{"RETVAL"} = 0;
1370 $var_types{"RETVAL"} = $ret_type;
1371 print "\tdXSTARG;\n"
1372 if $WantOptimize and $targetable{$type_kind{$ret_type}};
1375 if (@fake_INPUT or @fake_INPUT_pre) {
1376 unshift @line, @fake_INPUT_pre, @fake_INPUT, $_;
1378 $processing_arg_with_types = 1;
1383 process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS|OVERLOAD") ;
1385 if (check_keyword("PPCODE")) {
1387 death ("PPCODE must be last thing") if @line;
1388 print "\tLEAVE;\n" if $ScopeThisXSUB;
1389 print "\tPUTBACK;\n\treturn;\n";
1390 } elsif (check_keyword("CODE")) {
1392 } elsif (defined($class) and $func_name eq "DESTROY") {
1394 print "delete THIS;\n";
1397 if ($ret_type ne "void") {
1401 if (defined($static)) {
1402 if ($func_name eq 'new') {
1403 $func_name = "$class";
1407 } elsif (defined($class)) {
1408 if ($func_name eq 'new') {
1409 $func_name .= " $class";
1414 $func_name =~ s/^($spat)//
1416 $func_name = 'XSFUNCTION' if $interface;
1417 print "$func_name($func_args);\n";
1421 # do output variables
1422 $gotRETVAL = 0; # 1 if RETVAL seen in OUTPUT section;
1423 undef $RETVAL_code ; # code to set RETVAL (from OUTPUT section);
1424 # $wantRETVAL set if 'RETVAL =' autogenerated
1425 ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return;
1427 process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE|OVERLOAD");
1429 &generate_output($var_types{$_}, $args_match{$_}, $_, $DoSetMagic)
1430 for grep $in_out{$_} =~ /OUT$/, keys %in_out;
1432 # all OUTPUT done, so now push the return value on the stack
1433 if ($gotRETVAL && $RETVAL_code) {
1434 print "\t$RETVAL_code\n";
1435 } elsif ($gotRETVAL || $wantRETVAL) {
1436 my $t = $WantOptimize && $targetable{$type_kind{$ret_type}};
1438 my $type = $ret_type;
1440 # 0: type, 1: with_size, 2: how, 3: how_size
1441 if ($t and not $t->[1] and $t->[0] eq 'p') {
1442 # PUSHp corresponds to setpvn. Treate setpv directly
1443 my $what = eval qq("$t->[2]");
1446 print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
1450 my $what = eval qq("$t->[2]");
1454 $size = '' unless defined $size;
1455 $size = eval qq("$size");
1457 print "\tXSprePUSH; PUSH$t->[0]($what$size);\n";
1461 # RETVAL almost never needs SvSETMAGIC()
1462 &generate_output($ret_type, 0, 'RETVAL', 0);
1466 $xsreturn = 1 if $ret_type ne "void";
1467 my $num = $xsreturn;
1469 # (PP)CODE set different values of SP; reset to PPCODE's with 0 output
1470 print "\tXSprePUSH;" if $c and not $prepush_done;
1471 # Take into account stuff already put on stack
1472 print "\t++SP;" if $c and not $prepush_done and $xsreturn;
1473 # Now SP corresponds to ST($xsreturn), so one can combine PUSH and ST()
1474 print "\tEXTEND(SP,$c);\n" if $c;
1476 generate_output($var_types{$_}, $num++, $_, 0, 1) for @outlist;
1479 process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE|OVERLOAD") ;
1481 print Q<<"EOF" if $ScopeThisXSUB;
1484 print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE;
1488 # print function trailer
1492 print Q<<EOF if $except;
1495 # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
1498 if (check_keyword("CASE")) {
1499 blurt ("Error: No `CASE:' at top of function")
1501 $_ = "CASE: $_"; # Restore CASE: label
1504 last if $_ eq "$END:";
1505 death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function");
1508 print Q<<EOF if $except;
1510 # Perl_croak(aTHX_ errbuf);
1514 print Q<<EOF unless $PPCODE;
1515 # XSRETURN($xsreturn);
1518 print Q<<EOF unless $PPCODE;
1528 my $newXS = "newXS" ;
1531 # Build the prototype string for the xsub
1532 if ($ProtoThisXSUB) {
1533 $newXS = "newXSproto";
1535 if ($ProtoThisXSUB eq 2) {
1536 # User has specified empty prototype
1539 elsif ($ProtoThisXSUB ne 1) {
1540 # User has specified a prototype
1541 $proto = ', "' . $ProtoThisXSUB . '"';
1545 if ($min_args < $num_args) {
1547 $proto_arg[$min_args] .= ";" ;
1549 push @proto_arg, "$s\@"
1552 $proto = ', "' . join ("", @proto_arg) . '"';
1557 $XsubAliases{$pname} = 0
1558 unless defined $XsubAliases{$pname} ;
1559 while ( ($name, $value) = each %XsubAliases) {
1560 push(@InitFileCode, Q<<"EOF");
1561 # cv = newXS(\"$name\", XS_$Full_func_name, file);
1562 # XSANY.any_i32 = $value ;
1564 push(@InitFileCode, Q<<"EOF") if $proto;
1565 # sv_setpv((SV*)cv$proto) ;
1569 elsif (@Attributes) {
1570 push(@InitFileCode, Q<<"EOF");
1571 # cv = newXS(\"$pname\", XS_$Full_func_name, file);
1572 # apply_attrs_string("$Package", cv, "@Attributes", 0);
1575 elsif ($interface) {
1576 while ( ($name, $value) = each %Interfaces) {
1577 $name = "$Package\::$name" unless $name =~ /::/;
1578 push(@InitFileCode, Q<<"EOF");
1579 # cv = newXS(\"$name\", XS_$Full_func_name, file);
1580 # $interface_macro_set(cv,$value) ;
1582 push(@InitFileCode, Q<<"EOF") if $proto;
1583 # sv_setpv((SV*)cv$proto) ;
1589 " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n");
1593 if ($Overload) # make it findable with fetchmethod
1597 #XS(XS_${Packid}_nil); /* prototype to pass -Wmissing-prototypes */
1598 #XS(XS_${Packid}_nil)
1604 unshift(@InitFileCode, <<"MAKE_FETCHMETHOD_WORK");
1605 /* Making a sub named "${Package}::()" allows the package */
1606 /* to be findable via fetchmethod(), and causes */
1607 /* overload::Overloaded("${Package}") to return true. */
1608 newXS("${Package}::()", XS_${Packid}_nil, file$proto);
1609 MAKE_FETCHMETHOD_WORK
1612 # print initialization routine
1621 #XS(boot_$Module_cname); /* prototype to pass -Wmissing-prototypes */
1622 #XS(boot_$Module_cname)
1630 #-Wall: if there is no $Full_func_name there are no xsubs in this .xs
1631 #so `file' is unused
1632 print Q<<"EOF" if $Full_func_name;
1633 # char* file = __FILE__;
1638 print Q<<"EOF" if $WantVersionChk ;
1639 # XS_VERSION_BOOTCHECK ;
1643 print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
1649 print Q<<"EOF" if ($Overload);
1650 # /* register the overloading (type 'A') magic */
1651 # PL_amagic_generation++;
1652 # /* The magic for overload gets a GV* via gv_fetchmeth as */
1653 # /* mentioned above, and looks in the SV* slot of it for */
1654 # /* the "fallback" status. */
1656 # get_sv( "${Package}::()", TRUE ),
1661 print @InitFileCode;
1663 print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
1669 print "\n /* Initialisation Section */\n\n" ;
1672 print "\n /* End of Initialisation Section */\n\n" ;
1681 warn("Please specify prototyping behavior for $filename (see perlxs manual)\n")
1686 local($type, $num, $var, $init, $name_printed) = @_;
1687 local($arg) = "ST(" . ($num - 1) . ")";
1689 if( $init =~ /^=/ ) {
1690 if ($name_printed) {
1691 eval qq/print " $init\\n"/;
1693 eval qq/print "\\t$var $init\\n"/;
1697 if( $init =~ s/^\+// && $num ) {
1698 &generate_init($type, $num, $var, $name_printed);
1699 } elsif ($name_printed) {
1703 eval qq/print "\\t$var;\\n"/;
1707 $deferred .= eval qq/"\\n\\t$init\\n"/;
1714 # work out the line number
1715 my $line_no = $line_no[@line_no - @line -1] ;
1717 print STDERR "@_ in $filename, line $line_no\n" ;
1733 local($type, $num, $var) = @_;
1734 local($arg) = "ST(" . ($num - 1) . ")";
1735 local($argoff) = $num - 1;
1739 $type = TidyType($type) ;
1740 blurt("Error: '$type' not in typemap"), return
1741 unless defined($type_kind{$type});
1743 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1744 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1745 $tk = $type_kind{$type};
1746 $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
1747 if ($tk eq 'T_PV' and exists $lengthof{$var}) {
1748 print "\t$var" unless $name_printed;
1749 print " = ($type)SvPV($arg, STRLEN_length_of_$var);\n";
1750 die "default value not supported with length(NAME) supplied"
1751 if defined $defaults{$var};
1754 $type =~ tr/:/_/ unless $hiertype;
1755 blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
1756 unless defined $input_expr{$tk} ;
1757 $expr = $input_expr{$tk};
1758 if ($expr =~ /DO_ARRAY_ELEM/) {
1759 blurt("Error: '$subtype' not in typemap"), return
1760 unless defined($type_kind{$subtype});
1761 blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
1762 unless defined $input_expr{$type_kind{$subtype}} ;
1763 $subexpr = $input_expr{$type_kind{$subtype}};
1764 $subexpr =~ s/\$type/\$subtype/g;
1765 $subexpr =~ s/ntype/subtype/g;
1766 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1767 $subexpr =~ s/\n\t/\n\t\t/g;
1768 $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
1769 $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/;
1770 $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
1772 if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
1775 if (defined($defaults{$var})) {
1776 $expr =~ s/(\t+)/$1 /g;
1778 if ($name_printed) {
1781 eval qq/print "\\t$var;\\n"/;
1784 if ($defaults{$var} eq 'NO_INIT') {
1785 $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
1787 $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
1790 } elsif ($ScopeThisXSUB or $expr !~ /^\s*\$var =/) {
1791 if ($name_printed) {
1794 eval qq/print "\\t$var;\\n"/;
1797 $deferred .= eval qq/"\\n$expr;\\n"/;
1800 die "panic: do not know how to handle this branch for function pointers"
1802 eval qq/print "$expr;\\n"/;
1807 sub generate_output {
1808 local($type, $num, $var, $do_setmagic, $do_push) = @_;
1809 local($arg) = "ST(" . ($num - ($num != 0)) . ")";
1810 local($argoff) = $num - 1;
1813 $type = TidyType($type) ;
1814 if ($type =~ /^array\(([^,]*),(.*)\)/) {
1815 print "\t$arg = sv_newmortal();\n";
1816 print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n";
1817 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1819 blurt("Error: '$type' not in typemap"), return
1820 unless defined($type_kind{$type});
1821 blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
1822 unless defined $output_expr{$type_kind{$type}} ;
1823 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1824 $ntype =~ s/\(\)//g;
1825 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1826 $expr = $output_expr{$type_kind{$type}};
1827 if ($expr =~ /DO_ARRAY_ELEM/) {
1828 blurt("Error: '$subtype' not in typemap"), return
1829 unless defined($type_kind{$subtype});
1830 blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
1831 unless defined $output_expr{$type_kind{$subtype}} ;
1832 $subexpr = $output_expr{$type_kind{$subtype}};
1833 $subexpr =~ s/ntype/subtype/g;
1834 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1835 $subexpr =~ s/\$var/${var}[ix_$var]/g;
1836 $subexpr =~ s/\n\t/\n\t\t/g;
1837 $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
1838 eval "print qq\a$expr\a";
1840 print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic;
1842 elsif ($var eq 'RETVAL') {
1843 if ($expr =~ /^\t\$arg = new/) {
1844 # We expect that $arg has refcnt 1, so we need to
1846 eval "print qq\a$expr\a";
1848 print "\tsv_2mortal(ST($num));\n";
1849 print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic;
1851 elsif ($expr =~ /^\s*\$arg\s*=/) {
1852 # We expect that $arg has refcnt >=1, so we need
1854 eval "print qq\a$expr\a";
1856 print "\tsv_2mortal(ST(0));\n";
1857 print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic;
1860 # Just hope that the entry would safely write it
1861 # over an already mortalized value. By
1862 # coincidence, something like $arg = &sv_undef
1864 print "\tST(0) = sv_newmortal();\n";
1865 eval "print qq\a$expr\a";
1867 # new mortals don't have set magic
1871 print "\tPUSHs(sv_newmortal());\n";
1873 eval "print qq\a$expr\a";
1875 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1877 elsif ($arg =~ /^ST\(\d+\)$/) {
1878 eval "print qq\a$expr\a";
1880 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1886 my($type, $varname) = @_;
1888 # C++ has :: in types too so skip this
1889 $type =~ tr/:/_/ unless $hiertype;
1890 $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s;
1892 if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) {
1893 (substr $type, pos $type, 0) = " $varname ";
1895 $type .= "\t$varname";
1903 # If this is VMS, the exit status has meaning to the shell, so we
1904 # use a predictable value (SS$_Normal or SS$_Abort) rather than an
1906 # exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ;
1907 exit ($errors ? 1 : 0);