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