1N/A#!./miniperl
1N/A
1N/A=head1 NAME
1N/A
1N/Axsubpp - compiler to convert Perl XS code into C code
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/AB<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-nooptimize>] [B<-typemap typemap>] ... file.xs
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThis compiler is typically run by the makefiles created by L<ExtUtils::MakeMaker>.
1N/A
1N/AI<xsubpp> will compile XS code into C code by embedding the constructs
1N/Anecessary to let C functions manipulate Perl values and creates the glue
1N/Anecessary to let Perl access those functions. The compiler uses typemaps to
1N/Adetermine how to map C function parameters and variables to Perl values.
1N/A
1N/AThe compiler will search for typemap files called I<typemap>. It will use
1N/Athe following search path to find default typemaps, with the rightmost
1N/Atypemap taking precedence.
1N/A
1N/A ../../../typemap:../../typemap:../typemap:typemap
1N/A
1N/A=head1 OPTIONS
1N/A
1N/ANote that the C<XSOPT> MakeMaker option may be used to add these options to
1N/Aany makefiles generated by MakeMaker.
1N/A
1N/A=over 5
1N/A
1N/A=item B<-C++>
1N/A
1N/AAdds ``extern "C"'' to the C code.
1N/A
1N/A=item B<-hiertype>
1N/A
1N/ARetains '::' in type names so that C++ hierachical types can be mapped.
1N/A
1N/A=item B<-except>
1N/A
1N/AAdds exception handling stubs to the C code.
1N/A
1N/A=item B<-typemap typemap>
1N/A
1N/AIndicates that a user-supplied typemap should take precedence over the
1N/Adefault typemaps. This option may be used multiple times, with the last
1N/Atypemap having the highest precedence.
1N/A
1N/A=item B<-v>
1N/A
1N/APrints the I<xsubpp> version number to standard output, then exits.
1N/A
1N/A=item B<-prototypes>
1N/A
1N/ABy default I<xsubpp> will not automatically generate prototype code for
1N/Aall xsubs. This flag will enable prototypes.
1N/A
1N/A=item B<-noversioncheck>
1N/A
1N/ADisables the run time test that determines if the object file (derived
1N/Afrom the C<.xs> file) and the C<.pm> files have the same version
1N/Anumber.
1N/A
1N/A=item B<-nolinenumbers>
1N/A
1N/APrevents the inclusion of `#line' directives in the output.
1N/A
1N/A=item B<-nooptimize>
1N/A
1N/ADisables certain optimizations. The only optimization that is currently
1N/Aaffected is the use of I<target>s by the output C code (see L<perlguts>).
1N/AThis may significantly slow down the generated code, but this is the way
1N/AB<xsubpp> of 5.005 and earlier operated.
1N/A
1N/A=item B<-noinout>
1N/A
1N/ADisable recognition of C<IN>, C<OUT_LIST> and C<INOUT_LIST> declarations.
1N/A
1N/A=item B<-noargtypes>
1N/A
1N/ADisable recognition of ANSI-like descriptions of function signature.
1N/A
1N/A=back
1N/A
1N/A=head1 ENVIRONMENT
1N/A
1N/ANo environment variables are used.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ALarry Wall
1N/A
1N/A=head1 MODIFICATION HISTORY
1N/A
1N/ASee the file F<changes.pod>.
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/Aperl(1), perlxs(1), perlxstut(1)
1N/A
1N/A=cut
1N/A
1N/Arequire 5.002;
1N/Ause Cwd;
1N/Ause vars qw($cplusplus $hiertype);
1N/Ause vars '%v';
1N/A
1N/Ause Config;
1N/A
1N/Asub Q ;
1N/A
1N/A# Global Constants
1N/A
1N/A$XSUBPP_version = "1.9508";
1N/A
1N/Amy ($Is_VMS, $SymSet);
1N/Aif ($^O eq 'VMS') {
1N/A $Is_VMS = 1;
1N/A # Establish set of global symbols with max length 28, since xsubpp
1N/A # will later add the 'XS_' prefix.
1N/A require ExtUtils::XSSymSet;
1N/A $SymSet = new ExtUtils::XSSymSet 28;
1N/A}
1N/A
1N/A$FH = 'File0000' ;
1N/A
1N/A$usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-nooptimize] [-noinout] [-noargtypes] [-s pattern] [-typemap typemap]... file.xs\n";
1N/A
1N/A$proto_re = "[" . quotemeta('\$%&*@;[]') . "]" ;
1N/A
1N/A$except = "";
1N/A$WantPrototypes = -1 ;
1N/A$WantVersionChk = 1 ;
1N/A$ProtoUsed = 0 ;
1N/A$WantLineNumbers = 1 ;
1N/A$WantOptimize = 1 ;
1N/A$Overload = 0;
1N/A$Fallback = 'PL_sv_undef';
1N/A
1N/Amy $process_inout = 1;
1N/Amy $process_argtypes = 1;
1N/A
1N/ASWITCH: while (@ARGV and $ARGV[0] =~ /^-./) {
1N/A $flag = shift @ARGV;
1N/A $flag =~ s/^-// ;
1N/A $spat = quotemeta shift, next SWITCH if $flag eq 's';
1N/A $cplusplus = 1, next SWITCH if $flag eq 'C++';
1N/A $hiertype = 1, next SWITCH if $flag eq 'hiertype';
1N/A $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes';
1N/A $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes';
1N/A $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck';
1N/A $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck';
1N/A # XXX left this in for compat
1N/A next SWITCH if $flag eq 'object_capi';
1N/A $except = " TRY", next SWITCH if $flag eq 'except';
1N/A push(@tm,shift), next SWITCH if $flag eq 'typemap';
1N/A $WantLineNumbers = 0, next SWITCH if $flag eq 'nolinenumbers';
1N/A $WantLineNumbers = 1, next SWITCH if $flag eq 'linenumbers';
1N/A $WantOptimize = 0, next SWITCH if $flag eq 'nooptimize';
1N/A $WantOptimize = 1, next SWITCH if $flag eq 'optimize';
1N/A $process_inout = 0, next SWITCH if $flag eq 'noinout';
1N/A $process_inout = 1, next SWITCH if $flag eq 'inout';
1N/A $process_argtypes = 0, next SWITCH if $flag eq 'noargtypes';
1N/A $process_argtypes = 1, next SWITCH if $flag eq 'argtypes';
1N/A (print "xsubpp version $XSUBPP_version\n"), exit
1N/A if $flag eq 'v';
1N/A die $usage;
1N/A}
1N/Aif ($WantPrototypes == -1)
1N/A { $WantPrototypes = 0}
1N/Aelse
1N/A { $ProtoUsed = 1 }
1N/A
1N/A
1N/A@ARGV == 1 or die $usage;
1N/A($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)#
1N/A or ($dir, $filename) = $ARGV[0] =~ m#(.*)\\(.*)#
1N/A or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)#
1N/A or ($dir, $filename) = ('.', $ARGV[0]);
1N/Achdir($dir);
1N/A$pwd = cwd();
1N/A
1N/A++ $IncludedFiles{$ARGV[0]} ;
1N/A
1N/Amy(@XSStack) = ({type => 'none'}); # Stack of conditionals and INCLUDEs
1N/Amy($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA");
1N/A
1N/A
1N/Asub TrimWhitespace
1N/A{
1N/A $_[0] =~ s/^\s+|\s+$//go ;
1N/A}
1N/A
1N/Asub TidyType
1N/A{
1N/A local ($_) = @_ ;
1N/A
1N/A # rationalise any '*' by joining them into bunches and removing whitespace
1N/A s#\s*(\*+)\s*#$1#g;
1N/A s#(\*+)# $1 #g ;
1N/A
1N/A # change multiple whitespace into a single space
1N/A s/\s+/ /g ;
1N/A
1N/A # trim leading & trailing whitespace
1N/A TrimWhitespace($_) ;
1N/A
1N/A $_ ;
1N/A}
1N/A
1N/A$typemap = shift @ARGV;
1N/Aforeach $typemap (@tm) {
1N/A die "Can't find $typemap in $pwd\n" unless -r $typemap;
1N/A}
1N/Aunshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap
1N/A ../../lib/ExtUtils/typemap ../../../typemap ../../typemap
1N/A ../typemap typemap);
1N/Aforeach $typemap (@tm) {
1N/A next unless -f $typemap ;
1N/A # skip directories, binary files etc.
1N/A warn("Warning: ignoring non-text typemap file '$typemap'\n"), next
1N/A unless -T $typemap ;
1N/A open(TYPEMAP, $typemap)
1N/A or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
1N/A $mode = 'Typemap';
1N/A $junk = "" ;
1N/A $current = \$junk;
1N/A while (<TYPEMAP>) {
1N/A next if /^\s*#/;
1N/A my $line_no = $. + 1;
1N/A if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; }
1N/A if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; }
1N/A if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; }
1N/A if ($mode eq 'Typemap') {
1N/A chomp;
1N/A my $line = $_ ;
1N/A TrimWhitespace($_) ;
1N/A # skip blank lines and comment lines
1N/A next if /^$/ or /^#/ ;
1N/A my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or
1N/A warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next;
1N/A $type = TidyType($type) ;
1N/A $type_kind{$type} = $kind ;
1N/A # prototype defaults to '$'
1N/A $proto = "\$" unless $proto ;
1N/A warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n")
1N/A unless ValidProtoString($proto) ;
1N/A $proto_letter{$type} = C_string($proto) ;
1N/A }
1N/A elsif (/^\s/) {
1N/A $$current .= $_;
1N/A }
1N/A elsif ($mode eq 'Input') {
1N/A s/\s+$//;
1N/A $input_expr{$_} = '';
1N/A $current = \$input_expr{$_};
1N/A }
1N/A else {
1N/A s/\s+$//;
1N/A $output_expr{$_} = '';
1N/A $current = \$output_expr{$_};
1N/A }
1N/A }
1N/A close(TYPEMAP);
1N/A}
1N/A
1N/Aforeach $key (keys %input_expr) {
1N/A $input_expr{$key} =~ s/;*\s+\z//;
1N/A}
1N/A
1N/A$bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced
1N/A$cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast
1N/A$size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn)
1N/A
1N/Aforeach $key (keys %output_expr) {
1N/A use re 'eval';
1N/A
1N/A my ($t, $with_size, $arg, $sarg) =
1N/A ($output_expr{$key} =~
1N/A m[^ \s+ sv_set ( [iunp] ) v (n)? # Type, is_setpvn
1N/A \s* \( \s* $cast \$arg \s* ,
1N/A \s* ( (??{ $bal }) ) # Set from
1N/A ( (??{ $size }) )? # Possible sizeof set-from
1N/A \) \s* ; \s* $
1N/A ]x);
1N/A $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
1N/A}
1N/A
1N/A$END = "!End!\n\n"; # "impossible" keyword (multiple newline)
1N/A
1N/A# Match an XS keyword
1N/A$BLOCK_re= '\s*(' . join('|', qw(
1N/A REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT
1N/A CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE
1N/A SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL OVERLOAD FALLBACK
1N/A )) . "|$END)\\s*:";
1N/A
1N/A# Input: ($_, @line) == unparsed input.
1N/A# Output: ($_, @line) == (rest of line, following lines).
1N/A# Return: the matched keyword if found, otherwise 0
1N/Asub check_keyword {
1N/A $_ = shift(@line) while !/\S/ && @line;
1N/A s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2;
1N/A}
1N/A
1N/Amy ($C_group_rex, $C_arg);
1N/A# Group in C (no support for comments or literals)
1N/A$C_group_rex = qr/ [({\[]
1N/A (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
1N/A [)}\]] /x ;
1N/A# Chunk in C without comma at toplevel (no comments):
1N/A$C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
1N/A | (??{ $C_group_rex })
1N/A | " (?: (?> [^\\"]+ )
1N/A | \\.
1N/A )* " # String literal
1N/A | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
1N/A )* /xs;
1N/A
1N/Aif ($WantLineNumbers) {
1N/A {
1N/A package xsubpp::counter;
1N/A sub TIEHANDLE {
1N/A my ($class, $cfile) = @_;
1N/A my $buf = "";
1N/A $SECTION_END_MARKER = "#line --- \"$cfile\"";
1N/A $line_no = 1;
1N/A bless \$buf;
1N/A }
1N/A
1N/A sub PRINT {
1N/A my $self = shift;
1N/A for (@_) {
1N/A $$self .= $_;
1N/A while ($$self =~ s/^([^\n]*\n)//) {
1N/A my $line = $1;
1N/A ++ $line_no;
1N/A $line =~ s|^\#line\s+---(?=\s)|#line $line_no|;
1N/A print STDOUT $line;
1N/A }
1N/A }
1N/A }
1N/A
1N/A sub PRINTF {
1N/A my $self = shift;
1N/A my $fmt = shift;
1N/A $self->PRINT(sprintf($fmt, @_));
1N/A }
1N/A
1N/A sub DESTROY {
1N/A # Not necessary if we're careful to end with a "\n"
1N/A my $self = shift;
1N/A print STDOUT $$self;
1N/A }
1N/A }
1N/A
1N/A my $cfile = $filename;
1N/A $cfile =~ s/\.xs$/.c/i or $cfile .= ".c";
1N/A tie(*PSEUDO_STDOUT, 'xsubpp::counter', $cfile);
1N/A select PSEUDO_STDOUT;
1N/A}
1N/A
1N/Asub print_section {
1N/A # the "do" is required for right semantics
1N/A do { $_ = shift(@line) } while !/\S/ && @line;
1N/A
1N/A print("#line ", $line_no[@line_no - @line -1], " \"$filename\"\n")
1N/A if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/;
1N/A for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A print "$_\n";
1N/A }
1N/A print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
1N/A}
1N/A
1N/Asub merge_section {
1N/A my $in = '';
1N/A
1N/A while (!/\S/ && @line) {
1N/A $_ = shift(@line);
1N/A }
1N/A
1N/A for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A $in .= "$_\n";
1N/A }
1N/A chomp $in;
1N/A return $in;
1N/A}
1N/A
1N/Asub process_keyword($)
1N/A{
1N/A my($pattern) = @_ ;
1N/A my $kwd ;
1N/A
1N/A &{"${kwd}_handler"}()
1N/A while $kwd = check_keyword($pattern) ;
1N/A}
1N/A
1N/Asub CASE_handler {
1N/A blurt ("Error: `CASE:' after unconditional `CASE:'")
1N/A if $condnum && $cond eq '';
1N/A $cond = $_;
1N/A TrimWhitespace($cond);
1N/A print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n");
1N/A $_ = '' ;
1N/A}
1N/A
1N/Asub INPUT_handler {
1N/A for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A last if /^\s*NOT_IMPLEMENTED_YET/;
1N/A next unless /\S/; # skip blank lines
1N/A
1N/A TrimWhitespace($_) ;
1N/A my $line = $_ ;
1N/A
1N/A # remove trailing semicolon if no initialisation
1N/A s/\s*;$//g unless /[=;+].*\S/ ;
1N/A
1N/A # Process the length(foo) declarations
1N/A if (s/^([^=]*)\blength\(\s*(\w+)\s*\)\s*$/$1 XSauto_length_of_$2=NO_INIT/x) {
1N/A print "\tSTRLEN\tSTRLEN_length_of_$2;\n";
1N/A $lengthof{$2} = $name;
1N/A # $islengthof{$name} = $1;
1N/A $deferred .= "\n\tXSauto_length_of_$2 = STRLEN_length_of_$2;";
1N/A }
1N/A
1N/A # check for optional initialisation code
1N/A my $var_init = '' ;
1N/A $var_init = $1 if s/\s*([=;+].*)$//s ;
1N/A $var_init =~ s/"/\\"/g;
1N/A
1N/A s/\s+/ /g;
1N/A my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s
1N/A or blurt("Error: invalid argument declaration '$line'"), next;
1N/A
1N/A # Check for duplicate definitions
1N/A blurt ("Error: duplicate definition of argument '$var_name' ignored"), next
1N/A if $arg_list{$var_name}++
1N/A or defined $argtype_seen{$var_name} and not $processing_arg_with_types;
1N/A
1N/A $thisdone |= $var_name eq "THIS";
1N/A $retvaldone |= $var_name eq "RETVAL";
1N/A $var_types{$var_name} = $var_type;
1N/A # XXXX This check is a safeguard against the unfinished conversion of
1N/A # generate_init(). When generate_init() is fixed,
1N/A # one can use 2-args map_type() unconditionally.
1N/A if ($var_type =~ / \( \s* \* \s* \) /x) {
1N/A # Function pointers are not yet supported with &output_init!
1N/A print "\t" . &map_type($var_type, $var_name);
1N/A $name_printed = 1;
1N/A } else {
1N/A print "\t" . &map_type($var_type);
1N/A $name_printed = 0;
1N/A }
1N/A $var_num = $args_match{$var_name};
1N/A
1N/A $proto_arg[$var_num] = ProtoString($var_type)
1N/A if $var_num ;
1N/A $func_args =~ s/\b($var_name)\b/&$1/ if $var_addr;
1N/A if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/
1N/A or $in_out{$var_name} and $in_out{$var_name} =~ /^OUT/
1N/A and $var_init !~ /\S/) {
1N/A if ($name_printed) {
1N/A print ";\n";
1N/A } else {
1N/A print "\t$var_name;\n";
1N/A }
1N/A } elsif ($var_init =~ /\S/) {
1N/A &output_init($var_type, $var_num, $var_name, $var_init, $name_printed);
1N/A } elsif ($var_num) {
1N/A # generate initialization code
1N/A &generate_init($var_type, $var_num, $var_name, $name_printed);
1N/A } else {
1N/A print ";\n";
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub OUTPUT_handler {
1N/A for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A next unless /\S/;
1N/A if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
1N/A $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0);
1N/A next;
1N/A }
1N/A my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ;
1N/A blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next
1N/A if $outargs{$outarg} ++ ;
1N/A if (!$gotRETVAL and $outarg eq 'RETVAL') {
1N/A # deal with RETVAL last
1N/A $RETVAL_code = $outcode ;
1N/A $gotRETVAL = 1 ;
1N/A next ;
1N/A }
1N/A blurt ("Error: OUTPUT $outarg not an argument"), next
1N/A unless defined($args_match{$outarg});
1N/A blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next
1N/A unless defined $var_types{$outarg} ;
1N/A $var_num = $args_match{$outarg};
1N/A if ($outcode) {
1N/A print "\t$outcode\n";
1N/A print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic;
1N/A } else {
1N/A &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic);
1N/A }
1N/A delete $in_out{$outarg} # No need to auto-OUTPUT
1N/A if exists $in_out{$outarg} and $in_out{$outarg} =~ /OUT$/;
1N/A }
1N/A}
1N/A
1N/Asub C_ARGS_handler() {
1N/A my $in = merge_section();
1N/A
1N/A TrimWhitespace($in);
1N/A $func_args = $in;
1N/A}
1N/A
1N/Asub INTERFACE_MACRO_handler() {
1N/A my $in = merge_section();
1N/A
1N/A TrimWhitespace($in);
1N/A if ($in =~ /\s/) { # two
1N/A ($interface_macro, $interface_macro_set) = split ' ', $in;
1N/A } else {
1N/A $interface_macro = $in;
1N/A $interface_macro_set = 'UNKNOWN_CVT'; # catch later
1N/A }
1N/A $interface = 1; # local
1N/A $Interfaces = 1; # global
1N/A}
1N/A
1N/Asub INTERFACE_handler() {
1N/A my $in = merge_section();
1N/A
1N/A TrimWhitespace($in);
1N/A
1N/A foreach (split /[\s,]+/, $in) {
1N/A $Interfaces{$_} = $_;
1N/A }
1N/A print Q<<"EOF";
1N/A# XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr);
1N/AEOF
1N/A $interface = 1; # local
1N/A $Interfaces = 1; # global
1N/A}
1N/A
1N/Asub CLEANUP_handler() { print_section() }
1N/Asub PREINIT_handler() { print_section() }
1N/Asub POSTCALL_handler() { print_section() }
1N/Asub INIT_handler() { print_section() }
1N/A
1N/Asub GetAliases
1N/A{
1N/A my ($line) = @_ ;
1N/A my ($orig) = $line ;
1N/A my ($alias) ;
1N/A my ($value) ;
1N/A
1N/A # Parse alias definitions
1N/A # format is
1N/A # alias = value alias = value ...
1N/A
1N/A while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) {
1N/A $alias = $1 ;
1N/A $orig_alias = $alias ;
1N/A $value = $2 ;
1N/A
1N/A # check for optional package definition in the alias
1N/A $alias = $Packprefix . $alias if $alias !~ /::/ ;
1N/A
1N/A # check for duplicate alias name & duplicate value
1N/A Warn("Warning: Ignoring duplicate alias '$orig_alias'")
1N/A if defined $XsubAliases{$alias} ;
1N/A
1N/A Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values")
1N/A if $XsubAliasValues{$value} ;
1N/A
1N/A $XsubAliases = 1;
1N/A $XsubAliases{$alias} = $value ;
1N/A $XsubAliasValues{$value} = $orig_alias ;
1N/A }
1N/A
1N/A blurt("Error: Cannot parse ALIAS definitions from '$orig'")
1N/A if $line ;
1N/A}
1N/A
1N/Asub ATTRS_handler ()
1N/A{
1N/A for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A next unless /\S/;
1N/A TrimWhitespace($_) ;
1N/A push @Attributes, $_;
1N/A }
1N/A}
1N/A
1N/Asub ALIAS_handler ()
1N/A{
1N/A for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A next unless /\S/;
1N/A TrimWhitespace($_) ;
1N/A GetAliases($_) if $_ ;
1N/A }
1N/A}
1N/A
1N/Asub OVERLOAD_handler()
1N/A{
1N/A for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A next unless /\S/;
1N/A TrimWhitespace($_) ;
1N/A while ( s/^\s*([\w:"\\)\+\-\*\/\%\<\>\.\&\|\^\!\~\{\}\=]+)\s*//) {
1N/A $Overload = 1 unless $Overload;
1N/A my $overload = "$Package\::(".$1 ;
1N/A push(@InitFileCode,
1N/A " newXS(\"$overload\", XS_$Full_func_name, file$proto);\n");
1N/A }
1N/A }
1N/A
1N/A}
1N/A
1N/Asub FALLBACK_handler()
1N/A{
1N/A # the rest of the current line should contain either TRUE,
1N/A # FALSE or UNDEF
1N/A
1N/A TrimWhitespace($_) ;
1N/A my %map = (
1N/A TRUE => "PL_sv_yes", 1 => "PL_sv_yes",
1N/A FALSE => "PL_sv_no", 0 => "PL_sv_no",
1N/A UNDEF => "PL_sv_undef",
1N/A ) ;
1N/A
1N/A # check for valid FALLBACK value
1N/A death ("Error: FALLBACK: TRUE/FALSE/UNDEF") unless exists $map{uc $_} ;
1N/A
1N/A $Fallback = $map{uc $_} ;
1N/A}
1N/A
1N/Asub REQUIRE_handler ()
1N/A{
1N/A # the rest of the current line should contain a version number
1N/A my ($Ver) = $_ ;
1N/A
1N/A TrimWhitespace($Ver) ;
1N/A
1N/A death ("Error: REQUIRE expects a version number")
1N/A unless $Ver ;
1N/A
1N/A # check that the version number is of the form n.n
1N/A death ("Error: REQUIRE: expected a number, got '$Ver'")
1N/A unless $Ver =~ /^\d+(\.\d*)?/ ;
1N/A
1N/A death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.")
1N/A unless $XSUBPP_version >= $Ver ;
1N/A}
1N/A
1N/Asub VERSIONCHECK_handler ()
1N/A{
1N/A # the rest of the current line should contain either ENABLE or
1N/A # DISABLE
1N/A
1N/A TrimWhitespace($_) ;
1N/A
1N/A # check for ENABLE/DISABLE
1N/A death ("Error: VERSIONCHECK: ENABLE/DISABLE")
1N/A unless /^(ENABLE|DISABLE)/i ;
1N/A
1N/A $WantVersionChk = 1 if $1 eq 'ENABLE' ;
1N/A $WantVersionChk = 0 if $1 eq 'DISABLE' ;
1N/A
1N/A}
1N/A
1N/Asub PROTOTYPE_handler ()
1N/A{
1N/A my $specified ;
1N/A
1N/A death("Error: Only 1 PROTOTYPE definition allowed per xsub")
1N/A if $proto_in_this_xsub ++ ;
1N/A
1N/A for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A next unless /\S/;
1N/A $specified = 1 ;
1N/A TrimWhitespace($_) ;
1N/A if ($_ eq 'DISABLE') {
1N/A $ProtoThisXSUB = 0
1N/A }
1N/A elsif ($_ eq 'ENABLE') {
1N/A $ProtoThisXSUB = 1
1N/A }
1N/A else {
1N/A # remove any whitespace
1N/A s/\s+//g ;
1N/A death("Error: Invalid prototype '$_'")
1N/A unless ValidProtoString($_) ;
1N/A $ProtoThisXSUB = C_string($_) ;
1N/A }
1N/A }
1N/A
1N/A # If no prototype specified, then assume empty prototype ""
1N/A $ProtoThisXSUB = 2 unless $specified ;
1N/A
1N/A $ProtoUsed = 1 ;
1N/A
1N/A}
1N/A
1N/Asub SCOPE_handler ()
1N/A{
1N/A death("Error: Only 1 SCOPE declaration allowed per xsub")
1N/A if $scope_in_this_xsub ++ ;
1N/A
1N/A for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
1N/A next unless /\S/;
1N/A TrimWhitespace($_) ;
1N/A if ($_ =~ /^DISABLE/i) {
1N/A $ScopeThisXSUB = 0
1N/A }
1N/A elsif ($_ =~ /^ENABLE/i) {
1N/A $ScopeThisXSUB = 1
1N/A }
1N/A }
1N/A
1N/A}
1N/A
1N/Asub PROTOTYPES_handler ()
1N/A{
1N/A # the rest of the current line should contain either ENABLE or
1N/A # DISABLE
1N/A
1N/A TrimWhitespace($_) ;
1N/A
1N/A # check for ENABLE/DISABLE
1N/A death ("Error: PROTOTYPES: ENABLE/DISABLE")
1N/A unless /^(ENABLE|DISABLE)/i ;
1N/A
1N/A $WantPrototypes = 1 if $1 eq 'ENABLE' ;
1N/A $WantPrototypes = 0 if $1 eq 'DISABLE' ;
1N/A $ProtoUsed = 1 ;
1N/A
1N/A}
1N/A
1N/Asub INCLUDE_handler ()
1N/A{
1N/A # the rest of the current line should contain a valid filename
1N/A
1N/A TrimWhitespace($_) ;
1N/A
1N/A death("INCLUDE: filename missing")
1N/A unless $_ ;
1N/A
1N/A death("INCLUDE: output pipe is illegal")
1N/A if /^\s*\|/ ;
1N/A
1N/A # simple minded recursion detector
1N/A death("INCLUDE loop detected")
1N/A if $IncludedFiles{$_} ;
1N/A
1N/A ++ $IncludedFiles{$_} unless /\|\s*$/ ;
1N/A
1N/A # Save the current file context.
1N/A push(@XSStack, {
1N/A type => 'file',
1N/A LastLine => $lastline,
1N/A LastLineNo => $lastline_no,
1N/A Line => \@line,
1N/A LineNo => \@line_no,
1N/A Filename => $filename,
1N/A Handle => $FH,
1N/A }) ;
1N/A
1N/A ++ $FH ;
1N/A
1N/A # open the new file
1N/A open ($FH, "$_") or death("Cannot open '$_': $!") ;
1N/A
1N/A print Q<<"EOF" ;
1N/A#
1N/A#/* INCLUDE: Including '$_' from '$filename' */
1N/A#
1N/AEOF
1N/A
1N/A $filename = $_ ;
1N/A
1N/A # Prime the pump by reading the first
1N/A # non-blank line
1N/A
1N/A # skip leading blank lines
1N/A while (<$FH>) {
1N/A last unless /^\s*$/ ;
1N/A }
1N/A
1N/A $lastline = $_ ;
1N/A $lastline_no = $. ;
1N/A
1N/A}
1N/A
1N/Asub PopFile()
1N/A{
1N/A return 0 unless $XSStack[-1]{type} eq 'file' ;
1N/A
1N/A my $data = pop @XSStack ;
1N/A my $ThisFile = $filename ;
1N/A my $isPipe = ($filename =~ /\|\s*$/) ;
1N/A
1N/A -- $IncludedFiles{$filename}
1N/A unless $isPipe ;
1N/A
1N/A close $FH ;
1N/A
1N/A $FH = $data->{Handle} ;
1N/A $filename = $data->{Filename} ;
1N/A $lastline = $data->{LastLine} ;
1N/A $lastline_no = $data->{LastLineNo} ;
1N/A @line = @{ $data->{Line} } ;
1N/A @line_no = @{ $data->{LineNo} } ;
1N/A
1N/A if ($isPipe and $? ) {
1N/A -- $lastline_no ;
1N/A print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ;
1N/A exit 1 ;
1N/A }
1N/A
1N/A print Q<<"EOF" ;
1N/A#
1N/A#/* INCLUDE: Returning to '$filename' from '$ThisFile' */
1N/A#
1N/AEOF
1N/A
1N/A return 1 ;
1N/A}
1N/A
1N/Asub ValidProtoString ($)
1N/A{
1N/A my($string) = @_ ;
1N/A
1N/A if ( $string =~ /^$proto_re+$/ ) {
1N/A return $string ;
1N/A }
1N/A
1N/A return 0 ;
1N/A}
1N/A
1N/Asub C_string ($)
1N/A{
1N/A my($string) = @_ ;
1N/A
1N/A $string =~ s[\\][\\\\]g ;
1N/A $string ;
1N/A}
1N/A
1N/Asub ProtoString ($)
1N/A{
1N/A my ($type) = @_ ;
1N/A
1N/A $proto_letter{$type} or "\$" ;
1N/A}
1N/A
1N/Asub check_cpp {
1N/A my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line);
1N/A if (@cpp) {
1N/A my ($cpp, $cpplevel);
1N/A for $cpp (@cpp) {
1N/A if ($cpp =~ /^\#\s*if/) {
1N/A $cpplevel++;
1N/A } elsif (!$cpplevel) {
1N/A Warn("Warning: #else/elif/endif without #if in this function");
1N/A print STDERR " (precede it with a blank line if the matching #if is outside the function)\n"
1N/A if $XSStack[-1]{type} eq 'if';
1N/A return;
1N/A } elsif ($cpp =~ /^\#\s*endif/) {
1N/A $cpplevel--;
1N/A }
1N/A }
1N/A Warn("Warning: #if without #endif in this function") if $cpplevel;
1N/A }
1N/A}
1N/A
1N/A
1N/Asub Q {
1N/A my($text) = @_;
1N/A $text =~ s/^#//gm;
1N/A $text =~ s/\[\[/{/g;
1N/A $text =~ s/\]\]/}/g;
1N/A $text;
1N/A}
1N/A
1N/Aopen($FH, $filename) or die "cannot open $filename: $!\n";
1N/A
1N/A# Identify the version of xsubpp used
1N/Aprint <<EOM ;
1N/A/*
1N/A * This file was generated automatically by xsubpp version $XSUBPP_version from the
1N/A * contents of $filename. Do not edit this file, edit $filename instead.
1N/A *
1N/A * ANY CHANGES MADE HERE WILL BE LOST!
1N/A *
1N/A */
1N/A
1N/AEOM
1N/A
1N/A
1N/Aprint("#line 1 \"$filename\"\n")
1N/A if $WantLineNumbers;
1N/A
1N/Afirstmodule:
1N/Awhile (<$FH>) {
1N/A if (/^=/) {
1N/A my $podstartline = $.;
1N/A do {
1N/A if (/^=cut\s*$/) {
1N/A # We can't just write out a /* */ comment, as our embedded
1N/A # POD might itself be in a comment. We can't put a /**/
1N/A # comment inside #if 0, as the C standard says that the source
1N/A # file is decomposed into preprocessing characters in the stage
1N/A # before preprocessing commands are executed.
1N/A # I don't want to leave the text as barewords, because the spec
1N/A # isn't clear whether macros are expanded before or after
1N/A # preprocessing commands are executed, and someone pathological
1N/A # may just have defined one of the 3 words as a macro that does
1N/A # something strange. Multiline strings are illegal in C, so
1N/A # the "" we write must be a string literal. And they aren't
1N/A # concatenated until 2 steps later, so we are safe.
1N/A print("#if 0\n \"Skipped embedded POD.\"\n#endif\n");
1N/A printf("#line %d \"$filename\"\n", $. + 1)
1N/A if $WantLineNumbers;
1N/A next firstmodule
1N/A }
1N/A
1N/A } while (<$FH>);
1N/A # At this point $. is at end of file so die won't state the start
1N/A # of the problem, and as we haven't yet read any lines &death won't
1N/A # show the correct line in the message either.
1N/A die ("Error: Unterminated pod in $filename, line $podstartline\n")
1N/A unless $lastline;
1N/A }
1N/A last if ($Module, $Package, $Prefix) =
1N/A /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
1N/A
1N/A print $_;
1N/A}
1N/A&Exit unless defined $_;
1N/A
1N/Aprint "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
1N/A
1N/A$lastline = $_;
1N/A$lastline_no = $.;
1N/A
1N/A# Read next xsub into @line from ($lastline, <$FH>).
1N/Asub fetch_para {
1N/A # parse paragraph
1N/A death ("Error: Unterminated `#if/#ifdef/#ifndef'")
1N/A if !defined $lastline && $XSStack[-1]{type} eq 'if';
1N/A @line = ();
1N/A @line_no = () ;
1N/A return PopFile() if !defined $lastline;
1N/A
1N/A if ($lastline =~
1N/A /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
1N/A $Module = $1;
1N/A $Package = defined($2) ? $2 : ''; # keep -w happy
1N/A $Prefix = defined($3) ? $3 : ''; # keep -w happy
1N/A $Prefix = quotemeta $Prefix ;
1N/A ($Module_cname = $Module) =~ s/\W/_/g;
1N/A ($Packid = $Package) =~ tr/:/_/;
1N/A $Packprefix = $Package;
1N/A $Packprefix .= "::" if $Packprefix ne "";
1N/A $lastline = "";
1N/A }
1N/A
1N/A for(;;) {
1N/A # Skip embedded PODs
1N/A while ($lastline =~ /^=/) {
1N/A while ($lastline = <$FH>) {
1N/A last if ($lastline =~ /^=cut\s*$/);
1N/A }
1N/A death ("Error: Unterminated pod") unless $lastline;
1N/A $lastline = <$FH>;
1N/A chomp $lastline;
1N/A $lastline =~ s/^\s+$//;
1N/A }
1N/A if ($lastline !~ /^\s*#/ ||
1N/A # CPP directives:
1N/A # ANSI: if ifdef ifndef elif else endif define undef
1N/A # line error pragma
1N/A # gcc: warning include_next
1N/A # obj-c: import
1N/A # others: ident (gcc notes that some cpps have this one)
1N/A $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
1N/A last if $lastline =~ /^\S/ && @line && $line[-1] eq "";
1N/A push(@line, $lastline);
1N/A push(@line_no, $lastline_no) ;
1N/A }
1N/A
1N/A # Read next line and continuation lines
1N/A last unless defined($lastline = <$FH>);
1N/A $lastline_no = $.;
1N/A my $tmp_line;
1N/A $lastline .= $tmp_line
1N/A while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>));
1N/A
1N/A chomp $lastline;
1N/A $lastline =~ s/^\s+$//;
1N/A }
1N/A pop(@line), pop(@line_no) while @line && $line[-1] eq "";
1N/A 1;
1N/A}
1N/A
1N/APARAGRAPH:
1N/Awhile (fetch_para()) {
1N/A # Print initial preprocessor statements and blank lines
1N/A while (@line && $line[0] !~ /^[^\#]/) {
1N/A my $line = shift(@line);
1N/A print $line, "\n";
1N/A next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
1N/A my $statement = $+;
1N/A if ($statement eq 'if') {
1N/A $XSS_work_idx = @XSStack;
1N/A push(@XSStack, {type => 'if'});
1N/A } else {
1N/A death ("Error: `$statement' with no matching `if'")
1N/A if $XSStack[-1]{type} ne 'if';
1N/A if ($XSStack[-1]{varname}) {
1N/A push(@InitFileCode, "#endif\n");
1N/A push(@BootCode, "#endif");
1N/A }
1N/A
1N/A my(@fns) = keys %{$XSStack[-1]{functions}};
1N/A if ($statement ne 'endif') {
1N/A # Hide the functions defined in other #if branches, and reset.
1N/A @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns;
1N/A @{$XSStack[-1]}{qw(varname functions)} = ('', {});
1N/A } else {
1N/A my($tmp) = pop(@XSStack);
1N/A 0 while (--$XSS_work_idx
1N/A && $XSStack[$XSS_work_idx]{type} ne 'if');
1N/A # Keep all new defined functions
1N/A push(@fns, keys %{$tmp->{other_functions}});
1N/A @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
1N/A }
1N/A }
1N/A }
1N/A
1N/A next PARAGRAPH unless @line;
1N/A
1N/A if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) {
1N/A # We are inside an #if, but have not yet #defined its xsubpp variable.
1N/A print "#define $cpp_next_tmp 1\n\n";
1N/A push(@InitFileCode, "#if $cpp_next_tmp\n");
1N/A push(@BootCode, "#if $cpp_next_tmp");
1N/A $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++;
1N/A }
1N/A
1N/A death ("Code is not inside a function"
1N/A ." (maybe last function was ended by a blank line "
1N/A ." followed by a statement on column one?)")
1N/A if $line[0] =~ /^\s/;
1N/A
1N/A # initialize info arrays
1N/A undef(%args_match);
1N/A undef(%var_types);
1N/A undef(%defaults);
1N/A undef($class);
1N/A undef($static);
1N/A undef($elipsis);
1N/A undef($wantRETVAL) ;
1N/A undef($RETVAL_no_return) ;
1N/A undef(%arg_list) ;
1N/A undef(@proto_arg) ;
1N/A undef(@fake_INPUT_pre) ; # For length(s) generated variables
1N/A undef(@fake_INPUT) ;
1N/A undef($processing_arg_with_types) ;
1N/A undef(%argtype_seen) ;
1N/A undef(@outlist) ;
1N/A undef(%in_out) ;
1N/A undef(%lengthof) ;
1N/A # undef(%islengthof) ;
1N/A undef($proto_in_this_xsub) ;
1N/A undef($scope_in_this_xsub) ;
1N/A undef($interface);
1N/A undef($prepush_done);
1N/A $interface_macro = 'XSINTERFACE_FUNC' ;
1N/A $interface_macro_set = 'XSINTERFACE_FUNC_SET' ;
1N/A $ProtoThisXSUB = $WantPrototypes ;
1N/A $ScopeThisXSUB = 0;
1N/A $xsreturn = 0;
1N/A
1N/A $_ = shift(@line);
1N/A while ($kwd = check_keyword("REQUIRE|PROTOTYPES|FALLBACK|VERSIONCHECK|INCLUDE")) {
1N/A &{"${kwd}_handler"}() ;
1N/A next PARAGRAPH unless @line ;
1N/A $_ = shift(@line);
1N/A }
1N/A
1N/A if (check_keyword("BOOT")) {
1N/A &check_cpp;
1N/A push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"")
1N/A if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/;
1N/A push (@BootCode, @line, "") ;
1N/A next PARAGRAPH ;
1N/A }
1N/A
1N/A
1N/A # extract return type, function name and arguments
1N/A ($ret_type) = TidyType($_);
1N/A $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//;
1N/A
1N/A # Allow one-line ANSI-like declaration
1N/A unshift @line, $2
1N/A if $process_argtypes
1N/A and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s;
1N/A
1N/A # a function definition needs at least 2 lines
1N/A blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH
1N/A unless @line ;
1N/A
1N/A $static = 1 if $ret_type =~ s/^static\s+//;
1N/A
1N/A $func_header = shift(@line);
1N/A blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH
1N/A unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
1N/A
1N/A ($class, $func_name, $orig_args) = ($1, $2, $3) ;
1N/A $class = "$4 $class" if $4;
1N/A ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/;
1N/A ($clean_func_name = $func_name) =~ s/^$Prefix//;
1N/A $Full_func_name = "${Packid}_$clean_func_name";
1N/A if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); }
1N/A
1N/A # Check for duplicate function definition
1N/A for $tmp (@XSStack) {
1N/A next unless defined $tmp->{functions}{$Full_func_name};
1N/A Warn("Warning: duplicate function definition '$clean_func_name' detected");
1N/A last;
1N/A }
1N/A $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ;
1N/A %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = ();
1N/A $DoSetMagic = 1;
1N/A
1N/A $orig_args =~ s/\\\s*/ /g; # process line continuations
1N/A
1N/A my %only_C_inlist; # Not in the signature of Perl function
1N/A if ($process_argtypes and $orig_args =~ /\S/) {
1N/A my $args = "$orig_args ,";
1N/A if ($args =~ /^( (??{ $C_arg }) , )* $ /x) {
1N/A @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg);
1N/A for ( @args ) {
1N/A s/^\s+//;
1N/A s/\s+$//;
1N/A my ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x;
1N/A my ($pre, $name) = ($arg =~ /(.*?) \s*
1N/A \b ( \w+ | length\( \s*\w+\s* \) )
1N/A \s* $ /x);
1N/A next unless length $pre;
1N/A my $out_type;
1N/A my $inout_var;
1N/A if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//) {
1N/A my $type = $1;
1N/A $out_type = $type if $type ne 'IN';
1N/A $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//;
1N/A $pre =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//;
1N/A }
1N/A my $islength;
1N/A if ($name =~ /^length\( \s* (\w+) \s* \)\z/x) {
1N/A $name = "XSauto_length_of_$1";
1N/A $islength = 1;
1N/A die "Default value on length() argument: `$_'"
1N/A if length $default;
1N/A }
1N/A if (length $pre or $islength) { # Has a type
1N/A if ($islength) {
1N/A push @fake_INPUT_pre, $arg;
1N/A } else {
1N/A push @fake_INPUT, $arg;
1N/A }
1N/A # warn "pushing '$arg'\n";
1N/A $argtype_seen{$name}++;
1N/A $_ = "$name$default"; # Assigns to @args
1N/A }
1N/A $only_C_inlist{$_} = 1 if $out_type eq "OUTLIST" or $islength;
1N/A push @outlist, $name if $out_type =~ /OUTLIST$/;
1N/A $in_out{$name} = $out_type if $out_type;
1N/A }
1N/A } else {
1N/A @args = split(/\s*,\s*/, $orig_args);
1N/A Warn("Warning: cannot parse argument list '$orig_args', fallback to split");
1N/A }
1N/A } else {
1N/A @args = split(/\s*,\s*/, $orig_args);
1N/A for (@args) {
1N/A if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\s+//) {
1N/A my $out_type = $1;
1N/A next if $out_type eq 'IN';
1N/A $only_C_inlist{$_} = 1 if $out_type eq "OUTLIST";
1N/A push @outlist, $name if $out_type =~ /OUTLIST$/;
1N/A $in_out{$_} = $out_type;
1N/A }
1N/A }
1N/A }
1N/A if (defined($class)) {
1N/A my $arg0 = ((defined($static) or $func_name eq 'new')
1N/A ? "CLASS" : "THIS");
1N/A unshift(@args, $arg0);
1N/A ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/;
1N/A }
1N/A my $extra_args = 0;
1N/A @args_num = ();
1N/A $num_args = 0;
1N/A my $report_args = '';
1N/A foreach $i (0 .. $#args) {
1N/A if ($args[$i] =~ s/\.\.\.//) {
1N/A $elipsis = 1;
1N/A if ($args[$i] eq '' && $i == $#args) {
1N/A $report_args .= ", ...";
1N/A pop(@args);
1N/A last;
1N/A }
1N/A }
1N/A if ($only_C_inlist{$args[$i]}) {
1N/A push @args_num, undef;
1N/A } else {
1N/A push @args_num, ++$num_args;
1N/A $report_args .= ", $args[$i]";
1N/A }
1N/A if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) {
1N/A $extra_args++;
1N/A $args[$i] = $1;
1N/A $defaults{$args[$i]} = $2;
1N/A $defaults{$args[$i]} =~ s/"/\\"/g;
1N/A }
1N/A $proto_arg[$i+1] = "\$" ;
1N/A }
1N/A $min_args = $num_args - $extra_args;
1N/A $report_args =~ s/"/\\"/g;
1N/A $report_args =~ s/^,\s+//;
1N/A my @func_args = @args;
1N/A shift @func_args if defined($class);
1N/A
1N/A for (@func_args) {
1N/A s/^/&/ if $in_out{$_};
1N/A }
1N/A $func_args = join(", ", @func_args);
1N/A @args_match{@args} = @args_num;
1N/A
1N/A $PPCODE = grep(/^\s*PPCODE\s*:/, @line);
1N/A $CODE = grep(/^\s*CODE\s*:/, @line);
1N/A # Detect CODE: blocks which use ST(n)= or XST_m*(n,v)
1N/A # to set explicit return values.
1N/A $EXPLICIT_RETURN = ($CODE &&
1N/A ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x ));
1N/A $ALIAS = grep(/^\s*ALIAS\s*:/, @line);
1N/A $INTERFACE = grep(/^\s*INTERFACE\s*:/, @line);
1N/A
1N/A $xsreturn = 1 if $EXPLICIT_RETURN;
1N/A
1N/A # print function header
1N/A print Q<<"EOF";
1N/A#XS(XS_${Full_func_name}); /* prototype to pass -Wmissing-prototypes */
1N/A#XS(XS_${Full_func_name})
1N/A#[[
1N/A# dXSARGS;
1N/AEOF
1N/A print Q<<"EOF" if $ALIAS ;
1N/A# dXSI32;
1N/AEOF
1N/A print Q<<"EOF" if $INTERFACE ;
1N/A# dXSFUNCTION($ret_type);
1N/AEOF
1N/A if ($elipsis) {
1N/A $cond = ($min_args ? qq(items < $min_args) : 0);
1N/A }
1N/A elsif ($min_args == $num_args) {
1N/A $cond = qq(items != $min_args);
1N/A }
1N/A else {
1N/A $cond = qq(items < $min_args || items > $num_args);
1N/A }
1N/A
1N/A print Q<<"EOF" if $except;
1N/A# char errbuf[1024];
1N/A# *errbuf = '\0';
1N/AEOF
1N/A
1N/A if ($ALIAS)
1N/A { print Q<<"EOF" if $cond }
1N/A# if ($cond)
1N/A# Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv)));
1N/AEOF
1N/A else
1N/A { print Q<<"EOF" if $cond }
1N/A# if ($cond)
1N/A# Perl_croak(aTHX_ "Usage: $pname($report_args)");
1N/AEOF
1N/A
1N/A #gcc -Wall: if an xsub has no arguments and PPCODE is used
1N/A #it is likely none of ST, XSRETURN or XSprePUSH macros are used
1N/A #hence `ax' (setup by dXSARGS) is unused
1N/A #XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS
1N/A #but such a move could break third-party extensions
1N/A print Q<<"EOF" if $PPCODE and $num_args == 0;
1N/A# PERL_UNUSED_VAR(ax); /* -Wall */
1N/AEOF
1N/A
1N/A print Q<<"EOF" if $PPCODE;
1N/A# SP -= items;
1N/AEOF
1N/A
1N/A # Now do a block of some sort.
1N/A
1N/A $condnum = 0;
1N/A $cond = ''; # last CASE: condidional
1N/A push(@line, "$END:");
1N/A push(@line_no, $line_no[-1]);
1N/A $_ = '';
1N/A &check_cpp;
1N/A while (@line) {
1N/A &CASE_handler if check_keyword("CASE");
1N/A print Q<<"EOF";
1N/A# $except [[
1N/AEOF
1N/A
1N/A # do initialization of input variables
1N/A $thisdone = 0;
1N/A $retvaldone = 0;
1N/A $deferred = "";
1N/A %arg_list = () ;
1N/A $gotRETVAL = 0;
1N/A
1N/A INPUT_handler() ;
1N/A process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE|OVERLOAD") ;
1N/A
1N/A print Q<<"EOF" if $ScopeThisXSUB;
1N/A# ENTER;
1N/A# [[
1N/AEOF
1N/A
1N/A if (!$thisdone && defined($class)) {
1N/A if (defined($static) or $func_name eq 'new') {
1N/A print "\tchar *";
1N/A $var_types{"CLASS"} = "char *";
1N/A &generate_init("char *", 1, "CLASS");
1N/A }
1N/A else {
1N/A print "\t$class *";
1N/A $var_types{"THIS"} = "$class *";
1N/A &generate_init("$class *", 1, "THIS");
1N/A }
1N/A }
1N/A
1N/A # do code
1N/A if (/^\s*NOT_IMPLEMENTED_YET/) {
1N/A print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n";
1N/A $_ = '' ;
1N/A } else {
1N/A if ($ret_type ne "void") {
1N/A print "\t" . &map_type($ret_type, 'RETVAL') . ";\n"
1N/A if !$retvaldone;
1N/A $args_match{"RETVAL"} = 0;
1N/A $var_types{"RETVAL"} = $ret_type;
1N/A print "\tdXSTARG;\n"
1N/A if $WantOptimize and $targetable{$type_kind{$ret_type}};
1N/A }
1N/A
1N/A if (@fake_INPUT or @fake_INPUT_pre) {
1N/A unshift @line, @fake_INPUT_pre, @fake_INPUT, $_;
1N/A $_ = "";
1N/A $processing_arg_with_types = 1;
1N/A INPUT_handler() ;
1N/A }
1N/A print $deferred;
1N/A
1N/A process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS|OVERLOAD") ;
1N/A
1N/A if (check_keyword("PPCODE")) {
1N/A print_section();
1N/A death ("PPCODE must be last thing") if @line;
1N/A print "\tLEAVE;\n" if $ScopeThisXSUB;
1N/A print "\tPUTBACK;\n\treturn;\n";
1N/A } elsif (check_keyword("CODE")) {
1N/A print_section() ;
1N/A } elsif (defined($class) and $func_name eq "DESTROY") {
1N/A print "\n\t";
1N/A print "delete THIS;\n";
1N/A } else {
1N/A print "\n\t";
1N/A if ($ret_type ne "void") {
1N/A print "RETVAL = ";
1N/A $wantRETVAL = 1;
1N/A }
1N/A if (defined($static)) {
1N/A if ($func_name eq 'new') {
1N/A $func_name = "$class";
1N/A } else {
1N/A print "${class}::";
1N/A }
1N/A } elsif (defined($class)) {
1N/A if ($func_name eq 'new') {
1N/A $func_name .= " $class";
1N/A } else {
1N/A print "THIS->";
1N/A }
1N/A }
1N/A $func_name =~ s/^($spat)//
1N/A if defined($spat);
1N/A $func_name = 'XSFUNCTION' if $interface;
1N/A print "$func_name($func_args);\n";
1N/A }
1N/A }
1N/A
1N/A # do output variables
1N/A $gotRETVAL = 0; # 1 if RETVAL seen in OUTPUT section;
1N/A undef $RETVAL_code ; # code to set RETVAL (from OUTPUT section);
1N/A # $wantRETVAL set if 'RETVAL =' autogenerated
1N/A ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return;
1N/A undef %outargs ;
1N/A process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE|OVERLOAD");
1N/A
1N/A &generate_output($var_types{$_}, $args_match{$_}, $_, $DoSetMagic)
1N/A for grep $in_out{$_} =~ /OUT$/, keys %in_out;
1N/A
1N/A # all OUTPUT done, so now push the return value on the stack
1N/A if ($gotRETVAL && $RETVAL_code) {
1N/A print "\t$RETVAL_code\n";
1N/A } elsif ($gotRETVAL || $wantRETVAL) {
1N/A my $t = $WantOptimize && $targetable{$type_kind{$ret_type}};
1N/A my $var = 'RETVAL';
1N/A my $type = $ret_type;
1N/A
1N/A # 0: type, 1: with_size, 2: how, 3: how_size
1N/A if ($t and not $t->[1] and $t->[0] eq 'p') {
1N/A # PUSHp corresponds to setpvn. Treate setpv directly
1N/A my $what = eval qq("$t->[2]");
1N/A warn $@ if $@;
1N/A
1N/A print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
1N/A $prepush_done = 1;
1N/A }
1N/A elsif ($t) {
1N/A my $what = eval qq("$t->[2]");
1N/A warn $@ if $@;
1N/A
1N/A my $size = $t->[3];
1N/A $size = '' unless defined $size;
1N/A $size = eval qq("$size");
1N/A warn $@ if $@;
1N/A print "\tXSprePUSH; PUSH$t->[0]($what$size);\n";
1N/A $prepush_done = 1;
1N/A }
1N/A else {
1N/A # RETVAL almost never needs SvSETMAGIC()
1N/A &generate_output($ret_type, 0, 'RETVAL', 0);
1N/A }
1N/A }
1N/A
1N/A $xsreturn = 1 if $ret_type ne "void";
1N/A my $num = $xsreturn;
1N/A my $c = @outlist;
1N/A # (PP)CODE set different values of SP; reset to PPCODE's with 0 output
1N/A print "\tXSprePUSH;" if $c and not $prepush_done;
1N/A # Take into account stuff already put on stack
1N/A print "\t++SP;" if $c and not $prepush_done and $xsreturn;
1N/A # Now SP corresponds to ST($xsreturn), so one can combine PUSH and ST()
1N/A print "\tEXTEND(SP,$c);\n" if $c;
1N/A $xsreturn += $c;
1N/A generate_output($var_types{$_}, $num++, $_, 0, 1) for @outlist;
1N/A
1N/A # do cleanup
1N/A process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE|OVERLOAD") ;
1N/A
1N/A print Q<<"EOF" if $ScopeThisXSUB;
1N/A# ]]
1N/AEOF
1N/A print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE;
1N/A# LEAVE;
1N/AEOF
1N/A
1N/A # print function trailer
1N/A print Q<<EOF;
1N/A# ]]
1N/AEOF
1N/A print Q<<EOF if $except;
1N/A# BEGHANDLERS
1N/A# CATCHALL
1N/A# sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
1N/A# ENDHANDLERS
1N/AEOF
1N/A if (check_keyword("CASE")) {
1N/A blurt ("Error: No `CASE:' at top of function")
1N/A unless $condnum;
1N/A $_ = "CASE: $_"; # Restore CASE: label
1N/A next;
1N/A }
1N/A last if $_ eq "$END:";
1N/A death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function");
1N/A }
1N/A
1N/A print Q<<EOF if $except;
1N/A# if (errbuf[0])
1N/A# Perl_croak(aTHX_ errbuf);
1N/AEOF
1N/A
1N/A if ($xsreturn) {
1N/A print Q<<EOF unless $PPCODE;
1N/A# XSRETURN($xsreturn);
1N/AEOF
1N/A } else {
1N/A print Q<<EOF unless $PPCODE;
1N/A# XSRETURN_EMPTY;
1N/AEOF
1N/A }
1N/A
1N/A print Q<<EOF;
1N/A#]]
1N/A#
1N/AEOF
1N/A
1N/A my $newXS = "newXS" ;
1N/A my $proto = "" ;
1N/A
1N/A # Build the prototype string for the xsub
1N/A if ($ProtoThisXSUB) {
1N/A $newXS = "newXSproto";
1N/A
1N/A if ($ProtoThisXSUB eq 2) {
1N/A # User has specified empty prototype
1N/A $proto = ', ""' ;
1N/A }
1N/A elsif ($ProtoThisXSUB ne 1) {
1N/A # User has specified a prototype
1N/A $proto = ', "' . $ProtoThisXSUB . '"';
1N/A }
1N/A else {
1N/A my $s = ';';
1N/A if ($min_args < $num_args) {
1N/A $s = '';
1N/A $proto_arg[$min_args] .= ";" ;
1N/A }
1N/A push @proto_arg, "$s\@"
1N/A if $elipsis ;
1N/A
1N/A $proto = ', "' . join ("", @proto_arg) . '"';
1N/A }
1N/A }
1N/A
1N/A if (%XsubAliases) {
1N/A $XsubAliases{$pname} = 0
1N/A unless defined $XsubAliases{$pname} ;
1N/A while ( ($name, $value) = each %XsubAliases) {
1N/A push(@InitFileCode, Q<<"EOF");
1N/A# cv = newXS(\"$name\", XS_$Full_func_name, file);
1N/A# XSANY.any_i32 = $value ;
1N/AEOF
1N/A push(@InitFileCode, Q<<"EOF") if $proto;
1N/A# sv_setpv((SV*)cv$proto) ;
1N/AEOF
1N/A }
1N/A }
1N/A elsif (@Attributes) {
1N/A push(@InitFileCode, Q<<"EOF");
1N/A# cv = newXS(\"$pname\", XS_$Full_func_name, file);
1N/A# apply_attrs_string("$Package", cv, "@Attributes", 0);
1N/AEOF
1N/A }
1N/A elsif ($interface) {
1N/A while ( ($name, $value) = each %Interfaces) {
1N/A $name = "$Package\::$name" unless $name =~ /::/;
1N/A push(@InitFileCode, Q<<"EOF");
1N/A# cv = newXS(\"$name\", XS_$Full_func_name, file);
1N/A# $interface_macro_set(cv,$value) ;
1N/AEOF
1N/A push(@InitFileCode, Q<<"EOF") if $proto;
1N/A# sv_setpv((SV*)cv$proto) ;
1N/AEOF
1N/A }
1N/A }
1N/A else {
1N/A push(@InitFileCode,
1N/A " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n");
1N/A }
1N/A}
1N/A
1N/Aif ($Overload) # make it findable with fetchmethod
1N/A{
1N/A
1N/A print Q<<"EOF";
1N/A#XS(XS_${Packid}_nil); /* prototype to pass -Wmissing-prototypes */
1N/A#XS(XS_${Packid}_nil)
1N/A#{
1N/A# XSRETURN_EMPTY;
1N/A#}
1N/A#
1N/AEOF
1N/A unshift(@InitFileCode, <<"MAKE_FETCHMETHOD_WORK");
1N/A /* Making a sub named "${Package}::()" allows the package */
1N/A /* to be findable via fetchmethod(), and causes */
1N/A /* overload::Overloaded("${Package}") to return true. */
1N/A newXS("${Package}::()", XS_${Packid}_nil, file$proto);
1N/AMAKE_FETCHMETHOD_WORK
1N/A}
1N/A
1N/A# print initialization routine
1N/A
1N/Aprint Q<<"EOF";
1N/A##ifdef __cplusplus
1N/A#extern "C"
1N/A##endif
1N/AEOF
1N/A
1N/Aprint Q<<"EOF";
1N/A#XS(boot_$Module_cname); /* prototype to pass -Wmissing-prototypes */
1N/A#XS(boot_$Module_cname)
1N/AEOF
1N/A
1N/Aprint Q<<"EOF";
1N/A#[[
1N/A# dXSARGS;
1N/AEOF
1N/A
1N/A#-Wall: if there is no $Full_func_name there are no xsubs in this .xs
1N/A#so `file' is unused
1N/Aprint Q<<"EOF" if $Full_func_name;
1N/A# char* file = __FILE__;
1N/AEOF
1N/A
1N/Aprint Q "#\n";
1N/A
1N/Aprint Q<<"EOF" if $WantVersionChk ;
1N/A# XS_VERSION_BOOTCHECK ;
1N/A#
1N/AEOF
1N/A
1N/Aprint Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
1N/A# {
1N/A# CV * cv ;
1N/A#
1N/AEOF
1N/A
1N/Aprint Q<<"EOF" if ($Overload);
1N/A# /* register the overloading (type 'A') magic */
1N/A# PL_amagic_generation++;
1N/A# /* The magic for overload gets a GV* via gv_fetchmeth as */
1N/A# /* mentioned above, and looks in the SV* slot of it for */
1N/A# /* the "fallback" status. */
1N/A# sv_setsv(
1N/A# get_sv( "${Package}::()", TRUE ),
1N/A# $Fallback
1N/A# );
1N/AEOF
1N/A
1N/Aprint @InitFileCode;
1N/A
1N/Aprint Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
1N/A# }
1N/AEOF
1N/A
1N/Aif (@BootCode)
1N/A{
1N/A print "\n /* Initialisation Section */\n\n" ;
1N/A @line = @BootCode;
1N/A print_section();
1N/A print "\n /* End of Initialisation Section */\n\n" ;
1N/A}
1N/A
1N/Aprint Q<<"EOF";;
1N/A# XSRETURN_YES;
1N/A#]]
1N/A#
1N/AEOF
1N/A
1N/Awarn("Please specify prototyping behavior for $filename (see perlxs manual)\n")
1N/A unless $ProtoUsed ;
1N/A&Exit;
1N/A
1N/Asub output_init {
1N/A local($type, $num, $var, $init, $name_printed) = @_;
1N/A local($arg) = "ST(" . ($num - 1) . ")";
1N/A
1N/A if( $init =~ /^=/ ) {
1N/A if ($name_printed) {
1N/A eval qq/print " $init\\n"/;
1N/A } else {
1N/A eval qq/print "\\t$var $init\\n"/;
1N/A }
1N/A warn $@ if $@;
1N/A } else {
1N/A if( $init =~ s/^\+// && $num ) {
1N/A &generate_init($type, $num, $var, $name_printed);
1N/A } elsif ($name_printed) {
1N/A print ";\n";
1N/A $init =~ s/^;//;
1N/A } else {
1N/A eval qq/print "\\t$var;\\n"/;
1N/A warn $@ if $@;
1N/A $init =~ s/^;//;
1N/A }
1N/A $deferred .= eval qq/"\\n\\t$init\\n"/;
1N/A warn $@ if $@;
1N/A }
1N/A}
1N/A
1N/Asub Warn
1N/A{
1N/A # work out the line number
1N/A my $line_no = $line_no[@line_no - @line -1] ;
1N/A
1N/A print STDERR "@_ in $filename, line $line_no\n" ;
1N/A}
1N/A
1N/Asub blurt
1N/A{
1N/A Warn @_ ;
1N/A $errors ++
1N/A}
1N/A
1N/Asub death
1N/A{
1N/A Warn @_ ;
1N/A exit 1 ;
1N/A}
1N/A
1N/Asub generate_init {
1N/A local($type, $num, $var) = @_;
1N/A local($arg) = "ST(" . ($num - 1) . ")";
1N/A local($argoff) = $num - 1;
1N/A local($ntype);
1N/A local($tk);
1N/A
1N/A $type = TidyType($type) ;
1N/A blurt("Error: '$type' not in typemap"), return
1N/A unless defined($type_kind{$type});
1N/A
1N/A ($ntype = $type) =~ s/\s*\*/Ptr/g;
1N/A ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1N/A $tk = $type_kind{$type};
1N/A $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
1N/A if ($tk eq 'T_PV' and exists $lengthof{$var}) {
1N/A print "\t$var" unless $name_printed;
1N/A print " = ($type)SvPV($arg, STRLEN_length_of_$var);\n";
1N/A die "default value not supported with length(NAME) supplied"
1N/A if defined $defaults{$var};
1N/A return;
1N/A }
1N/A $type =~ tr/:/_/ unless $hiertype;
1N/A blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
1N/A unless defined $input_expr{$tk} ;
1N/A $expr = $input_expr{$tk};
1N/A if ($expr =~ /DO_ARRAY_ELEM/) {
1N/A blurt("Error: '$subtype' not in typemap"), return
1N/A unless defined($type_kind{$subtype});
1N/A blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
1N/A unless defined $input_expr{$type_kind{$subtype}} ;
1N/A $subexpr = $input_expr{$type_kind{$subtype}};
1N/A $subexpr =~ s/\$type/\$subtype/g;
1N/A $subexpr =~ s/ntype/subtype/g;
1N/A $subexpr =~ s/\$arg/ST(ix_$var)/g;
1N/A $subexpr =~ s/\n\t/\n\t\t/g;
1N/A $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
1N/A $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/;
1N/A $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
1N/A }
1N/A if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
1N/A $ScopeThisXSUB = 1;
1N/A }
1N/A if (defined($defaults{$var})) {
1N/A $expr =~ s/(\t+)/$1 /g;
1N/A $expr =~ s/ /\t/g;
1N/A if ($name_printed) {
1N/A print ";\n";
1N/A } else {
1N/A eval qq/print "\\t$var;\\n"/;
1N/A warn $@ if $@;
1N/A }
1N/A if ($defaults{$var} eq 'NO_INIT') {
1N/A $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
1N/A } else {
1N/A $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
1N/A }
1N/A warn $@ if $@;
1N/A } elsif ($ScopeThisXSUB or $expr !~ /^\s*\$var =/) {
1N/A if ($name_printed) {
1N/A print ";\n";
1N/A } else {
1N/A eval qq/print "\\t$var;\\n"/;
1N/A warn $@ if $@;
1N/A }
1N/A $deferred .= eval qq/"\\n$expr;\\n"/;
1N/A warn $@ if $@;
1N/A } else {
1N/A die "panic: do not know how to handle this branch for function pointers"
1N/A if $name_printed;
1N/A eval qq/print "$expr;\\n"/;
1N/A warn $@ if $@;
1N/A }
1N/A}
1N/A
1N/Asub generate_output {
1N/A local($type, $num, $var, $do_setmagic, $do_push) = @_;
1N/A local($arg) = "ST(" . ($num - ($num != 0)) . ")";
1N/A local($argoff) = $num - 1;
1N/A local($ntype);
1N/A
1N/A $type = TidyType($type) ;
1N/A if ($type =~ /^array\(([^,]*),(.*)\)/) {
1N/A print "\t$arg = sv_newmortal();\n";
1N/A print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n";
1N/A print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1N/A } else {
1N/A blurt("Error: '$type' not in typemap"), return
1N/A unless defined($type_kind{$type});
1N/A blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
1N/A unless defined $output_expr{$type_kind{$type}} ;
1N/A ($ntype = $type) =~ s/\s*\*/Ptr/g;
1N/A $ntype =~ s/\(\)//g;
1N/A ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1N/A $expr = $output_expr{$type_kind{$type}};
1N/A if ($expr =~ /DO_ARRAY_ELEM/) {
1N/A blurt("Error: '$subtype' not in typemap"), return
1N/A unless defined($type_kind{$subtype});
1N/A blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
1N/A unless defined $output_expr{$type_kind{$subtype}} ;
1N/A $subexpr = $output_expr{$type_kind{$subtype}};
1N/A $subexpr =~ s/ntype/subtype/g;
1N/A $subexpr =~ s/\$arg/ST(ix_$var)/g;
1N/A $subexpr =~ s/\$var/${var}[ix_$var]/g;
1N/A $subexpr =~ s/\n\t/\n\t\t/g;
1N/A $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
1N/A eval "print qq\a$expr\a";
1N/A warn $@ if $@;
1N/A print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic;
1N/A }
1N/A elsif ($var eq 'RETVAL') {
1N/A if ($expr =~ /^\t\$arg = new/) {
1N/A # We expect that $arg has refcnt 1, so we need to
1N/A # mortalize it.
1N/A eval "print qq\a$expr\a";
1N/A warn $@ if $@;
1N/A print "\tsv_2mortal(ST($num));\n";
1N/A print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic;
1N/A }
1N/A elsif ($expr =~ /^\s*\$arg\s*=/) {
1N/A # We expect that $arg has refcnt >=1, so we need
1N/A # to mortalize it!
1N/A eval "print qq\a$expr\a";
1N/A warn $@ if $@;
1N/A print "\tsv_2mortal(ST(0));\n";
1N/A print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic;
1N/A }
1N/A else {
1N/A # Just hope that the entry would safely write it
1N/A # over an already mortalized value. By
1N/A # coincidence, something like $arg = &sv_undef
1N/A # works too.
1N/A print "\tST(0) = sv_newmortal();\n";
1N/A eval "print qq\a$expr\a";
1N/A warn $@ if $@;
1N/A # new mortals don't have set magic
1N/A }
1N/A }
1N/A elsif ($do_push) {
1N/A print "\tPUSHs(sv_newmortal());\n";
1N/A $arg = "ST($num)";
1N/A eval "print qq\a$expr\a";
1N/A warn $@ if $@;
1N/A print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1N/A }
1N/A elsif ($arg =~ /^ST\(\d+\)$/) {
1N/A eval "print qq\a$expr\a";
1N/A warn $@ if $@;
1N/A print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub map_type {
1N/A my($type, $varname) = @_;
1N/A
1N/A # C++ has :: in types too so skip this
1N/A $type =~ tr/:/_/ unless $hiertype;
1N/A $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s;
1N/A if ($varname) {
1N/A if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) {
1N/A (substr $type, pos $type, 0) = " $varname ";
1N/A } else {
1N/A $type .= "\t$varname";
1N/A }
1N/A }
1N/A $type;
1N/A}
1N/A
1N/A
1N/Asub Exit {
1N/A# If this is VMS, the exit status has meaning to the shell, so we
1N/A# use a predictable value (SS$_Normal or SS$_Abort) rather than an
1N/A# arbitrary number.
1N/A# exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ;
1N/A exit ($errors ? 1 : 0);
1N/A}