This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the build reproducible.
[perl5.git] / dist / ExtUtils-ParseXS / lib / ExtUtils / ParseXS.pm
index 08d6ac1..0213e3b 100644 (file)
@@ -1,31 +1,30 @@
 package ExtUtils::ParseXS;
 use strict;
 
-use 5.008001;  # We use /??{}/ in regexes
+use 5.006001;
 use Cwd;
 use Config;
-use Exporter;
+use Exporter 'import';
 use File::Basename;
 use File::Spec;
 use Symbol;
 
 our $VERSION;
 BEGIN {
-  $VERSION = '3.04_01';
+  $VERSION = '3.33';
 }
 use ExtUtils::ParseXS::Constants $VERSION;
 use ExtUtils::ParseXS::CountLines $VERSION;
 use ExtUtils::ParseXS::Utilities $VERSION;
+use ExtUtils::ParseXS::Eval $VERSION;
 $VERSION = eval $VERSION if $VERSION =~ /_/;
 
 use ExtUtils::ParseXS::Utilities qw(
   standard_typemap_locations
   trim_whitespace
-  tidy_type
   C_string
   valid_proto_string
   process_typemaps
-  make_targetable
   map_type
   standard_XS_defs
   assign_func_args
@@ -36,28 +35,57 @@ use ExtUtils::ParseXS::Utilities qw(
   blurt
   death
   check_conditional_preprocessor_statements
+  escape_file_for_line_directive
+  report_typemap_failure
 );
 
-our @ISA = qw(Exporter);
 our @EXPORT_OK = qw(
   process_file
   report_error_count
 );
 
-# The scalars in the line below remain as 'our' variables because pulling
-# them into $self led to build problems.  In most cases, strings being
-# 'eval'-ed contain the variables' names hard-coded.
-our (
-  $Package, $func_name, $Full_func_name, $pname, $ALIAS,
-);
+##############################
+# A number of "constants"
 
-our $self = bless {} => __PACKAGE__;
+our ($C_group_rex, $C_arg);
+# Group in C (no support for comments or literals)
+$C_group_rex = qr/ [({\[]
+             (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
+             [)}\]] /x;
+# Chunk in C without comma at toplevel (no comments):
+$C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
+       |   (??{ $C_group_rex })
+       |   " (?: (?> [^\\"]+ )
+         |   \\.
+         )* "        # String literal
+              |   ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
+       )* /xs;
 
-sub process_file {
+# "impossible" keyword (multiple newline)
+my $END = "!End!\n\n";
+# Match an XS Keyword
+my $BLOCK_regexp = '\s*(' . $ExtUtils::ParseXS::Constants::XSKeywordsAlternation . "|$END)\\s*:";
+
+
+
+sub new {
+  return bless {} => shift;
+}
+
+our $Singleton = __PACKAGE__->new;
 
-  # Allow for $package->process_file(%hash) in the future
-  my ($pkg, %options) = @_ % 2 ? @_ : (__PACKAGE__, @_);
+sub process_file {
+  my $self;
+  # Allow for $package->process_file(%hash), $obj->process_file, and process_file()
+  if (@_ % 2) {
+    my $invocant = shift;
+    $self = ref($invocant) ? $invocant : $invocant->new;
+  }
+  else {
+    $self = $Singleton;
+  }
 
+  my %options = @_;
   $self->{ProtoUsed} = exists $options{prototypes};
 
   # Set defaults.
@@ -90,15 +118,15 @@ sub process_file {
   }
   @{ $self->{XSStack} } = ({type => 'none'});
   $self->{InitFileCode} = [ @ExtUtils::ParseXS::Constants::InitFileCode ];
-  $self->{Overload}     = 0;
-  $self->{errors}       = 0;
+  $self->{Overload}     = 0; # bool
+  $self->{errors}       = 0; # count
   $self->{Fallback}     = '&PL_sv_undef';
 
   # Most of the 1500 lines below uses these globals.  We'll have to
   # clean this up sometime, probably.  For now, we just pull them out
   # of %args.  -Ken
 
-  $self->{hiertype} = $args{hiertype};
+  $self->{RetainCplusplusHierarchicalTypes} = $args{hiertype};
   $self->{WantPrototypes} = $args{prototypes};
   $self->{WantVersionChk} = $args{versioncheck};
   $self->{WantLineNumbers} = $args{linenumbers};
@@ -146,39 +174,11 @@ sub process_file {
 
   $self->{typemap} = process_typemaps( $args{typemap}, $pwd );
 
-  my $END = "!End!\n\n";        # "impossible" keyword (multiple newline)
-
-  # Match an XS keyword
-  $self->{BLOCK_re} = '\s*(' .
-    join('|' => @ExtUtils::ParseXS::Constants::XSKeywords) .
-    "|$END)\\s*:";
-
-  our ($C_group_rex, $C_arg);
-  # Group in C (no support for comments or literals)
-  $C_group_rex = qr/ [({\[]
-               (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
-               [)}\]] /x;
-  # Chunk in C without comma at toplevel (no comments):
-  $C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
-         |   (??{ $C_group_rex })
-         |   " (?: (?> [^\\"]+ )
-           |   \\.
-           )* "        # String literal
-                |   ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
-         )* /xs;
-
-  # Since at this point we're ready to begin printing to the output file and
-  # reading from the input file, I want to get as much data as possible into
-  # the proto-object $self.  That means assigning to $self and elements of
-  # %args referenced below this point.
-  # HOWEVER:  This resulted in an error when I tried:
-  #   $args{'s'} ---> $self->{s}.
-  # Use of uninitialized value in quotemeta at
-  #   .../blib/lib/ExtUtils/ParseXS.pm line 733
-
+  # Move more settings from parameters to object
   foreach my $datum ( qw| argtypes except inout optimize | ) {
     $self->{$datum} = $args{$datum};
   }
+  $self->{strip_c_func_prefix} = $args{s};
 
   # Identify the version of xsubpp used
   print <<EOM;
@@ -193,14 +193,14 @@ sub process_file {
 EOM
 
 
-  print("#line 1 \"$self->{filepathname}\"\n")
+  print("#line 1 \"" . escape_file_for_line_directive($self->{filepathname}) . "\"\n")
     if $self->{WantLineNumbers};
 
   # Open the input file (using $self->{filename} which
   # is a basename'd $args{filename} due to chdir above)
   open($self->{FH}, '<', $self->{filename}) or die "cannot open $self->{filename}: $!\n";
 
-  firstmodule:
+  FIRSTMODULE:
   while (readline($self->{FH})) {
     if (/^=/) {
       my $podstartline = $.;
@@ -220,9 +220,9 @@ EOM
           # concatenated until 2 steps later, so we are safe.
           #     - Nicholas Clark
           print("#if 0\n  \"Skipped embedded POD.\"\n#endif\n");
-          printf("#line %d \"$self->{filepathname}\"\n", $. + 1)
+          printf("#line %d \"%s\"\n", $. + 1, escape_file_for_line_directive($self->{filepathname}))
             if $self->{WantLineNumbers};
-          next firstmodule
+          next FIRSTMODULE;
         }
 
       } while (readline($self->{FH}));
@@ -232,7 +232,7 @@ EOM
       die ("Error: Unterminated pod in $self->{filename}, line $podstartline\n")
         unless $self->{lastline};
     }
-    last if ($Package, $self->{Prefix}) =
+    last if ($self->{Package}, $self->{Prefix}) =
       /^MODULE\s*=\s*[\w:]+(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
 
     print $_;
@@ -292,14 +292,14 @@ EOM
       $self->{$member} = {};
     }
     $self->{proto_arg} = [];
-    $self->{processing_arg_with_types} = undef;
-    $self->{proto_in_this_xsub}        = undef;
-    $self->{scope_in_this_xsub}        = undef;
-    $self->{interface}                 = undef;
+    $self->{processing_arg_with_types} = 0; # bool
+    $self->{proto_in_this_xsub}        = 0; # counter & bool
+    $self->{scope_in_this_xsub}        = 0; # counter & bool
+    $self->{interface}                 = 0; # bool
     $self->{interface_macro}           = 'XSINTERFACE_FUNC';
     $self->{interface_macro_set}       = 'XSINTERFACE_FUNC_SET';
-    $self->{ProtoThisXSUB}             = $self->{WantPrototypes};
-    $self->{ScopeThisXSUB}             = 0;
+    $self->{ProtoThisXSUB}             = $self->{WantPrototypes}; # states 0 (none), 1 (yes), 2 (empty prototype)
+    $self->{ScopeThisXSUB}             = 0; # bool
 
     my $xsreturn = 0;
 
@@ -313,14 +313,15 @@ EOM
 
     if ($self->check_keyword("BOOT")) {
       check_conditional_preprocessor_statements($self);
-      push (@{ $BootCode_ref }, "#line $self->{line_no}->[@{ $self->{line_no} } - @{ $self->{line} }] \"$self->{filepathname}\"")
+      push (@{ $BootCode_ref }, "#line $self->{line_no}->[@{ $self->{line_no} } - @{ $self->{line} }] \""
+                                . escape_file_for_line_directive($self->{filepathname}) . "\"")
         if $self->{WantLineNumbers} && $self->{line}->[0] !~ /^\s*#\s*line\b/;
       push (@{ $BootCode_ref }, @{ $self->{line} }, "");
       next PARAGRAPH;
     }
 
     # extract return type, function name and arguments
-    ($self->{ret_type}) = tidy_type($_);
+    ($self->{ret_type}) = ExtUtils::Typemaps::tidy_type($_);
     my $RETVAL_no_return = 1 if $self->{ret_type} =~ s/^NO_OUTPUT\s+//;
 
     # Allow one-line ANSI-like declaration
@@ -340,25 +341,25 @@ EOM
       unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
 
     my ($class, $orig_args);
-    ($class, $func_name, $orig_args) =  ($1, $2, $3);
+    ($class, $self->{func_name}, $orig_args) =  ($1, $2, $3);
     $class = "$4 $class" if $4;
-    ($pname = $func_name) =~ s/^($self->{Prefix})?/$self->{Packprefix}/;
+    ($self->{pname} = $self->{func_name}) =~ s/^($self->{Prefix})?/$self->{Packprefix}/;
     my $clean_func_name;
-    ($clean_func_name = $func_name) =~ s/^$self->{Prefix}//;
-    $Full_func_name = "$self->{Packid}_$clean_func_name";
+    ($clean_func_name = $self->{func_name}) =~ s/^$self->{Prefix}//;
+    $self->{Full_func_name} = "$self->{Packid}_$clean_func_name";
     if ($Is_VMS) {
-      $Full_func_name = $SymSet->addsym($Full_func_name);
+      $self->{Full_func_name} = $SymSet->addsym( $self->{Full_func_name} );
     }
 
     # Check for duplicate function definition
     for my $tmp (@{ $self->{XSStack} }) {
-      next unless defined $tmp->{functions}{$Full_func_name};
+      next unless defined $tmp->{functions}{ $self->{Full_func_name} };
       Warn( $self, "Warning: duplicate function definition '$clean_func_name' detected");
       last;
     }
-    $self->{XSStack}->[$XSS_work_idx]{functions}{$Full_func_name}++;
-    %{ $self->{XsubAliases} }     = ();
-    %{ $self->{XsubAliasValues} } = ();
+    $self->{XSStack}->[$XSS_work_idx]{functions}{ $self->{Full_func_name} }++;
+    delete $self->{XsubAliases};
+    delete $self->{XsubAliasValues};
     %{ $self->{Interfaces} }      = ();
     @{ $self->{Attributes} }      = ();
     $self->{DoSetMagic} = 1;
@@ -371,8 +372,10 @@ EOM
     my $only_C_inlist_ref = {};        # Not in the signature of Perl function
     if ($self->{argtypes} and $orig_args =~ /\S/) {
       my $args = "$orig_args ,";
+      use re 'eval';
       if ($args =~ /^( (??{ $C_arg }) , )* $ /x) {
         @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg);
+        no re 'eval';
         for ( @args ) {
           s/^\s+//;
           s/\s+$//;
@@ -393,7 +396,7 @@ EOM
           if ($len_name =~ /^length\( \s* (\w+) \s* \)\z/x) {
             $len_name = "XSauto_length_of_$1";
             $islength = 1;
-            die "Default value on length() argument: `$_'"
+            die "Default value on length() argument: '$_'"
               if length $default;
           }
           if (length $pre or $islength) { # Has a type
@@ -413,6 +416,7 @@ EOM
         }
       }
       else {
+        no re 'eval';
         @args = split(/\s*,\s*/, $orig_args);
         Warn( $self, "Warning: cannot parse argument list '$orig_args', fallback to split");
       }
@@ -432,7 +436,7 @@ EOM
       }
     }
     if (defined($class)) {
-      my $arg0 = ((defined($static) or $func_name eq 'new')
+      my $arg0 = ((defined($static) or $self->{func_name} eq 'new')
           ? "CLASS" : "THIS");
       unshift(@args, $arg0);
     }
@@ -478,20 +482,7 @@ EOM
     my $EXPLICIT_RETURN = ($CODE &&
             ("@{ $self->{line} }" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x ));
 
-    # The $ALIAS which follows is only explicitly called within the scope of
-    # process_file().  In principle, it ought to be a lexical, i.e., 'my
-    # $ALIAS' like the other nearby variables.  However, implementing that
-    # change produced a slight difference in the resulting .c output in at
-    # least two distributions:  B/BD/BDFOY/Crypt-Rijndael and
-    # G/GF/GFUJI/Hash-FieldHash.  The difference is, arguably, an improvement
-    # in the resulting C code.  Example:
-    # 388c388
-    # <                       GvNAME(CvGV(cv)),
-    # ---
-    # >                       "Crypt::Rijndael::encrypt",
-    # But at this point we're committed to generating the *same* C code that
-    # the current version of ParseXS.pm does.  So we're declaring it as 'our'.
-    $ALIAS  = grep(/^\s*ALIAS\s*:/,  @{ $self->{line} });
+    $self->{ALIAS}  = grep(/^\s*ALIAS\s*:/,  @{ $self->{line} });
 
     my $INTERFACE  = grep(/^\s*INTERFACE\s*:/,  @{ $self->{line} });
 
@@ -502,16 +493,12 @@ EOM
     # print function header
     print Q(<<"EOF");
 #$externC
-#XS_EUPXS(XS_${Full_func_name}); /* prototype to pass -Wmissing-prototypes */
-#XS_EUPXS(XS_${Full_func_name})
+#XS_EUPXS(XS_$self->{Full_func_name}); /* prototype to pass -Wmissing-prototypes */
+#XS_EUPXS(XS_$self->{Full_func_name})
 #[[
-##ifdef dVAR
 #    dVAR; dXSARGS;
-##else
-#    dXSARGS;
-##endif
 EOF
-    print Q(<<"EOF") if $ALIAS;
+    print Q(<<"EOF") if $self->{ALIAS};
 #    dXSI32;
 EOF
     print Q(<<"EOF") if $INTERFACE;
@@ -522,7 +509,7 @@ EOF
 
     print Q(<<"EOF") if $self->{except};
 #    char errbuf[1024];
-#    *errbuf = '\0';
+#    *errbuf = '\\0';
 EOF
 
     if($self->{cond}) {
@@ -540,7 +527,7 @@ EOF
 
     #gcc -Wall: if an xsub has PPCODE is used
     #it is possible none of ST, XSRETURN or XSprePUSH macros are used
-    #hence `ax' (setup by dXSARGS) is unused
+    #hence 'ax' (setup by dXSARGS) is unused
     #XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS
     #but such a move could break third-party extensions
     print Q(<<"EOF") if $PPCODE;
@@ -581,10 +568,10 @@ EOF
 EOF
 
       if (!$self->{thisdone} && defined($class)) {
-        if (defined($static) or $func_name eq 'new') {
+        if (defined($static) or $self->{func_name} eq 'new') {
           print "\tchar *";
           $self->{var_types}->{"CLASS"} = "char *";
-          generate_init( {
+          $self->generate_init( {
             type          => "char *",
             num           => 1,
             var           => "CLASS",
@@ -592,9 +579,9 @@ EOF
           } );
         }
         else {
-          print "\t$class *";
+          print "\t" . map_type($self, "$class *");
           $self->{var_types}->{"THIS"} = "$class *";
-          generate_init( {
+          $self->generate_init( {
             type          => "$class *",
             num           => 1,
             var           => "THIS",
@@ -609,7 +596,7 @@ EOF
       my ($wantRETVAL);
       # do code
       if (/^\s*NOT_IMPLEMENTED_YET/) {
-        print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n";
+        print "\n\tPerl_croak(aTHX_ \"$self->{pname}: not implemented yet\");\n";
         $_ = '';
       }
       else {
@@ -645,7 +632,7 @@ EOF
             $self->{have_CODE_with_RETVAL} = 1;
           }
         }
-        elsif (defined($class) and $func_name eq "DESTROY") {
+        elsif (defined($class) and $self->{func_name} eq "DESTROY") {
           print "\n\t";
           print "delete THIS;\n";
         }
@@ -656,25 +643,26 @@ EOF
             $wantRETVAL = 1;
           }
           if (defined($static)) {
-            if ($func_name eq 'new') {
-              $func_name = "$class";
+            if ($self->{func_name} eq 'new') {
+              $self->{func_name} = "$class";
             }
             else {
               print "${class}::";
             }
           }
           elsif (defined($class)) {
-            if ($func_name eq 'new') {
-              $func_name .= " $class";
+            if ($self->{func_name} eq 'new') {
+              $self->{func_name} .= " $class";
             }
             else {
               print "THIS->";
             }
           }
-          $func_name =~ s/^\Q$args{'s'}//
-            if exists $args{'s'};
-          $func_name = 'XSFUNCTION' if $self->{interface};
-          print "$func_name($self->{func_args});\n";
+          my $strip = $self->{strip_c_func_prefix};
+          $self->{func_name} =~ s/^\Q$strip//
+            if defined $strip;
+          $self->{func_name} = 'XSFUNCTION' if $self->{interface};
+          print "$self->{func_name}($self->{func_args});\n";
         }
       }
 
@@ -692,13 +680,13 @@ EOF
         $self->Warn("Warning: Found a 'CODE' section which seems to be using 'RETVAL' but no 'OUTPUT' section.");
       }
 
-      generate_output( {
+      $self->generate_output( {
         type        => $self->{var_types}->{$_},
         num         => $self->{args_match}->{$_},
         var         => $_,
         do_setmagic => $self->{DoSetMagic},
         do_push     => undef,
-      } ) for grep $self->{in_out}->{$_} =~ /OUT$/, keys %{ $self->{in_out} };
+      } ) for grep $self->{in_out}->{$_} =~ /OUT$/, sort keys %{ $self->{in_out} };
 
       my $prepush_done;
       # all OUTPUT done, so now push the return value on the stack
@@ -707,40 +695,34 @@ EOF
       }
       elsif ($self->{gotRETVAL} || $wantRETVAL) {
         my $outputmap = $self->{typemap}->get_outputmap( ctype => $self->{ret_type} );
-        my $t = $self->{optimize} && $outputmap && $outputmap->targetable;
-        # Although the '$var' declared in the next line is never explicitly
-        # used within this 'elsif' block, commenting it out leads to
-        # disaster, starting with the first 'eval qq' inside the 'elsif' block
-        # below.
-        # It appears that this is related to the fact that at this point the
-        # value of $t is a reference to an array whose [2] element includes
-        # '$var' as a substring:
-        # <i> <> <(IV)$var>
+        my $trgt = $self->{optimize} && $outputmap && $outputmap->targetable;
         my $var = 'RETVAL';
         my $type = $self->{ret_type};
 
-        if ($t and not $t->{with_size} and $t->{type} eq 'p') {
-          # PUSHp corresponds to setpvn.  Treat setpv directly
-          my $what = eval qq("$t->{what}");
-          warn $@ if $@;
-
-          print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
-          $prepush_done = 1;
-        }
-        elsif ($t) {
-          my $what = eval qq("$t->{what}");
-          warn $@ if $@;
-
-          my $tsize = $t->{what_size};
-          $tsize = '' unless defined $tsize;
-          $tsize = eval qq("$tsize");
-          warn $@ if $@;
-          print "\tXSprePUSH; PUSH$t->{type}($what$tsize);\n";
-          $prepush_done = 1;
+        if ($trgt) {
+          my $what = $self->eval_output_typemap_code(
+            qq("$trgt->{what}"),
+            {var => $var, type => $self->{ret_type}}
+          );
+          if (not $trgt->{with_size} and $trgt->{type} eq 'p') { # sv_setpv
+            # PUSHp corresponds to sv_setpvn.  Treat sv_setpv directly
+            print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
+            $prepush_done = 1;
+          }
+          else {
+            my $tsize = $trgt->{what_size};
+            $tsize = '' unless defined $tsize;
+            $tsize = $self->eval_output_typemap_code(
+              qq("$tsize"),
+              {var => $var, type => $self->{ret_type}}
+            );
+            print "\tXSprePUSH; PUSH$trgt->{type}($what$tsize);\n";
+            $prepush_done = 1;
+          }
         }
         else {
           # RETVAL almost never needs SvSETMAGIC()
-          generate_output( {
+          $self->generate_output( {
             type        => $self->{ret_type},
             num         => 0,
             var         => 'RETVAL',
@@ -756,7 +738,7 @@ EOF
       print "\tXSprePUSH;" if $c and not $prepush_done;
       print "\tEXTEND(SP,$c);\n" if $c;
       $xsreturn += $c;
-      generate_output( {
+      $self->generate_output( {
         type        => $self->{var_types}->{$_},
         num         => $num++,
         var         => $_,
@@ -785,13 +767,13 @@ EOF
 #    ENDHANDLERS
 EOF
       if ($self->check_keyword("CASE")) {
-        $self->blurt("Error: No `CASE:' at top of function")
+        $self->blurt("Error: No 'CASE:' at top of function")
           unless $self->{condnum};
         $_ = "CASE: $_";    # Restore CASE: label
         next;
       }
       last if $_ eq "$END:";
-      $self->death(/^$self->{BLOCK_re}/o ? "Misplaced `$1:'" : "Junk at end of function ($_)");
+      $self->death(/^$BLOCK_regexp/o ? "Misplaced '$1:'" : "Junk at end of function ($_)");
     }
 
     print Q(<<"EOF") if $self->{except};
@@ -815,12 +797,15 @@ EOF
 #
 EOF
 
-    $self->{newXS} = "newXS";
     $self->{proto} = "";
-
+    unless($self->{ProtoThisXSUB}) {
+      $self->{newXS} = "newXS_deffile";
+      $self->{file} = "";
+    }
+    else {
     # Build the prototype string for the xsub
-    if ($self->{ProtoThisXSUB}) {
       $self->{newXS} = "newXSproto_portable";
+      $self->{file} = ", file";
 
       if ($self->{ProtoThisXSUB} eq 2) {
         # User has specified empty prototype
@@ -843,38 +828,40 @@ EOF
       $self->{proto} = qq{, "$self->{proto}"};
     }
 
-    if (%{ $self->{XsubAliases} }) {
-      $self->{XsubAliases}->{$pname} = 0
-        unless defined $self->{XsubAliases}->{$pname};
-      while ( my ($xname, $value) = each %{ $self->{XsubAliases} }) {
+    if ($self->{XsubAliases} and keys %{ $self->{XsubAliases} }) {
+      $self->{XsubAliases}->{ $self->{pname} } = 0
+        unless defined $self->{XsubAliases}->{ $self->{pname} };
+      foreach my $xname (sort keys %{ $self->{XsubAliases} }) {
+        my $value = $self->{XsubAliases}{$xname};
         push(@{ $self->{InitFileCode} }, Q(<<"EOF"));
-#        cv = $self->{newXS}(\"$xname\", XS_$Full_func_name, file$self->{proto});
+#        cv = $self->{newXS}(\"$xname\", XS_$self->{Full_func_name}$self->{file}$self->{proto});
 #        XSANY.any_i32 = $value;
 EOF
       }
     }
     elsif (@{ $self->{Attributes} }) {
       push(@{ $self->{InitFileCode} }, Q(<<"EOF"));
-#        cv = $self->{newXS}(\"$pname\", XS_$Full_func_name, file$self->{proto});
-#        apply_attrs_string("$Package", cv, "@{ $self->{Attributes} }", 0);
+#        cv = $self->{newXS}(\"$self->{pname}\", XS_$self->{Full_func_name}$self->{file}$self->{proto});
+#        apply_attrs_string("$self->{Package}", cv, "@{ $self->{Attributes} }", 0);
 EOF
     }
     elsif ($self->{interface}) {
-      while ( my ($yname, $value) = each %{ $self->{Interfaces} }) {
-        $yname = "$Package\::$yname" unless $yname =~ /::/;
+      foreach my $yname (sort keys %{ $self->{Interfaces} }) {
+        my $value = $self->{Interfaces}{$yname};
+        $yname = "$self->{Package}\::$yname" unless $yname =~ /::/;
         push(@{ $self->{InitFileCode} }, Q(<<"EOF"));
-#        cv = $self->{newXS}(\"$yname\", XS_$Full_func_name, file$self->{proto});
+#        cv = $self->{newXS}(\"$yname\", XS_$self->{Full_func_name}$self->{file}$self->{proto});
 #        $self->{interface_macro_set}(cv,$value);
 EOF
       }
     }
-    elsif($self->{newXS} eq 'newXS'){ # work around P5NCI's empty newXS macro
+    elsif($self->{newXS} eq 'newXS_deffile'){ # work around P5NCI's empty newXS macro
       push(@{ $self->{InitFileCode} },
-       "        $self->{newXS}(\"$pname\", XS_$Full_func_name, file$self->{proto});\n");
+       "        $self->{newXS}(\"$self->{pname}\", XS_$self->{Full_func_name}$self->{file}$self->{proto});\n");
     }
     else {
       push(@{ $self->{InitFileCode} },
-       "        (void)$self->{newXS}(\"$pname\", XS_$Full_func_name, file$self->{proto});\n");
+       "        (void)$self->{newXS}(\"$self->{pname}\", XS_$self->{Full_func_name}$self->{file}$self->{proto});\n");
     }
   } # END 'PARAGRAPH' 'while' loop
 
@@ -889,10 +876,10 @@ EOF
 #
 EOF
     unshift(@{ $self->{InitFileCode} }, <<"MAKE_FETCHMETHOD_WORK");
-    /* Making a sub named "${Package}::()" allows the package */
+    /* Making a sub named "$self->{Package}::()" allows the package */
     /* to be findable via fetchmethod(), and causes */
-    /* overload::Overloaded("${Package}") to return true. */
-    (void)$self->{newXS}("${Package}::()", XS_$self->{Packid}_nil, file$self->{proto});
+    /* overload::Overloaded("$self->{Package}") to return true. */
+    (void)$self->{newXS}("$self->{Package}::()", XS_$self->{Packid}_nil$self->{file}$self->{proto});
 MAKE_FETCHMETHOD_WORK
   }
 
@@ -907,28 +894,28 @@ EOF
   print Q(<<"EOF");
 #XS_EXTERNAL(boot_$self->{Module_cname}); /* prototype to pass -Wmissing-prototypes */
 #XS_EXTERNAL(boot_$self->{Module_cname})
-EOF
-
-  print Q(<<"EOF");
 #[[
-##ifdef dVAR
+##if PERL_VERSION_LE(5, 21, 5)
 #    dVAR; dXSARGS;
 ##else
-#    dXSARGS;
+#    dVAR; ${\($self->{WantVersionChk} ?
+     'dXSBOOTARGSXSAPIVERCHK;' : 'dXSBOOTARGSAPIVERCHK;')}
 ##endif
 EOF
 
   #Under 5.8.x and lower, newXS is declared in proto.h as expecting a non-const
   #file name argument. If the wrong qualifier is used, it causes breakage with
   #C++ compilers and warnings with recent gcc.
-  #-Wall: if there is no $Full_func_name there are no xsubs in this .xs
-  #so `file' is unused
-  print Q(<<"EOF") if $Full_func_name;
+  #-Wall: if there is no $self->{Full_func_name} there are no xsubs in this .xs
+  #so 'file' is unused
+  print Q(<<"EOF") if $self->{Full_func_name};
 ##if (PERL_REVISION == 5 && PERL_VERSION < 9)
 #    char* file = __FILE__;
 ##else
 #    const char* file = __FILE__;
 ##endif
+#
+#    PERL_UNUSED_VAR(file);
 EOF
 
   print Q("#\n");
@@ -936,17 +923,28 @@ EOF
   print Q(<<"EOF");
 #    PERL_UNUSED_VAR(cv); /* -W */
 #    PERL_UNUSED_VAR(items); /* -W */
-##ifdef XS_APIVERSION_BOOTCHECK
+EOF
+
+  if( $self->{WantVersionChk}){
+    print Q(<<"EOF") ;
+##if PERL_VERSION_LE(5, 21, 5)
+#    XS_VERSION_BOOTCHECK;
+##  ifdef XS_APIVERSION_BOOTCHECK
 #    XS_APIVERSION_BOOTCHECK;
+##  endif
 ##endif
+
 EOF
+  } else {
+    print Q(<<"EOF") ;
+##if PERL_VERSION_LE(5, 21, 5) && defined(XS_APIVERSION_BOOTCHECK)
+#  XS_APIVERSION_BOOTCHECK;
+##endif
 
-  print Q(<<"EOF") if $self->{WantVersionChk};
-#    XS_VERSION_BOOTCHECK;
-#
 EOF
+  }
 
-  print Q(<<"EOF") if defined $self->{xsubaliases} or defined $self->{interfaces};
+  print Q(<<"EOF") if defined $self->{XsubAliases} or defined $self->{interfaces};
 #    {
 #        CV * cv;
 #
@@ -954,19 +952,21 @@ EOF
 
   print Q(<<"EOF") if ($self->{Overload});
 #    /* register the overloading (type 'A') magic */
+##if (PERL_REVISION == 5 && PERL_VERSION < 9)
 #    PL_amagic_generation++;
+##endif
 #    /* The magic for overload gets a GV* via gv_fetchmeth as */
 #    /* mentioned above, and looks in the SV* slot of it for */
 #    /* the "fallback" status. */
 #    sv_setsv(
-#        get_sv( "${Package}::()", TRUE ),
+#        get_sv( "$self->{Package}::()", TRUE ),
 #        $self->{Fallback}
 #    );
 EOF
 
   print @{ $self->{InitFileCode} };
 
-  print Q(<<"EOF") if defined $self->{xsubaliases} or defined $self->{interfaces};
+  print Q(<<"EOF") if defined $self->{XsubAliases} or defined $self->{interfaces};
 #    }
 EOF
 
@@ -978,14 +978,15 @@ EOF
   }
 
   print Q(<<'EOF');
-##if (PERL_REVISION == 5 && PERL_VERSION >= 9)
-#  if (PL_unitcheckav)
-#       call_list(PL_scopestack_ix, PL_unitcheckav);
-##endif
-EOF
-
-  print Q(<<"EOF");
+##if PERL_VERSION_LE(5, 21, 5)
+##  if PERL_VERSION_GE(5, 9, 0)
+#    if (PL_unitcheckav)
+#        call_list(PL_scopestack_ix, PL_unitcheckav);
+##  endif
 #    XSRETURN_YES;
+##else
+#    Perl_xs_boot_epilog(aTHX_ ax);
+##endif
 #]]
 #
 EOF
@@ -1001,7 +1002,14 @@ EOF
   return 1;
 }
 
-sub report_error_count { $self->{errors} }
+sub report_error_count {
+  if (@_) {
+    return $_[0]->{errors}||0;
+  }
+  else {
+    return $Singleton->{errors}||0;
+  }
+}
 
 # Input:  ($self, $_, @{ $self->{line} }) == unparsed input.
 # Output: ($_, @{ $self->{line} }) == (rest of line, following lines).
@@ -1020,9 +1028,10 @@ sub print_section {
 
   my $consumed_code = '';
 
-  print("#line ", $self->{line_no}->[@{ $self->{line_no} } - @{ $self->{line} } -1], " \"$self->{filepathname}\"\n")
+  print("#line ", $self->{line_no}->[@{ $self->{line_no} } - @{ $self->{line} } -1], " \"",
+        escape_file_for_line_directive($self->{filepathname}), "\"\n")
     if $self->{WantLineNumbers} && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/;
-  for (;  defined($_) && !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  defined($_) && !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     print "$_\n";
     $consumed_code .= "$_\n";
   }
@@ -1039,7 +1048,7 @@ sub merge_section {
     $_ = shift(@{ $self->{line} });
   }
 
-  for (;  defined($_) && !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  defined($_) && !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     $in .= "$_\n";
   }
   chomp $in;
@@ -1058,7 +1067,7 @@ sub process_keyword {
 sub CASE_handler {
   my $self = shift;
   $_ = shift;
-  $self->blurt("Error: `CASE:' after unconditional `CASE:'")
+  $self->blurt("Error: 'CASE:' after unconditional 'CASE:'")
     if $self->{condnum} && $self->{cond} eq '';
   $self->{cond} = $_;
   trim_whitespace($self->{cond});
@@ -1069,7 +1078,7 @@ sub CASE_handler {
 sub INPUT_handler {
   my $self = shift;
   $_ = shift;
-  for (;  !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     last if /^\s*NOT_IMPLEMENTED_YET/;
     next unless /\S/;        # skip blank lines
 
@@ -1122,7 +1131,7 @@ sub INPUT_handler {
 
     if ($self->{var_num}) {
       my $typemap = $self->{typemap}->get_typemap(ctype => $var_type);
-      $self->death("Could not find a typemap for C type '$var_type'")
+      $self->report_typemap_failure($self->{typemap}, $var_type, "death")
         if not $typemap and not $is_overridden_typemap;
       $self->{proto_arg}->[$self->{var_num}] = ($typemap && $typemap->proto) || "\$";
     }
@@ -1138,7 +1147,7 @@ sub INPUT_handler {
       }
     }
     elsif ($var_init =~ /\S/) {
-      output_init( {
+      $self->output_init( {
         type          => $var_type,
         num           => $self->{var_num},
         var           => $var_name,
@@ -1147,7 +1156,7 @@ sub INPUT_handler {
       } );
     }
     elsif ($self->{var_num}) {
-      generate_init( {
+      $self->generate_init( {
         type          => $var_type,
         num           => $self->{var_num},
         var           => $var_name,
@@ -1165,7 +1174,7 @@ sub OUTPUT_handler {
   $self->{have_OUTPUT} = 1;
 
   $_ = shift;
-  for (;  !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     next unless /\S/;
     if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
       $self->{DoSetMagic} = ($1 eq "ENABLE" ? 1 : 0);
@@ -1190,7 +1199,7 @@ sub OUTPUT_handler {
       print "\tSvSETMAGIC(ST(" , $self->{var_num} - 1 , "));\n" if $self->{DoSetMagic};
     }
     else {
-      generate_output( {
+      $self->generate_output( {
         type        => $self->{var_types}->{$outarg},
         num         => $self->{var_num},
         var         => $outarg,
@@ -1291,7 +1300,6 @@ sub get_aliases {
     Warn( $self, "Warning: Aliases '$orig_alias' and '$self->{XsubAliasValues}->{$value}' have identical values")
       if $self->{XsubAliasValues}->{$value};
 
-    $self->{xsubaliases} = 1;
     $self->{XsubAliases}->{$alias} = $value;
     $self->{XsubAliasValues}->{$value} = $orig_alias;
   }
@@ -1304,7 +1312,7 @@ sub ATTRS_handler {
   my $self = shift;
   $_ = shift;
 
-  for (;  !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     next unless /\S/;
     trim_whitespace($_);
     push @{ $self->{Attributes} }, $_;
@@ -1315,7 +1323,7 @@ sub ALIAS_handler {
   my $self = shift;
   $_ = shift;
 
-  for (;  !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     next unless /\S/;
     trim_whitespace($_);
     $self->get_aliases($_) if $_;
@@ -1326,26 +1334,27 @@ sub OVERLOAD_handler {
   my $self = shift;
   $_ = shift;
 
-  for (;  !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     next unless /\S/;
     trim_whitespace($_);
     while ( s/^\s*([\w:"\\)\+\-\*\/\%\<\>\.\&\|\^\!\~\{\}\=]+)\s*//) {
       $self->{Overload} = 1 unless $self->{Overload};
-      my $overload = "$Package\::(".$1;
+      my $overload = "$self->{Package}\::(".$1;
       push(@{ $self->{InitFileCode} },
-       "        (void)$self->{newXS}(\"$overload\", XS_$Full_func_name, file$self->{proto});\n");
+       "        (void)$self->{newXS}(\"$overload\", XS_$self->{Full_func_name}$self->{file}$self->{proto});\n");
     }
   }
 }
 
 sub FALLBACK_handler {
-  my $self = shift;
-  $_ = shift;
+  my ($self, $setting) = @_;
 
   # the rest of the current line should contain either TRUE,
   # FALSE or UNDEF
 
-  trim_whitespace($_);
+  trim_whitespace($setting);
+  $setting = uc($setting);
+
   my %map = (
     TRUE => "&PL_sv_yes", 1 => "&PL_sv_yes",
     FALSE => "&PL_sv_no", 0 => "&PL_sv_no",
@@ -1353,42 +1362,39 @@ sub FALLBACK_handler {
   );
 
   # check for valid FALLBACK value
-  $self->death("Error: FALLBACK: TRUE/FALSE/UNDEF") unless exists $map{uc $_};
+  $self->death("Error: FALLBACK: TRUE/FALSE/UNDEF") unless exists $map{$setting};
 
-  $self->{Fallback} = $map{uc $_};
+  $self->{Fallback} = $map{$setting};
 }
 
 
 sub REQUIRE_handler {
-  my $self = shift;
   # the rest of the current line should contain a version number
-  my $Ver = shift;
+  my ($self, $ver) = @_;
 
-  trim_whitespace($Ver);
+  trim_whitespace($ver);
 
   $self->death("Error: REQUIRE expects a version number")
-    unless $Ver;
+    unless $ver;
 
   # check that the version number is of the form n.n
-  $self->death("Error: REQUIRE: expected a number, got '$Ver'")
-    unless $Ver =~ /^\d+(\.\d*)?/;
+  $self->death("Error: REQUIRE: expected a number, got '$ver'")
+    unless $ver =~ /^\d+(\.\d*)?/;
 
-  $self->death("Error: xsubpp $Ver (or better) required--this is only $VERSION.")
-    unless $VERSION >= $Ver;
+  $self->death("Error: xsubpp $ver (or better) required--this is only $VERSION.")
+    unless $VERSION >= $ver;
 }
 
 sub VERSIONCHECK_handler {
-  my $self = shift;
-  $_ = shift;
-
   # the rest of the current line should contain either ENABLE or
   # DISABLE
+  my ($self, $setting) = @_;
 
-  trim_whitespace($_);
+  trim_whitespace($setting);
 
   # check for ENABLE/DISABLE
   $self->death("Error: VERSIONCHECK: ENABLE/DISABLE")
-    unless /^(ENABLE|DISABLE)/i;
+    unless $setting =~ /^(ENABLE|DISABLE)/i;
 
   $self->{WantVersionChk} = 1 if $1 eq 'ENABLE';
   $self->{WantVersionChk} = 0 if $1 eq 'DISABLE';
@@ -1404,7 +1410,7 @@ sub PROTOTYPE_handler {
   $self->death("Error: Only 1 PROTOTYPE definition allowed per xsub")
     if $self->{proto_in_this_xsub}++;
 
-  for (;  !/^$self->{BLOCK_re}/o;  $_ = shift(@{ $self->{line} })) {
+  for (;  !/^$BLOCK_regexp/o;  $_ = shift(@{ $self->{line} })) {
     next unless /\S/;
     $specified = 1;
     trim_whitespace($_);
@@ -1430,30 +1436,28 @@ sub PROTOTYPE_handler {
 }
 
 sub SCOPE_handler {
-  my $self = shift;
-  $_ = shift;
+  # Rest of line should be either ENABLE or DISABLE
+  my ($self, $setting) = @_;
 
   $self->death("Error: Only 1 SCOPE declaration allowed per xsub")
     if $self->{scope_in_this_xsub}++;
 
-  trim_whitespace($_);
+  trim_whitespace($setting);
   $self->death("Error: SCOPE: ENABLE/DISABLE")
-      unless /^(ENABLE|DISABLE)\b/i;
+      unless $setting =~ /^(ENABLE|DISABLE)\b/i;
   $self->{ScopeThisXSUB} = ( uc($1) eq 'ENABLE' );
 }
 
 sub PROTOTYPES_handler {
-  my $self = shift;
-  $_ = shift;
-
   # the rest of the current line should contain either ENABLE or
   # DISABLE
+  my ($self, $setting) = @_;
 
-  trim_whitespace($_);
+  trim_whitespace($setting);
 
   # check for ENABLE/DISABLE
   $self->death("Error: PROTOTYPES: ENABLE/DISABLE")
-    unless /^(ENABLE|DISABLE)/i;
+    unless $setting =~ /^(ENABLE|DISABLE)/i;
 
   $self->{WantPrototypes} = 1 if $1 eq 'ENABLE';
   $self->{WantPrototypes} = 0 if $1 eq 'DISABLE';
@@ -1461,23 +1465,27 @@ sub PROTOTYPES_handler {
 }
 
 sub EXPORT_XSUB_SYMBOLS_handler {
-  my $self = shift;
-  $_ = shift;
-
   # the rest of the current line should contain either ENABLE or
   # DISABLE
+  my ($self, $setting) = @_;
 
-  trim_whitespace($_);
+  trim_whitespace($setting);
 
   # check for ENABLE/DISABLE
   $self->death("Error: EXPORT_XSUB_SYMBOLS: ENABLE/DISABLE")
-    unless /^(ENABLE|DISABLE)/i;
+    unless $setting =~ /^(ENABLE|DISABLE)/i;
 
   my $xs_impl = $1 eq 'ENABLE' ? 'XS_EXTERNAL' : 'XS_INTERNAL';
 
   print Q(<<"EOF");
 ##undef XS_EUPXS
-##define XS_EUPXS(name) $xs_impl(name)
+##if defined(PERL_EUPXS_ALWAYS_EXPORT)
+##  define XS_EUPXS(name) XS_EXTERNAL(name)
+##elif defined(PERL_EUPXS_NEVER_EXPORT)
+##  define XS_EUPXS(name) XS_INTERNAL(name)
+##else
+##  define XS_EUPXS(name) $xs_impl(name)
+##endif
 EOF
 }
 
@@ -1534,7 +1542,7 @@ sub INCLUDE_handler {
   $self->{FH} = Symbol::gensym();
 
   # open the new file
-  open ($self->{FH}, '<', $_) or $self->death("Cannot open '$_': $!");
+  open($self->{FH}, $_) or $self->death("Cannot open '$_': $!");
 
   print Q(<<"EOF");
 #
@@ -1569,6 +1577,25 @@ sub QuoteArgs {
   return join (' ', ($cmd, @args));
 }
 
+# code copied from CPAN::HandleConfig::safe_quote
+#  - that has doc saying leave if start/finish with same quote, but no code
+# given text, will conditionally quote it to protect from shell
+{
+  my ($quote, $use_quote) = $^O eq 'MSWin32'
+      ? (q{"}, q{"})
+      : (q{"'}, q{'});
+  sub _safe_quote {
+      my ($self, $command) = @_;
+      # Set up quote/default quote
+      if (defined($command)
+          and $command =~ /\s/
+          and $command !~ /[$quote]/) {
+          return qq{$use_quote$command$use_quote}
+      }
+      return $command;
+  }
+}
+
 sub INCLUDE_COMMAND_handler {
   my $self = shift;
   $_ = shift;
@@ -1590,7 +1617,8 @@ sub INCLUDE_COMMAND_handler {
 
   # If $^X is used in INCLUDE_COMMAND, we know it's supposed to be
   # the same perl interpreter as we're currently running
-  s/^\s*\$\^X/$^X/;
+  my $X = $self->_safe_quote($^X); # quotes if has spaces
+  s/^\s*\$\^X/$X/;
 
   # open the new file
   open ($self->{FH}, "-|", $_)
@@ -1634,7 +1662,7 @@ sub PopFile {
   close $self->{FH};
 
   $self->{FH}         = $data->{Handle};
-  # $filename is the leafname, which for some reason isused for diagnostic
+  # $filename is the leafname, which for some reason is used for diagnostic
   # messages, whereas $filepathname is the full pathname, and is used for
   # #line directives.
   $self->{filename}   = $data->{Filename};
@@ -1667,76 +1695,110 @@ sub Q {
   $text;
 }
 
+# Process "MODULE = Foo ..." lines and update global state accordingly
+sub _process_module_xs_line {
+  my ($self, $module, $pkg, $prefix) = @_;
+
+  ($self->{Module_cname} = $module) =~ s/\W/_/g;
+
+  $self->{Package} = defined($pkg) ? $pkg : '';
+  $self->{Prefix}  = quotemeta( defined($prefix) ? $prefix : '' );
+
+  ($self->{Packid} = $self->{Package}) =~ tr/:/_/;
+
+  $self->{Packprefix} = $self->{Package};
+  $self->{Packprefix} .= "::" if $self->{Packprefix} ne "";
+
+  $self->{lastline} = "";
+}
+
+# Skip any embedded POD sections
+sub _maybe_skip_pod {
+  my ($self) = @_;
+
+  while ($self->{lastline} =~ /^=/) {
+    while ($self->{lastline} = readline($self->{FH})) {
+      last if ($self->{lastline} =~ /^=cut\s*$/);
+    }
+    $self->death("Error: Unterminated pod") unless defined $self->{lastline};
+    $self->{lastline} = readline($self->{FH});
+    chomp $self->{lastline};
+    $self->{lastline} =~ s/^\s+$//;
+  }
+}
+
+# This chunk of code strips out (and parses) embedded TYPEMAP blocks
+# which support a HEREdoc-alike block syntax.
+sub _maybe_parse_typemap_block {
+  my ($self) = @_;
+
+  # This is special cased from the usual paragraph-handler logic
+  # due to the HEREdoc-ish syntax.
+  if ($self->{lastline} =~ /^TYPEMAP\s*:\s*<<\s*(?:(["'])(.+?)\1|([^\s'"]+?))\s*;?\s*$/)
+  {
+    my $end_marker = quotemeta(defined($1) ? $2 : $3);
+
+    # Scan until we find $end_marker alone on a line.
+    my @tmaplines;
+    while (1) {
+      $self->{lastline} = readline($self->{FH});
+      $self->death("Error: Unterminated TYPEMAP section") if not defined $self->{lastline};
+      last if $self->{lastline} =~ /^$end_marker\s*$/;
+      push @tmaplines, $self->{lastline};
+    }
+
+    my $tmap = ExtUtils::Typemaps->new(
+      string        => join("", @tmaplines),
+      lineno_offset => 1 + ($self->current_line_number() || 0),
+      fake_filename => $self->{filename},
+    );
+    $self->{typemap}->merge(typemap => $tmap, replace => 1);
+
+    $self->{lastline} = "";
+  }
+}
+
 # Read next xsub into @{ $self->{line} } from ($lastline, readline($self->{FH})).
 sub fetch_para {
   my $self = shift;
 
   # parse paragraph
-  $self->death("Error: Unterminated `#if/#ifdef/#ifndef'")
+  $self->death("Error: Unterminated '#if/#ifdef/#ifndef'")
     if !defined $self->{lastline} && $self->{XSStack}->[-1]{type} eq 'if';
   @{ $self->{line} } = ();
   @{ $self->{line_no} } = ();
-  return $self->PopFile() if !defined $self->{lastline};
+  return $self->PopFile() if not defined $self->{lastline}; # EOF
 
   if ($self->{lastline} =~
-      /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
-    my $Module = $1;
-    $Package = defined($2) ? $2 : ''; # keep -w happy
-    $self->{Prefix}  = defined($3) ? $3 : ''; # keep -w happy
-    $self->{Prefix} = quotemeta $self->{Prefix};
-    ($self->{Module_cname} = $Module) =~ s/\W/_/g;
-    ($self->{Packid} = $Package) =~ tr/:/_/;
-    $self->{Packprefix} = $Package;
-    $self->{Packprefix} .= "::" if $self->{Packprefix} ne "";
-    $self->{lastline} = "";
+      /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/)
+  {
+    $self->_process_module_xs_line($1, $2, $3);
   }
 
   for (;;) {
-    # Skip embedded PODs
-    while ($self->{lastline} =~ /^=/) {
-      while ($self->{lastline} = readline($self->{FH})) {
-        last if ($self->{lastline} =~ /^=cut\s*$/);
-      }
-      $self->death("Error: Unterminated pod") unless $self->{lastline};
-      $self->{lastline} = readline($self->{FH});
-      chomp $self->{lastline};
-      $self->{lastline} =~ s/^\s+$//;
-    }
-
-    # This chunk of code strips out (and parses) embedded TYPEMAP blocks
-    # which support a HEREdoc-alike block syntax.
-    # This is special cased from the usual paragraph-handler logic
-    # due to the HEREdoc-ish syntax.
-    if ($self->{lastline} =~ /^TYPEMAP\s*:\s*<<\s*(?:(["'])(.+?)\1|([^\s'"]+))\s*;?\s*$/) {
-      my $end_marker = quotemeta(defined($1) ? $2 : $3);
-      my @tmaplines;
-      while (1) {
-        $self->{lastline} = readline($self->{FH});
-        $self->death("Error: Unterminated typemap") if not defined $self->{lastline};
-        last if $self->{lastline} =~ /^$end_marker\s*$/;
-        push @tmaplines, $self->{lastline};
-      }
-
-      my $tmapcode = join "", @tmaplines;
-      my $tmap = ExtUtils::Typemaps->new(
-        string => $tmapcode,
-        lineno_offset => $self->current_line_number()+1,
-        fake_filename => $self->{filename},
-      );
-      $self->{typemap}->merge(typemap => $tmap, replace => 1);
-
-      last unless defined($self->{lastline} = readline($self->{FH}));
-      next;
-    }
-
-    if ($self->{lastline} !~ /^\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)
-    $self->{lastline} =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
+    $self->_maybe_skip_pod;
+
+    $self->_maybe_parse_typemap_block;
+
+    if ($self->{lastline} !~ /^\s*#/ # not a CPP directive
+        # 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)
+        || $self->{lastline} =~ /^\#[ \t]*
+                                  (?:
+                                        (?:if|ifn?def|elif|else|endif|
+                                           define|undef|pragma|error|
+                                           warning|line\s+\d+|ident)
+                                        \b
+                                      | (?:include(?:_next)?|import)
+                                        \s* ["<] .* [>"]
+                                 )
+                                /x
+    )
+    {
       last if $self->{lastline} =~ /^\S/ && @{ $self->{line} } && $self->{line}->[-1] eq "";
       push(@{ $self->{line} }, $self->{lastline});
       push(@{ $self->{line_no} }, $self->{lastline_no});
@@ -1752,33 +1814,37 @@ sub fetch_para {
     chomp $self->{lastline};
     $self->{lastline} =~ s/^\s+$//;
   }
-  pop(@{ $self->{line} }), pop(@{ $self->{line_no} }) while @{ $self->{line} } && $self->{line}->[-1] eq "";
-  1;
+
+  # Nuke trailing "line" entries until there's one that's not empty
+  pop(@{ $self->{line} }), pop(@{ $self->{line_no} })
+    while @{ $self->{line} } && $self->{line}->[-1] eq "";
+
+  return 1;
 }
 
 sub output_init {
+  my $self = shift;
   my $argsref = shift;
-  my ($type, $num, $var, $init, $printed_name) = (
-    $argsref->{type},
-    $argsref->{num},
-    $argsref->{var},
-    $argsref->{init},
-    $argsref->{printed_name}
-  );
-  my $arg = "ST(" . ($num - 1) . ")";
 
-  if (  $init =~ /^=/  ) {
+  my ($type, $num, $var, $init, $printed_name)
+    = @{$argsref}{qw(type num var init printed_name)};
+
+  # local assign for efficiently passing in to eval_input_typemap_code
+  local $argsref->{arg} = $num
+                          ? "ST(" . ($num-1) . ")"
+                          : "/* not a parameter */";
+
+  if ( $init =~ /^=/ ) {
     if ($printed_name) {
-      eval qq/print " $init\\n"/;
+      $self->eval_input_typemap_code(qq/print " $init\\n"/, $argsref);
     }
     else {
-      eval qq/print "\\t$var $init\\n"/;
+      $self->eval_input_typemap_code(qq/print "\\t$var $init\\n"/, $argsref);
     }
-    warn $@ if $@;
   }
   else {
     if (  $init =~ s/^\+//  &&  $num  ) {
-      generate_init( {
+      $self->generate_init( {
         type          => $type,
         num           => $num,
         var           => $var,
@@ -1790,39 +1856,41 @@ sub output_init {
       $init =~ s/^;//;
     }
     else {
-      eval qq/print "\\t$var;\\n"/;
-      warn $@ if $@;
+      $self->eval_input_typemap_code(qq/print "\\t$var;\\n"/, $argsref);
       $init =~ s/^;//;
     }
-    $self->{deferred} .= eval qq/"\\n\\t$init\\n"/;
-    warn $@ if $@;
+    $self->{deferred}
+      .= $self->eval_input_typemap_code(qq/"\\n\\t$init\\n"/, $argsref);
   }
 }
 
 sub generate_init {
+  my $self = shift;
   my $argsref = shift;
-  my ($type, $num, $var, $printed_name) = (
-    $argsref->{type},
-    $argsref->{num},
-    $argsref->{var},
-    $argsref->{printed_name},
-  );
-  my $arg = "ST(" . ($num - 1) . ")";
-  my ($argoff, $ntype);
-  $argoff = $num - 1;
+
+  my ($type, $num, $var, $printed_name)
+    = @{$argsref}{qw(type num var printed_name)};
+
+  my $argoff = $num - 1;
+  my $arg = "ST($argoff)";
 
   my $typemaps = $self->{typemap};
 
-  $type = tidy_type($type);
-  $self->blurt("Error: '$type' not in typemap"), return
-    unless $typemaps->get_typemap(ctype => $type);
+  $type = ExtUtils::Typemaps::tidy_type($type);
+  if (not $typemaps->get_typemap(ctype => $type)) {
+    $self->report_typemap_failure($typemaps, $type);
+    return;
+  }
+
+  (my $ntype = $type) =~ s/\s*\*/Ptr/g;
+  (my $subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
 
-  ($ntype = $type) =~ s/\s*\*/Ptr/g;
-  my $subtype;
-  ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
   my $typem = $typemaps->get_typemap(ctype => $type);
   my $xstype = $typem->xstype;
-  $xstype =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
+  #this is an optimization from perl 5.0 alpha 6, class check is skipped
+  #T_REF_IV_REF is missing since it has no untyped analog at the moment
+  $xstype =~ s/OBJ$/REF/ || $xstype =~ s/^T_REF_IV_PTR$/T_PTRREF/
+    if $self->{func_name} =~ /DESTROY$/;
   if ($xstype eq 'T_PV' and exists $self->{lengthof}->{$var}) {
     print "\t$var" unless $printed_name;
     print " = ($type)SvPV($arg, STRLEN_length_of_$var);\n";
@@ -1830,33 +1898,53 @@ sub generate_init {
       if defined $self->{defaults}->{$var};
     return;
   }
-  $type =~ tr/:/_/ unless $self->{hiertype};
+  $type =~ tr/:/_/ unless $self->{RetainCplusplusHierarchicalTypes};
 
   my $inputmap = $typemaps->get_inputmap(xstype => $xstype);
-  $self->blurt("Error: No INPUT definition for type '$type', typekind '" . $type->xstype . "' found"), return
-    unless defined $inputmap;
+  if (not defined $inputmap) {
+    $self->blurt("Error: No INPUT definition for type '$type', typekind '" . $type->xstype . "' found");
+    return;
+  }
 
   my $expr = $inputmap->cleaned_code;
   # Note: This gruesome bit either needs heavy rethinking or documentation. I vote for the former. --Steffen
   if ($expr =~ /DO_ARRAY_ELEM/) {
     my $subtypemap  = $typemaps->get_typemap(ctype => $subtype);
-    $self->blurt("Error: C type '$subtype' not in typemap"), return
-      if not $subtypemap;
+    if (not $subtypemap) {
+      $self->report_typemap_failure($typemaps, $subtype);
+      return;
+    }
+
     my $subinputmap = $typemaps->get_inputmap(xstype => $subtypemap->xstype);
-    $self->blurt("Error: No INPUT definition for type '$subtype', typekind '" . $subtypemap->xstype . "' found"), return
-      unless $subinputmap;
+    if (not $subinputmap) {
+      $self->blurt("Error: No INPUT definition for type '$subtype', typekind '" . $subtypemap->xstype . "' found");
+      return;
+    }
+
     my $subexpr = $subinputmap->cleaned_code;
     $subexpr =~ s/\$type/\$subtype/g;
     $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/\$var/${var}[ix_$var - $argoff]/;
+    $subexpr =~ s/\$var/${var}\[ix_$var - $argoff]/;
     $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
   }
   if ($expr =~ m#/\*.*scope.*\*/#i) {  # "scope" in C comments
     $self->{ScopeThisXSUB} = 1;
   }
+
+  my $eval_vars = {
+    var           => $var,
+    printed_name  => $printed_name,
+    type          => $type,
+    ntype         => $ntype,
+    subtype       => $subtype,
+    num           => $num,
+    arg           => $arg,
+    argoff        => $argoff,
+  };
+
   if (defined($self->{defaults}->{$var})) {
     $expr =~ s/(\t+)/$1    /g;
     $expr =~ s/        /\t/g;
@@ -1864,129 +1952,198 @@ sub generate_init {
       print ";\n";
     }
     else {
-      eval qq/print "\\t$var;\\n"/;
-      warn $@ if $@;
+      $self->eval_input_typemap_code(qq/print "\\t$var;\\n"/, $eval_vars);
     }
     if ($self->{defaults}->{$var} eq 'NO_INIT') {
-      $self->{deferred} .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
+      $self->{deferred} .= $self->eval_input_typemap_code(
+        qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/,
+        $eval_vars
+      );
     }
     else {
-      $self->{deferred} .= eval qq/"\\n\\tif (items < $num)\\n\\t    $var = $self->{defaults}->{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
+      $self->{deferred} .= $self->eval_input_typemap_code(
+        qq/"\\n\\tif (items < $num)\\n\\t    $var = $self->{defaults}->{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/,
+        $eval_vars
+      );
     }
-    warn $@ if $@;
   }
   elsif ($self->{ScopeThisXSUB} or $expr !~ /^\s*\$var =/) {
     if ($printed_name) {
       print ";\n";
     }
     else {
-      eval qq/print "\\t$var;\\n"/;
-      warn $@ if $@;
+      $self->eval_input_typemap_code(qq/print "\\t$var;\\n"/, $eval_vars);
     }
-    $self->{deferred} .= eval qq/"\\n$expr;\\n"/;
-    warn $@ if $@;
+    $self->{deferred}
+      .= $self->eval_input_typemap_code(qq/"\\n$expr;\\n"/, $eval_vars);
   }
   else {
     die "panic: do not know how to handle this branch for function pointers"
       if $printed_name;
-    eval qq/print "$expr;\\n"/;
-    warn $@ if $@;
+    $self->eval_input_typemap_code(qq/print "$expr;\\n"/, $eval_vars);
   }
 }
 
 sub generate_output {
+  my $self = shift;
   my $argsref = shift;
-  my ($type, $num, $var, $do_setmagic, $do_push) = (
-    $argsref->{type},
-    $argsref->{num},
-    $argsref->{var},
-    $argsref->{do_setmagic},
-    $argsref->{do_push}
-  );
+  my ($type, $num, $var, $do_setmagic, $do_push)
+    = @{$argsref}{qw(type num var do_setmagic do_push)};
+
   my $arg = "ST(" . ($num - ($num != 0)) . ")";
-  my $ntype;
 
   my $typemaps = $self->{typemap};
 
-  $type = tidy_type($type);
+  $type = ExtUtils::Typemaps::tidy_type($type);
+  local $argsref->{type} = $type;
+
   if ($type =~ /^array\(([^,]*),(.*)\)/) {
     print "\t$arg = sv_newmortal();\n";
     print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n";
     print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
   }
   else {
-    my $typemap   = $typemaps->get_typemap(ctype => $type);
-    $self->blurt("Could not find a typemap for C type '$type'"), return
-      if not $typemap;
+    my $typemap = $typemaps->get_typemap(ctype => $type);
+    if (not $typemap) {
+      $self->report_typemap_failure($typemaps, $type);
+      return;
+    }
+
     my $outputmap = $typemaps->get_outputmap(xstype => $typemap->xstype);
-    $self->blurt("Error: No OUTPUT definition for type '$type', typekind '" . $typemap->xstype . "' found"), return
-      unless $outputmap;
-    ($ntype = $type) =~ s/\s*\*/Ptr/g;
+    if (not $outputmap) {
+      $self->blurt("Error: No OUTPUT definition for type '$type', typekind '" . $typemap->xstype . "' found");
+      return;
+    }
+
+    (my $ntype = $type) =~ s/\s*\*/Ptr/g;
     $ntype =~ s/\(\)//g;
-    my $subtype;
-    ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
+    (my $subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
 
+    my $eval_vars = {%$argsref, subtype => $subtype, ntype => $ntype, arg => $arg};
     my $expr = $outputmap->cleaned_code;
     if ($expr =~ /DO_ARRAY_ELEM/) {
-      my $subtypemap   = $typemaps->get_typemap(ctype => $subtype);
-      $self->blurt("Could not find a typemap for C type '$subtype'"), return
-        if not $subtypemap;
+      my $subtypemap = $typemaps->get_typemap(ctype => $subtype);
+      if (not $subtypemap) {
+        $self->report_typemap_failure($typemaps, $subtype);
+        return;
+      }
+
       my $suboutputmap = $typemaps->get_outputmap(xstype => $subtypemap->xstype);
-      $self->blurt("Error: No OUTPUT definition for type '$subtype', typekind '" . $subtypemap->xstype . "' found"), return
-        unless $suboutputmap;
+      if (not $suboutputmap) {
+        $self->blurt("Error: No OUTPUT definition for type '$subtype', typekind '" . $subtypemap->xstype . "' found");
+        return;
+      }
+
       my $subexpr = $suboutputmap->cleaned_code;
       $subexpr =~ s/ntype/subtype/g;
       $subexpr =~ s/\$arg/ST(ix_$var)/g;
-      $subexpr =~ s/\$var/${var}[ix_$var]/g;
+      $subexpr =~ s/\$var/${var}\[ix_$var]/g;
       $subexpr =~ s/\n\t/\n\t\t/g;
       $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
-      eval "print qq\a$expr\a";
-      warn $@ if $@;
+      $self->eval_output_typemap_code("print qq\a$expr\a", $eval_vars);
       print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic;
     }
     elsif ($var eq 'RETVAL') {
-      if ($expr =~ /^\t\$arg = new/) {
+      my $orig_arg = $arg;
+      my $indent;
+      my $use_RETVALSV = 1;
+      my $do_mortal = 0;
+      my $do_copy_tmp = 1;
+      my $pre_expr;
+      local $eval_vars->{arg} = $arg = 'RETVALSV';
+      my $evalexpr = $self->eval_output_typemap_code("qq\a$expr\a", $eval_vars);
+
+      if ($expr =~ /^\t\Q$arg\E = new/) {
         # We expect that $arg has refcnt 1, so we need to
         # mortalize it.
-        eval "print qq\a$expr\a";
-        warn $@ if $@;
-        print "\tsv_2mortal(ST($num));\n";
-        print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic;
+        $do_mortal = 1;
+      }
+      # If RETVAL is immortal, don't mortalize it. This code is not perfect:
+      # It won't detect a func or expression that only returns immortals, for
+      # example, this RE must be tried before next elsif.
+      elsif ($evalexpr =~ /^\t\Q$arg\E\s*=\s*(boolSV\(|(&PL_sv_yes|&PL_sv_no|&PL_sv_undef)\s*;)/) {
+        $do_copy_tmp = 0; #$arg will be a ST(X), no SV* RETVAL, no RETVALSV
+        $use_RETVALSV = 0;
       }
-      elsif ($expr =~ /^\s*\$arg\s*=/) {
+      elsif ($evalexpr =~ /^\s*\Q$arg\E\s*=/) {
         # We expect that $arg has refcnt >=1, so we need
         # to mortalize it!
-        eval "print qq\a$expr\a";
-        warn $@ if $@;
-        print "\tsv_2mortal(ST(0));\n";
-        print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic;
+        $use_RETVALSV = 0 if $ntype eq "SVPtr";#reuse SV* RETVAL vs open new block
+        $do_mortal = 1;
       }
       else {
         # Just hope that the entry would safely write it
         # over an already mortalized value. By
-        # coincidence, something like $arg = &sv_undef
-        # works too.
-        print "\tST(0) = sv_newmortal();\n";
-        eval "print qq\a$expr\a";
-        warn $@ if $@;
+        # coincidence, something like $arg = &PL_sv_undef
+        # works too, but should be caught above.
+        $pre_expr = "RETVALSV = sv_newmortal();\n";
         # new mortals don't have set magic
+        $do_setmagic = 0;
+      }
+      if($use_RETVALSV) {
+        print "\t{\n\t    SV * RETVALSV;\n";
+        $indent = "\t    ";
+      } else {
+        $indent = "\t";
+      }
+      print $indent.$pre_expr if $pre_expr;
+
+      if($use_RETVALSV) {
+        #take control of 1 layer of indent, may or may not indent more
+        $evalexpr =~ s/^(\t|        )/$indent/gm;
+        #"\t    \t" doesn't draw right in some IDEs
+        #break down all \t into spaces
+        $evalexpr =~ s/\t/        /g;
+        #rebuild back into \t'es, \t==8 spaces, indent==4 spaces
+        $evalexpr =~ s/        /\t/g;
       }
+      else {
+        if($do_mortal || $do_setmagic) {
+        #typemap entry evaled with RETVALSV, if we aren't using RETVALSV replace
+          $evalexpr =~ s/RETVALSV/RETVAL/g; #all uses with RETVAL for prettier code
+        }
+        else { #if no extra boilerplate (no mortal, no set magic) is needed
+            #after $evalexport, get rid of RETVALSV's visual cluter and change
+          $evalexpr =~ s/RETVALSV/$orig_arg/g;#the lvalue to ST(X)
+        }
+      }
+      #stop "  RETVAL = RETVAL;" for SVPtr type
+      print $evalexpr if $evalexpr !~ /^\s*RETVAL = RETVAL;$/;
+      print $indent.'RETVAL'.($use_RETVALSV ? 'SV':'')
+            .' = sv_2mortal(RETVAL'.($use_RETVALSV ? 'SV':'').");\n" if $do_mortal;
+      print $indent.'SvSETMAGIC(RETVAL'.($use_RETVALSV ? 'SV':'').");\n" if $do_setmagic;
+      #dont do "RETVALSV = boolSV(RETVAL); ST(0) = RETVALSV;", it is visual clutter
+      print $indent."$orig_arg = RETVAL".($use_RETVALSV ? 'SV':'').";\n"
+        if $do_mortal || $do_setmagic || $do_copy_tmp;
+      print "\t}\n" if $use_RETVALSV;
     }
     elsif ($do_push) {
       print "\tPUSHs(sv_newmortal());\n";
-      $arg = "ST($num)";
-      eval "print qq\a$expr\a";
-      warn $@ if $@;
+      local $eval_vars->{arg} = "ST($num)";
+      $self->eval_output_typemap_code("print qq\a$expr\a", $eval_vars);
       print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
     }
     elsif ($arg =~ /^ST\(\d+\)$/) {
-      eval "print qq\a$expr\a";
-      warn $@ if $@;
+      $self->eval_output_typemap_code("print qq\a$expr\a", $eval_vars);
       print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
     }
   }
 }
 
+
+# Just delegates to a clean package.
+# Shim to evaluate Perl code in the right variable context
+# for typemap code (having things such as $ALIAS set up).
+sub eval_output_typemap_code {
+  my ($self, $code, $other) = @_;
+  return ExtUtils::ParseXS::Eval::eval_output_typemap_code($self, $code, $other);
+}
+
+sub eval_input_typemap_code {
+  my ($self, $code, $other) = @_;
+  return ExtUtils::ParseXS::Eval::eval_input_typemap_code($self, $code, $other);
+}
+
 1;
 
 # vim: ts=2 sw=2 et: