Pod typos, pod2man bugs, and miscellaneous installation comments
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / xsubpp
index 8d8e6dc..f2f10d7 100755 (executable)
@@ -76,8 +76,9 @@ perl(1), perlxs(1), perlxstut(1), perlapi(1)
 =cut
 
 # Global Constants
-$XSUBPP_version = "1.933";
+$XSUBPP_version = "1.938";
 require 5.002;
+use vars '$cplusplus';
 
 sub Q ;
 
@@ -124,6 +125,9 @@ else                                 { $Is_VMS = 0; chomp($pwd = `pwd`);   }
 
 ++ $IncludedFiles{$ARGV[0]} ;
 
+my(@XSStack) = ({type => 'none'});     # Stack of conditionals and INCLUDEs
+my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA");
+
 sub TrimWhitespace
 {
     $_[0] =~ s/^\s+|\s+$//go ;
@@ -179,7 +183,7 @@ foreach $typemap (@tm) {
             $type = TidyType($type) ;
            $type_kind{$type} = $kind ;
             # prototype defaults to '$'
-            $proto = '$' unless $proto ;
+            $proto = "\$" unless $proto ;
             warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n") 
                 unless ValidProtoString($proto) ;
             $proto_letter{$type} = C_string($proto) ;
@@ -211,6 +215,7 @@ $END = "!End!\n\n";         # "impossible" keyword (multiple newline)
 $BLOCK_re= '\s*(' . join('|', qw(
        REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT 
        CLEANUP ALIAS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE
+       SCOPE
        )) . "|$END)\\s*:";
 
 # Input:  ($_, @line) == unparsed input.
@@ -346,13 +351,14 @@ sub GetAliases
         
         # check for duplicate alias name & duplicate value
        Warn("Warning: Ignoring duplicate alias '$orig_alias'")
-           if defined $XsubAliases{$pname}{$alias} ;
+           if defined $XsubAliases{$alias} ;
 
-        Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$pname}{$value}' have identical values")
-           if $XsubAliasValues{$pname}{$value} ;
+       Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values")
+           if $XsubAliasValues{$value} ;
 
-        $XsubAliases{$pname}{$alias} = $value ;
-        $XsubAliasValues{$pname}{$value} = $orig_alias ;
+       $XsubAliases = 1;
+       $XsubAliases{$alias} = $value ;
+       $XsubAliasValues{$value} = $orig_alias ;
     }
 
     blurt("Error: Cannot parse ALIAS definitions from '$orig'")
@@ -435,6 +441,24 @@ sub PROTOTYPE_handler ()
 
 }
 
+sub SCOPE_handler ()
+{
+    death("Error: Only 1 SCOPE declaration allowed per xsub") 
+        if $scope_in_this_xsub ++ ;
+
+    for (;  !/^$BLOCK_re/o;  $_ = shift(@line)) {
+               next unless /\S/;
+               TrimWhitespace($_) ;
+        if ($_ =~ /^DISABLE/i) {
+                  $ScopeThisXSUB = 0 
+        }
+        elsif ($_ =~ /^ENABLE/i) {
+                  $ScopeThisXSUB = 1 
+        }
+    }
+
+}
+
 sub PROTOTYPES_handler ()
 {
     # the rest of the current line should contain either ENABLE or
@@ -471,7 +495,8 @@ sub INCLUDE_handler ()
     ++ $IncludedFiles{$_} unless /\|\s*$/ ;
 
     # Save the current file context.
-    push(@FileStack, {
+    push(@XSStack, {
+       type            => 'file',
         LastLine        => $lastline,
         LastLineNo      => $lastline_no,
         Line            => \@line,
@@ -508,9 +533,9 @@ EOF
  
 sub PopFile()
 {
-    return 0 unless @FileStack ;
-    my $data     = pop @FileStack ;
+    return 0 unless $XSStack[-1]{type} eq 'file' ;
+
+    my $data     = pop @XSStack ;
     my $ThisFile = $filename ;
     my $isPipe   = ($filename =~ /\|\s*$/) ;
  
@@ -525,7 +550,7 @@ sub PopFile()
     $lastline_no = $data->{LastLineNo} ;
     @line       = @{ $data->{Line} } ;
     @line_no    = @{ $data->{LineNo} } ;
+
     if ($isPipe and $? ) {
         -- $lastline_no ;
         print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n"  ;
@@ -564,7 +589,7 @@ sub ProtoString ($)
 {
     my ($type) = @_ ;
 
-    $proto_letter{$type} or '$' ;
+    $proto_letter{$type} or "\$" ;
 }
 
 sub check_cpp {
@@ -576,6 +601,8 @@ sub check_cpp {
                $cpplevel++;
            } elsif (!$cpplevel) {
                Warn("Warning: #else/elif/endif without #if in this function");
+               print STDERR "    (precede it with a blank line if the matching #if is outside the function)\n"
+                   if $XSStack[-1]{type} eq 'if';
                return;
            } elsif ($cpp =~ /^\#\s*endif/) {
                $cpplevel--;
@@ -600,7 +627,7 @@ open($FH, $filename) or die "cannot open $filename: $!\n";
 print <<EOM ;
 /*
  * This file was generated automatically by xsubpp version $XSUBPP_version from the 
- * contents of $filename. Don't edit this file, edit $filename instead.
+ * contents of $filename. Do not edit this file, edit $filename instead.
  *
  *     ANY CHANGES MADE HERE WILL BE LOST! 
  *
@@ -623,12 +650,11 @@ $lastline_no = $.;
 # Read next xsub into @line from ($lastline, <$FH>).
 sub fetch_para {
     # parse paragraph
+    death ("Error: Unterminated `#if/#ifdef/#ifndef'")
+       if !defined $lastline && $XSStack[-1]{type} eq 'if';
     @line = ();
     @line_no = () ;
-    if (! defined $lastline) {
-        return 1 if PopFile() ;
-        return 0 ;
-    }
+    return PopFile() if !defined $lastline;
 
     if ($lastline =~
        /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
@@ -644,7 +670,13 @@ sub fetch_para {
 
     for(;;) {
        if ($lastline !~ /^\s*#/ ||
-           $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|else|elif|endif|define|undef|pragma)\b|include\s*["<].*[>"])/) {
+           # CPP directives:
+           #   ANSI:   if ifdef ifndef elif else endif define undef
+           #           line error pragma
+           #   gcc:    warning include_next
+           #   obj-c:  import
+           #   others: ident (gcc notes that some cpps have this one)
+           $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
            last if $lastline =~ /^\S/ && @line && $line[-1] eq "";
            push(@line, $lastline);
            push(@line_no, $lastline_no) ;
@@ -667,11 +699,48 @@ sub fetch_para {
 PARAGRAPH:
 while (fetch_para()) {
     # Print initial preprocessor statements and blank lines
-    print shift(@line), "\n"
-       while @line && $line[0] !~ /^[^\#]/;
+    while (@line && $line[0] !~ /^[^\#]/) {
+       my $line = shift(@line);
+       print $line, "\n";
+       next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
+       my $statement = $+;
+       if ($statement eq 'if') {
+           $XSS_work_idx = @XSStack;
+           push(@XSStack, {type => 'if'});
+       } else {
+           death ("Error: `$statement' with no matching `if'")
+               if $XSStack[-1]{type} ne 'if';
+           if ($XSStack[-1]{varname}) {
+               push(@InitFileCode, "#endif\n");
+               push(@BootCode,     "#endif");
+           }
+
+           my(@fns) = keys %{$XSStack[-1]{functions}};
+           if ($statement ne 'endif') {
+               # Hide the functions defined in other #if branches, and reset.
+               @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns;
+               @{$XSStack[-1]}{qw(varname functions)} = ('', {});
+           } else {
+               my($tmp) = pop(@XSStack);
+               0 while (--$XSS_work_idx
+                        && $XSStack[$XSS_work_idx]{type} ne 'if');
+               # Keep all new defined functions
+               push(@fns, keys %{$tmp->{other_functions}});
+               @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
+           }
+       }
+    }
 
     next PARAGRAPH unless @line;
 
+    if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) {
+       # We are inside an #if, but have not yet #defined its xsubpp variable.
+       print "#define $cpp_next_tmp 1\n\n";
+       push(@InitFileCode, "#if $cpp_next_tmp\n");
+       push(@BootCode,     "#if $cpp_next_tmp");
+       $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++;
+    }
+
     death ("Code is not inside a function")
        if $line[0] =~ /^\s/;
 
@@ -687,7 +756,9 @@ while (fetch_para()) {
     undef(%arg_list) ;
     undef(@proto_arg) ;
     undef($proto_in_this_xsub) ;
+    undef($scope_in_this_xsub) ;
     $ProtoThisXSUB = $WantPrototypes ;
+    $ScopeThisXSUB = 0;
 
     $_ = shift(@line);
     while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) {
@@ -718,16 +789,16 @@ while (fetch_para()) {
 
     ($class, $func_name, $orig_args) =  ($1, $2, $3) ;
     ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/;
+    $Full_func_name = "${Packid}_$func_name";
 
     # Check for duplicate function definition
-    if (defined $Func_name{"${Packid}_$func_name"} ) {
-       Warn("Warning: duplicate function definition '$func_name' detected") 
+    for $tmp (@XSStack) {
+       next unless defined $tmp->{functions}{$Full_func_name};
+       Warn("Warning: duplicate function definition '$func_name' detected");
+       last;
     }
-    else {
-        push(@Func_name, "${Packid}_$func_name");
-        push(@Func_pname, $pname);
-    }
-    $Func_name{"${Packid}_$func_name"} ++ ;
+    $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ;
+    %XsubAliases = %XsubAliasValues = ();
 
     @args = split(/\s*,\s*/, $orig_args);
     if (defined($class)) {
@@ -752,7 +823,7 @@ while (fetch_para()) {
                    $defaults{$args[$i]} = $2;
                    $defaults{$args[$i]} =~ s/"/\\"/g;
            }
-           $proto_arg[$i+1] = '$' ;
+           $proto_arg[$i+1] = "\$" ;
     }
     if (defined($class)) {
            $func_args = join(", ", @args[1..$#args]);
@@ -762,6 +833,7 @@ while (fetch_para()) {
     @args_match{@args} = 1..@args;
 
     $PPCODE = grep(/^\s*PPCODE\s*:/, @line);
+    $CODE = grep(/^\s*CODE\s*:/, @line);
     $ALIAS  = grep(/^\s*ALIAS\s*:/,  @line);
 
     # print function header
@@ -825,8 +897,13 @@ EOF
         $gotRETVAL = 0;
 
        INPUT_handler() ;
-       process_keyword("INPUT|PREINIT|ALIAS|PROTOTYPE") ;
+       process_keyword("INPUT|PREINIT|ALIAS|PROTOTYPE|SCOPE") ;
 
+       print Q<<"EOF" if $ScopeThisXSUB;
+#   ENTER;
+#   [[
+EOF
+       
        if (!$thisdone && defined($class)) {
            if (defined($static) or $func_name =~ /^new/) {
                print "\tchar *";
@@ -851,12 +928,15 @@ EOF
                        $args_match{"RETVAL"} = 0;
                        $var_types{"RETVAL"} = $ret_type;
                }
+
                print $deferred;
-                process_keyword("INIT|ALIAS|PROTOTYPE") ;
+
+        process_keyword("INIT|ALIAS|PROTOTYPE") ;
 
                if (check_keyword("PPCODE")) {
                        print_section();
                        death ("PPCODE must be last thing") if @line;
+                       print "\tLEAVE;\n" if $ScopeThisXSUB;
                        print "\tPUTBACK;\n\treturn;\n";
                } elsif (check_keyword("CODE")) {
                        print_section() ;
@@ -904,6 +984,13 @@ EOF
        # do cleanup
        process_keyword("CLEANUP|ALIAS|PROTOTYPE") ;
 
+       print Q<<"EOF" if $ScopeThisXSUB;
+#   ]]
+EOF
+       print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE;
+#   LEAVE;
+EOF
+
        # print function trailer
        print Q<<EOF;
 #    ]]
@@ -929,24 +1016,35 @@ EOF
 #      croak(errbuf);
 EOF
 
-    print Q<<EOF unless $PPCODE;
+    if ($ret_type ne "void" or $CODE) {
+        print Q<<EOF unless $PPCODE;
 #    XSRETURN(1);
 EOF
+    } else {
+        print Q<<EOF unless $PPCODE;
+#    XSRETURN_EMPTY;
+EOF
+    }
 
     print Q<<EOF;
 #]]
 #
 EOF
 
+    my $newXS = "newXS" ;
+    my $proto = "" ;
+
     # Build the prototype string for the xsub
     if ($ProtoThisXSUB) {
-        if ($ProtoThisXSUB == 2) {
-            # User has specified empty prototype
-            $ProtoXSUB{$pname} = '""'
-        }
+       $newXS = "newXSproto";
+
+       if ($ProtoThisXSUB == 2) {
+           # User has specified empty prototype
+           $proto = ', ""' ;
+       }
         elsif ($ProtoThisXSUB != 1) {
             # User has specified a prototype
-            $ProtoXSUB{$pname} = '"' . $ProtoThisXSUB . '"'
+            $proto = ', "' . $ProtoThisXSUB . '"';
         }
         else {
            my $s = ';';
@@ -954,13 +1052,30 @@ EOF
                 $s = ''; 
                $proto_arg[$min_args] .= ";" ;
            }
-            push @proto_arg, "${s}@" 
+            push @proto_arg, "$s\@" 
                 if $elipsis ;
     
-            $ProtoXSUB{$pname} = '"' . join ("", @proto_arg) . '"' 
+            $proto = ', "' . join ("", @proto_arg) . '"';
         }
     }
 
+    if (%XsubAliases) {
+       $XsubAliases{$pname} = 0 
+           unless defined $XsubAliases{$pname} ;
+       while ( ($name, $value) = each %XsubAliases) {
+           push(@InitFileCode, Q<<"EOF");
+#        cv = newXS(\"$name\", XS_$Full_func_name, file);
+#        XSANY.any_i32 = $value ;
+EOF
+       push(@InitFileCode, Q<<"EOF") if $proto;
+#        sv_setpv((SV*)cv$proto) ;
+EOF
+        }
+    }
+    else {
+       push(@InitFileCode,
+            "        ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n");
+    }
 }
 
 # print initialization routine
@@ -980,41 +1095,15 @@ print Q<<"EOF" if $WantVersionChk ;
 #
 EOF
 
-print Q<<"EOF" if defined %XsubAliases ;
+print Q<<"EOF" if defined $XsubAliases ;
 #    {
 #        CV * cv ;
 #
 EOF
 
-for (@Func_name) {
-    $pname = shift(@Func_pname);
-    my $newXS = "newXS" ;
-    my $proto = "" ;
-
-    if ($ProtoXSUB{$pname}) {
-        $newXS = "newXSproto" ;
-        $proto = ", $ProtoXSUB{$pname}" ;
-    }
+print @InitFileCode;
 
-    if ($XsubAliases{$pname}) {
-        $XsubAliases{$pname}{$pname} = 0 
-               unless defined $XsubAliases{$pname}{$pname} ;
-        while ( ($name, $value) = each %{$XsubAliases{$pname}}) {
-            print Q<<"EOF" ;
-#        cv = newXS(\"$name\", XS_$_, file);
-#        XSANY.any_i32 = $value ;
-EOF
-            print Q<<"EOF" if $proto ;
-#        sv_setpv((SV*)cv, $ProtoXSUB{$pname}) ;
-EOF
-        }
-    }
-    else {
-        print "        ${newXS}(\"$pname\", XS_$_, file$proto);\n";
-    }
-}
-
-print Q<<"EOF" if defined %XsubAliases ;
+print Q<<"EOF" if defined $XsubAliases ;
 #    }
 EOF
 
@@ -1091,16 +1180,19 @@ sub generate_init {
        $subexpr =~ s/ntype/subtype/g;
        $subexpr =~ s/\$arg/ST(ix_$var)/g;
        $subexpr =~ s/\n\t/\n\t\t/g;
-       $subexpr =~ s/is not of (.*")/[arg %d] is not of $1, ix_$var + 1/g;
+       $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
        $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/;
        $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
     }
+    if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
+       $ScopeThisXSUB = 1;
+    }
     if (defined($defaults{$var})) {
            $expr =~ s/(\t+)/$1    /g;
            $expr =~ s/        /\t/g;
            eval qq/print "\\t$var;\\n"/;
            $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t    $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
-    } elsif ($expr !~ /^\t\$var =/) {
+    } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) {
            eval qq/print "\\t$var;\\n"/;
            $deferred .= eval qq/"\\n$expr;\\n"/;
     } else {
@@ -1140,7 +1232,11 @@ sub generate_output {
                eval "print qq\a$expr\a";
            }
            elsif ($var eq 'RETVAL') {
-               if ($expr =~ /^\t\$arg = /) {
+               if ($expr =~ /^\t\$arg\s*=\s*\$var\s*;/) {
+                   eval "print qq\a$expr\a";
+                   print "\tif (SvREFCNT(ST(0))) sv_2mortal(ST(0));\n";
+               } 
+               elsif ($expr =~ /^\t\$arg = /) {
                    eval "print qq\a$expr\a";
                    print "\tsv_2mortal(ST(0));\n";
                }